Merge pull request #61 from tgolsson/ts/filestorage-from-path

actually take path for `FileStorage::from_path`
This commit is contained in:
Emil Ernerfeldt 2020-12-11 12:02:56 +01:00 committed by GitHub
commit c173ea6b2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 6 deletions

View file

@ -6,7 +6,7 @@ fn main() {
let title = "Egui glium demo"; let title = "Egui glium demo";
// Persist app state to file: // Persist app state to file:
let storage = egui_glium::storage::FileStorage::from_path(".egui_demo_glium.json".into()); let storage = egui_glium::storage::FileStorage::from_path(".egui_demo_glium.json");
// Alternative: store nowhere // Alternative: store nowhere
// let storage = egui::app::DummyStorage::default(); // let storage = egui::app::DummyStorage::default();

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased ## Unreleased
### Changed
* FileStorage::from_path now takes `Into<Path>` instead of `String`
## 0.4.0 - 2020-11-28 ## 0.4.0 - 2020-11-28
Started changelog. Features: Started changelog. Features:

View file

@ -1,17 +1,21 @@
use std::collections::HashMap; use std::{
collections::HashMap,
path::{Path, PathBuf},
};
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/// A key-value store backed by a JSON file on disk. /// A key-value store backed by a JSON file on disk.
/// Used to restore egui state, glium window position/size and app state. /// Used to restore egui state, glium window position/size and app state.
pub struct FileStorage { pub struct FileStorage {
path: String, path: PathBuf,
kv: HashMap<String, String>, kv: HashMap<String, String>,
dirty: bool, dirty: bool,
} }
impl FileStorage { impl FileStorage {
pub fn from_path(path: String) -> Self { pub fn from_path(path: impl Into<PathBuf>) -> Self {
let path: PathBuf = path.into();
Self { Self {
kv: read_json(&path).unwrap_or_default(), kv: read_json(&path).unwrap_or_default(),
path, path,
@ -42,7 +46,7 @@ impl egui::app::Storage for FileStorage {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
pub fn read_json<T>(memory_json_path: impl AsRef<std::path::Path>) -> Option<T> pub fn read_json<T>(memory_json_path: impl AsRef<Path>) -> Option<T>
where where
T: serde::de::DeserializeOwned, T: serde::de::DeserializeOwned,
{ {

1
example_glium/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.egui_example_glium.json

View file

@ -10,7 +10,7 @@ fn main() {
let title = "My Egui Window"; let title = "My Egui Window";
// Persist app state to file: // Persist app state to file:
let storage = egui_glium::storage::FileStorage::from_path(".egui_example_glium.json".into()); let storage = egui_glium::storage::FileStorage::from_path(".egui_example_glium.json");
// Alternative: store nowhere // Alternative: store nowhere
// let storage = egui::app::DummyStorage::default(); // let storage = egui::app::DummyStorage::default();