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
pull/1079/head
thewh1teagle 1 year ago committed by GitHub
parent 57d01bf82e
commit c013fa52cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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`.

@ -21,5 +21,4 @@ serde_json = { workspace = true }
tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
bincode = "1.3"
bitflags = "2"

@ -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<bincode::ErrorKind>),
SerdeJson(#[from] serde_json::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
@ -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(())
}
@ -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(())
})

Loading…
Cancel
Save