Skip to content

Commit

Permalink
refactor persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Feb 27, 2024
1 parent 2886344 commit 73e1284
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use crate::{
constants::*,
localization::{t, LOCALE},
persistent,
persistent::Store,
state::{read, write, STATE},
utils::*,
};
Expand All @@ -29,7 +29,7 @@ pub struct App {

impl Default for App {
fn default() -> Self {
let (df_bin, selected_language) = match persistent::load() {
let (df_bin, selected_language) = match Store::load() {
Ok(store) => {
write!(hook_manifest, store.hook_manifest);
write!(dict_manifest, store.dict_manifest);
Expand Down Expand Up @@ -63,14 +63,15 @@ impl Default for App {
impl eframe::App for App {
fn on_close_event(&mut self) -> bool {
if self.df_bin.is_some() {
let _ = persistent::save(persistent::Store {
let _ = Store {
df_bin: self.df_bin.clone().unwrap().as_path().display().to_string(),
hook_manifest: read!(hook_manifest).clone(),
vec_hook_manifests: read!(vec_hook_manifests).clone(),
dict_manifest: read!(dict_manifest).clone(),
vec_dict_manifests: read!(vec_dict_manifests).clone(),
selected_language: self.selected_language.clone(),
});
}
.save();
}
true
}
Expand Down
18 changes: 10 additions & 8 deletions src/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ pub struct Store {
pub selected_language: String,
}

pub fn save(store: Store) -> Result<()> {
let _ = std::fs::write(PATH_CACHE_FILE, serde_json::to_string_pretty(&store)?)?;
Ok(())
}
impl Store {
pub fn load() -> Result<Self> {
let content = std::fs::read_to_string(PATH_CACHE_FILE)?;
let store: Store = serde_json::from_str(&content)?;
Ok(store)
}

pub fn load() -> Result<Store> {
let content = std::fs::read_to_string(PATH_CACHE_FILE)?;
let store: Store = serde_json::from_str(&content)?;
Ok(store)
pub fn save(&self) -> Result<()> {
let _ = std::fs::write(PATH_CACHE_FILE, serde_json::to_string_pretty(self)?)?;
Ok(())
}
}

0 comments on commit 73e1284

Please sign in to comment.