Skip to content

Commit 455c550

Browse files
authored
refactor(core): Settings serialization using bincode (#1758)
1 parent 8d2e4c6 commit 455c550

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

.changes/refactor-settings.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
`Settings` is now serialized using `bincode`.

core/tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ os_pipe = "0.9"
5050
minisign-verify = "0.1.8"
5151
image = "0.23"
5252
state = "0.4"
53+
bincode = "1.3"
5354

5455
[build-dependencies]
5556
cfg_aliases = "0.1.1"

core/tauri/src/api/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub enum Error {
4545
/// JSON error.
4646
#[error("{0}")]
4747
Json(#[from] serde_json::Error),
48+
/// Bincode error.
49+
#[error("{0}")]
50+
Bincode(#[from] Box<bincode::ErrorKind>),
4851
/// IO error.
4952
#[error("{0}")]
5053
Io(#[from] std::io::Error),

core/tauri/src/endpoints/notification.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn send(options: NotificationOptions, config: &Config) -> crate::Result<Invo
8080

8181
#[cfg(notification_all)]
8282
pub fn is_permission_granted(config: &Config) -> crate::Result<InvokeResponse> {
83-
let settings = crate::settings::read_settings(config)?;
83+
let settings = crate::settings::read_settings(config);
8484
if let Some(allow_notification) = settings.allow_notification {
8585
Ok(allow_notification.into())
8686
} else {
@@ -90,7 +90,7 @@ pub fn is_permission_granted(config: &Config) -> crate::Result<InvokeResponse> {
9090

9191
#[cfg(notification_all)]
9292
pub fn request_permission(config: &Config) -> crate::Result<String> {
93-
let mut settings = crate::settings::read_settings(config)?;
93+
let mut settings = crate::settings::read_settings(config);
9494
if let Some(allow_notification) = settings.allow_notification {
9595
return Ok(if allow_notification {
9696
PERMISSION_GRANTED.to_string()

core/tauri/src/settings.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::{
66
api::{
7-
file::read_string,
7+
file::read_binary,
88
path::{resolve_path, BaseDirectory},
99
},
1010
Config,
@@ -27,7 +27,7 @@ pub struct Settings {
2727

2828
/// Gets the path to the settings file
2929
fn get_settings_path(config: &Config) -> crate::api::Result<PathBuf> {
30-
resolve_path(config, ".tauri-settings.json", Some(BaseDirectory::App))
30+
resolve_path(config, ".tauri-settings", Some(BaseDirectory::App))
3131
}
3232

3333
/// Write the settings to the file system.
@@ -41,19 +41,22 @@ pub(crate) fn write_settings(config: &Config, settings: Settings) -> crate::Resu
4141
File::create(settings_path)
4242
.map_err(Into::into)
4343
.and_then(|mut f| {
44-
f.write_all(serde_json::to_string(&settings)?.as_bytes())
44+
f.write_all(&bincode::serialize(&settings).map_err(crate::api::Error::Bincode)?)
4545
.map_err(Into::into)
4646
})
4747
}
4848

4949
/// Reads the settings from the file system.
50-
pub fn read_settings(config: &Config) -> crate::Result<Settings> {
51-
let settings_path = get_settings_path(config)?;
52-
if settings_path.exists() {
53-
read_string(settings_path)
54-
.and_then(|settings| serde_json::from_str(settings.as_str()).map_err(Into::into))
55-
.map_err(Into::into)
50+
pub fn read_settings(config: &Config) -> Settings {
51+
if let Ok(settings_path) = get_settings_path(config) {
52+
if settings_path.exists() {
53+
read_binary(settings_path)
54+
.and_then(|settings| bincode::deserialize(&settings).map_err(Into::into))
55+
.unwrap_or_default()
56+
} else {
57+
Settings::default()
58+
}
5659
} else {
57-
Ok(Default::default())
60+
Settings::default()
5861
}
5962
}

0 commit comments

Comments
 (0)