use crate::{Dimension, WallSwitchResult, get_config_path};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, path::PathBuf};
const MAX_ITENS: usize = 10_000;
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct CacheEntry {
pub size: u64,
pub mtime: u64,
pub hash: String,
#[serde(default)]
pub dimension: Option<Dimension>,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct State {
pub history: Vec<PathBuf>,
pub hashes: HashMap<PathBuf, CacheEntry>,
}
impl State {
pub fn load() -> Self {
if let Ok(path) = Self::get_path()
&& let Ok(content) = fs::read_to_string(&path)
&& let Ok(state) = serde_json::from_str(&content)
{
return state;
}
State::default()
}
pub fn save(&mut self) -> WallSwitchResult<()> {
if self.history.len() > MAX_ITENS {
let start = self.history.len() - MAX_ITENS;
self.history = self.history[start..].to_vec();
}
let path = Self::get_path()?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let file = fs::File::create(path)?;
serde_json::to_writer_pretty(file, self)?;
Ok(())
}
pub fn garbage_collect(&mut self) {
self.hashes.retain(|path, _| path.exists());
}
fn get_path() -> WallSwitchResult<PathBuf> {
let mut path = get_config_path()?;
path.set_file_name("wallswitch-state.json");
Ok(path)
}
}