2021-01-04 00:44:02 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-04-05 10:09:01 +00:00
|
|
|
/// A key-value store backed by a [RON](https://github.com/ron-rs/ron) file on disk.
|
2021-01-04 00:44:02 +00:00
|
|
|
/// Used to restore egui state, glium window position/size and app state.
|
|
|
|
pub struct FileStorage {
|
|
|
|
path: PathBuf,
|
|
|
|
kv: HashMap<String, String>,
|
|
|
|
dirty: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileStorage {
|
|
|
|
pub fn from_path(path: impl Into<PathBuf>) -> Self {
|
|
|
|
let path: PathBuf = path.into();
|
|
|
|
Self {
|
2021-04-05 10:09:01 +00:00
|
|
|
kv: read_ron(&path).unwrap_or_default(),
|
2021-01-04 00:44:02 +00:00
|
|
|
path,
|
|
|
|
dirty: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl epi::Storage for FileStorage {
|
|
|
|
fn get_string(&self, key: &str) -> Option<String> {
|
|
|
|
self.kv.get(key).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_string(&mut self, key: &str, value: String) {
|
|
|
|
if self.kv.get(key) != Some(&value) {
|
|
|
|
self.kv.insert(key.to_owned(), value);
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) {
|
|
|
|
if self.dirty {
|
2021-02-23 19:28:55 +00:00
|
|
|
// eprintln!("Persisted to {}", self.path.display());
|
2021-04-05 10:09:01 +00:00
|
|
|
let file = std::fs::File::create(&self.path).unwrap();
|
|
|
|
let config = Default::default();
|
|
|
|
ron::ser::to_writer_pretty(file, &self.kv, config).unwrap();
|
2021-01-04 00:44:02 +00:00
|
|
|
self.dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-04-05 10:09:01 +00:00
|
|
|
pub fn read_ron<T>(ron_path: impl AsRef<Path>) -> Option<T>
|
2021-01-04 00:44:02 +00:00
|
|
|
where
|
|
|
|
T: serde::de::DeserializeOwned,
|
|
|
|
{
|
2021-04-05 10:09:01 +00:00
|
|
|
match std::fs::File::open(ron_path) {
|
2021-01-04 00:44:02 +00:00
|
|
|
Ok(file) => {
|
|
|
|
let reader = std::io::BufReader::new(file);
|
2021-04-05 10:09:01 +00:00
|
|
|
match ron::de::from_reader(reader) {
|
2021-01-04 00:44:02 +00:00
|
|
|
Ok(value) => Some(value),
|
|
|
|
Err(err) => {
|
2021-04-05 10:09:01 +00:00
|
|
|
eprintln!("ERROR: Failed to parse RON: {}", err);
|
2021-01-04 00:44:02 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_err) => {
|
|
|
|
// File probably doesn't exist. That's fine.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Alternative to `FileStorage`
|
2021-04-05 10:09:01 +00:00
|
|
|
pub fn read_memory(ctx: &egui::Context, memory_file_path: impl AsRef<std::path::Path>) {
|
|
|
|
let memory: Option<egui::Memory> = read_ron(memory_file_path);
|
2021-01-04 00:44:02 +00:00
|
|
|
if let Some(memory) = memory {
|
|
|
|
*ctx.memory() = memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Alternative to `FileStorage`
|
|
|
|
pub fn write_memory(
|
|
|
|
ctx: &egui::Context,
|
2021-04-05 10:09:01 +00:00
|
|
|
memory_file_path: impl AsRef<std::path::Path>,
|
2021-01-04 00:44:02 +00:00
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
2021-04-05 10:09:01 +00:00
|
|
|
let file = std::fs::File::create(memory_file_path)?;
|
|
|
|
let ron_config = Default::default();
|
|
|
|
ron::ser::to_writer_pretty(file, &*ctx.memory(), ron_config)?;
|
2021-01-04 00:44:02 +00:00
|
|
|
Ok(())
|
|
|
|
}
|