diff options
| author | Joe Carstairs <me@joeac.net> | 2026-02-07 18:45:08 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-02-07 18:45:17 +0000 |
| commit | 65596363d43995bf2002d4aa8405d84484f2542b (patch) | |
| tree | 830846f335d97a236c61ff8ac3a9080b455e3b4a /schist_desktop_gui/src/cache.rs | |
| parent | 0986b29354690327b5dafc852667c7dc191d1a22 (diff) | |
task-088: cache open file
Diffstat (limited to 'schist_desktop_gui/src/cache.rs')
| -rw-r--r-- | schist_desktop_gui/src/cache.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/schist_desktop_gui/src/cache.rs b/schist_desktop_gui/src/cache.rs new file mode 100644 index 0000000..51e2c55 --- /dev/null +++ b/schist_desktop_gui/src/cache.rs @@ -0,0 +1,83 @@ +use std::{ + fs::{create_dir_all, exists, read, write, File}, + path::{Path, PathBuf}, +}; + +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; + +const SCHIST_CACHE_DIR: &'static str = "schist"; +const SCHIST_CACHE_FILENAME: &'static str = "schist_cache.toml"; + +#[derive(Default, Deserialize, Serialize)] +pub struct Cache { + pub last_open_file: Option<String>, +} + +pub struct CacheKeys { + pub last_open_file: &'static str, +} + +pub const CACHE_KEYS: CacheKeys = CacheKeys { + last_open_file: "last_open_file", +}; + +pub fn get_cache() -> Result<Cache> { + const CONTEXT: &'static str = "Failed to get cache"; + let cache_filepath = get_and_touch_cache_filepath().context(CONTEXT)?; + let cache_toml = read(&cache_filepath).context(CONTEXT)?; + let cache_table: toml::Table = toml::from_slice(&cache_toml).context(CONTEXT)?; + Ok(Cache { + last_open_file: cache_table + .get(&CACHE_KEYS.last_open_file.to_string()) + .map(|o| o.as_str().map(str::to_string)) + .flatten(), + }) +} + +pub fn put_to_cache<T>(key: &str, value: T) -> Result<()> +where + toml::Value: From<T>, +{ + const CONTEXT: &'static str = "Failed to save cache"; + let cache_filepath = get_and_touch_cache_filepath().context(CONTEXT)?; + let curr_cache_toml = read(&cache_filepath).context(CONTEXT)?; + let mut cache: toml::Table = toml::from_slice(&curr_cache_toml).context(CONTEXT)?; + cache.insert(key.to_string(), value.into()); + let new_cache_toml = toml::to_string(&cache).context(CONTEXT)?; + write(cache_filepath, new_cache_toml).context(CONTEXT)?; + Ok(()) +} + +pub fn remove_from_cache(key: &str) -> Result<()> { + const CONTEXT: &'static str = "Failed to remove key from cache"; + let cache_filepath = get_and_touch_cache_filepath().context(CONTEXT)?; + let curr_cache_toml = read(&cache_filepath).context(CONTEXT)?; + let mut cache: toml::Table = toml::from_slice(&curr_cache_toml).context(CONTEXT)?; + cache.remove(key); + let new_cache_toml = toml::to_string(&cache).context(CONTEXT)?; + write(cache_filepath, new_cache_toml).context(CONTEXT)?; + Ok(()) +} + +fn get_and_touch_cache_filepath() -> Result<PathBuf, anyhow::Error> { + let cache_dir = get_and_make_schist_cache_dir()?; + let cache_filepath = cache_dir.join(Path::new(SCHIST_CACHE_FILENAME)); + let does_cache_file_exist = exists(cache_filepath.clone()) + .context("Failed to determine the existence of Schist's cache file")?; + if !does_cache_file_exist { + File::create(cache_filepath.clone()).context("Failed to create a new cache file")?; + } + Ok(cache_filepath) +} + +fn get_and_make_schist_cache_dir() -> anyhow::Result<PathBuf> { + let cache_dir = dirs::cache_dir().context("Failed to get app cache directory")?; + let schist_cache_dir = cache_dir.join(Path::new(SCHIST_CACHE_DIR)); + create_dir_all(&schist_cache_dir).context(format!( + "Failed to create app cache directory at {}", + schist_cache_dir.display() + ))?; + println!("Found app cache directory at {:?}", schist_cache_dir); + Ok(schist_cache_dir) +} |
