From 455c550f347fe09581b4440bb868cacbbbbe2ad2 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sun, 9 May 2021 22:09:32 -0300 Subject: [PATCH] refactor(core): `Settings` serialization using `bincode` (#1758) --- .changes/refactor-settings.md | 5 +++++ core/tauri/Cargo.toml | 1 + core/tauri/src/api/error.rs | 3 +++ core/tauri/src/endpoints/notification.rs | 4 ++-- core/tauri/src/settings.rs | 23 +++++++++++++---------- 5 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 .changes/refactor-settings.md diff --git a/.changes/refactor-settings.md b/.changes/refactor-settings.md new file mode 100644 index 000000000..58a4776ee --- /dev/null +++ b/.changes/refactor-settings.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +`Settings` is now serialized using `bincode`. diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 3530db125..3c715feca 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -50,6 +50,7 @@ os_pipe = "0.9" minisign-verify = "0.1.8" image = "0.23" state = "0.4" +bincode = "1.3" [build-dependencies] cfg_aliases = "0.1.1" diff --git a/core/tauri/src/api/error.rs b/core/tauri/src/api/error.rs index 424822b58..ba883b166 100644 --- a/core/tauri/src/api/error.rs +++ b/core/tauri/src/api/error.rs @@ -45,6 +45,9 @@ pub enum Error { /// JSON error. #[error("{0}")] Json(#[from] serde_json::Error), + /// Bincode error. + #[error("{0}")] + Bincode(#[from] Box), /// IO error. #[error("{0}")] Io(#[from] std::io::Error), diff --git a/core/tauri/src/endpoints/notification.rs b/core/tauri/src/endpoints/notification.rs index 7499b1fa1..6ae27482c 100644 --- a/core/tauri/src/endpoints/notification.rs +++ b/core/tauri/src/endpoints/notification.rs @@ -80,7 +80,7 @@ pub fn send(options: NotificationOptions, config: &Config) -> crate::Result crate::Result { - let settings = crate::settings::read_settings(config)?; + let settings = crate::settings::read_settings(config); if let Some(allow_notification) = settings.allow_notification { Ok(allow_notification.into()) } else { @@ -90,7 +90,7 @@ pub fn is_permission_granted(config: &Config) -> crate::Result { #[cfg(notification_all)] pub fn request_permission(config: &Config) -> crate::Result { - let mut settings = crate::settings::read_settings(config)?; + let mut settings = crate::settings::read_settings(config); if let Some(allow_notification) = settings.allow_notification { return Ok(if allow_notification { PERMISSION_GRANTED.to_string() diff --git a/core/tauri/src/settings.rs b/core/tauri/src/settings.rs index e7835e68a..c1d72b1cd 100644 --- a/core/tauri/src/settings.rs +++ b/core/tauri/src/settings.rs @@ -4,7 +4,7 @@ use crate::{ api::{ - file::read_string, + file::read_binary, path::{resolve_path, BaseDirectory}, }, Config, @@ -27,7 +27,7 @@ pub struct Settings { /// Gets the path to the settings file fn get_settings_path(config: &Config) -> crate::api::Result { - resolve_path(config, ".tauri-settings.json", Some(BaseDirectory::App)) + resolve_path(config, ".tauri-settings", Some(BaseDirectory::App)) } /// Write the settings to the file system. @@ -41,19 +41,22 @@ pub(crate) fn write_settings(config: &Config, settings: Settings) -> crate::Resu File::create(settings_path) .map_err(Into::into) .and_then(|mut f| { - f.write_all(serde_json::to_string(&settings)?.as_bytes()) + f.write_all(&bincode::serialize(&settings).map_err(crate::api::Error::Bincode)?) .map_err(Into::into) }) } /// Reads the settings from the file system. -pub fn read_settings(config: &Config) -> crate::Result { - let settings_path = get_settings_path(config)?; - if settings_path.exists() { - read_string(settings_path) - .and_then(|settings| serde_json::from_str(settings.as_str()).map_err(Into::into)) - .map_err(Into::into) +pub fn read_settings(config: &Config) -> Settings { + if let Ok(settings_path) = get_settings_path(config) { + if settings_path.exists() { + read_binary(settings_path) + .and_then(|settings| bincode::deserialize(&settings).map_err(Into::into)) + .unwrap_or_default() + } else { + Settings::default() + } } else { - Ok(Default::default()) + Settings::default() } }