gveditor_core_api/state_persistors/
file.rs1use std::fs;
2use std::path::PathBuf;
3
4use crate::state::StateData;
5
6use super::Persistor;
7
8#[derive(Clone)]
10pub struct FilePersistor {
11    path: PathBuf,
12}
13
14impl FilePersistor {
15    pub fn new(path: PathBuf) -> Self {
16        Self { path }
17    }
18}
19
20impl Persistor for FilePersistor {
22    fn load(&mut self) -> StateData {
23        let file_content = fs::read_to_string(&self.path).expect("Failed to read file");
24        serde_json::from_str(&file_content).unwrap_or_default()
25    }
26
27    fn save(&mut self, state: &StateData) {
28        let file_content = serde_json::to_string(&state).unwrap();
29        fs::write(&self.path, file_content.as_bytes()).unwrap();
30    }
31}