Skip to content

Commit

Permalink
refactor(window-state)!: use json instead of bincode (#1078)
Browse files Browse the repository at this point in the history
* feat(window-state): add option to use json

* feat(window-state): change state file from bincode to serdejson

* change file

* fmt
  • Loading branch information
thewh1teagle authored Mar 29, 2024
1 parent 57d01bf commit c013fa5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/window-state-json.md
Original file line number Diff line number Diff line change
@@ -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`.
1 change: 0 additions & 1 deletion plugins/window-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ serde_json = { workspace = true }
tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
bincode = "1.3"
bitflags = "2"
41 changes: 19 additions & 22 deletions plugins/window-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,7 +37,7 @@ pub enum Error {
#[error(transparent)]
Tauri(#[from] tauri::Error),
#[error(transparent)]
Bincode(#[from] Box<bincode::ErrorKind>),
SerdeJson(#[from] serde_json::Error),
}

pub type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -116,10 +115,7 @@ impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
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(())
}
Expand Down Expand Up @@ -324,23 +320,24 @@ impl Builder {
cmd::restore_state
])
.setup(|app, _api| {
let cache: Arc<Mutex<HashMap<String, WindowState>>> = 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<Mutex<HashMap<String, WindowState>>> =
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(())
})
Expand Down

0 comments on commit c013fa5

Please sign in to comment.