From c013fa52cd66885cf457a64e75373cb2066bc849 Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Fri, 29 Mar 2024 02:41:42 +0200 Subject: [PATCH] refactor(window-state)!: use json instead of bincode (#1078) * feat(window-state): add option to use json * feat(window-state): change state file from bincode to serdejson * change file * fmt --- .changes/window-state-json.md | 5 ++++ plugins/window-state/Cargo.toml | 1 - plugins/window-state/src/lib.rs | 41 +++++++++++++++------------------ 3 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 .changes/window-state-json.md diff --git a/.changes/window-state-json.md b/.changes/window-state-json.md new file mode 100644 index 000000000..9da7561bb --- /dev/null +++ b/.changes/window-state-json.md @@ -0,0 +1,5 @@ +--- +"window-state": patch +--- + +**Breaking change**: Changed the format of the state file from bincode to json. Also changed the filename to from `.window-state` to `.window-state.json`. \ No newline at end of file diff --git a/plugins/window-state/Cargo.toml b/plugins/window-state/Cargo.toml index 996455476..76dac5727 100644 --- a/plugins/window-state/Cargo.toml +++ b/plugins/window-state/Cargo.toml @@ -21,5 +21,4 @@ serde_json = { workspace = true } tauri = { workspace = true } log = { workspace = true } thiserror = { workspace = true } -bincode = "1.3" bitflags = "2" diff --git a/plugins/window-state/src/lib.rs b/plugins/window-state/src/lib.rs index ed9f10cfe..393c23025 100644 --- a/plugins/window-state/src/lib.rs +++ b/plugins/window-state/src/lib.rs @@ -23,13 +23,12 @@ use tauri::{ use std::{ collections::{HashMap, HashSet}, fs::{create_dir_all, File}, - io::Write, sync::{Arc, Mutex}, }; mod cmd; -pub const STATE_FILENAME: &str = ".window-state"; +pub const STATE_FILENAME: &str = ".window-state.json"; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -38,7 +37,7 @@ pub enum Error { #[error(transparent)] Tauri(#[from] tauri::Error), #[error(transparent)] - Bincode(#[from] Box), + SerdeJson(#[from] serde_json::Error), } pub type Result = std::result::Result; @@ -116,10 +115,7 @@ impl AppHandleExt for tauri::AppHandle { create_dir_all(&app_dir) .map_err(Error::Io) .and_then(|_| File::create(state_path).map_err(Into::into)) - .and_then(|mut f| { - f.write_all(&bincode::serialize(&*state).map_err(Error::Bincode)?) - .map_err(Into::into) - }) + .and_then(|mut f| serde_json::to_writer_pretty(&mut f, &*state).map_err(Into::into)) } else { Ok(()) } @@ -324,23 +320,24 @@ impl Builder { cmd::restore_state ]) .setup(|app, _api| { - let cache: Arc>> = if let Ok(app_dir) = - app.path().app_config_dir() - { - let state_path = app_dir.join(STATE_FILENAME); - if state_path.exists() { - Arc::new(Mutex::new( - std::fs::read(state_path) - .map_err(Error::from) - .and_then(|state| bincode::deserialize(&state).map_err(Into::into)) - .unwrap_or_default(), - )) + let cache: Arc>> = + if let Ok(app_dir) = app.path().app_config_dir() { + let state_path = app_dir.join(STATE_FILENAME); + if state_path.exists() { + Arc::new(Mutex::new( + std::fs::read(state_path) + .map_err(Error::from) + .and_then(|state| { + serde_json::from_slice(&state).map_err(Into::into) + }) + .unwrap_or_default(), + )) + } else { + Default::default() + } } else { Default::default() - } - } else { - Default::default() - }; + }; app.manage(WindowStateCache(cache)); Ok(()) })