Skip to content

Commit

Permalink
refactor(core): Settings serialization using bincode (#1758)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 10, 2021
1 parent 8d2e4c6 commit 455c550
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/refactor-settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

`Settings` is now serialized using `bincode`.
1 change: 1 addition & 0 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum Error {
/// JSON error.
#[error("{0}")]
Json(#[from] serde_json::Error),
/// Bincode error.
#[error("{0}")]
Bincode(#[from] Box<bincode::ErrorKind>),
/// IO error.
#[error("{0}")]
Io(#[from] std::io::Error),
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/endpoints/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn send(options: NotificationOptions, config: &Config) -> crate::Result<Invo

#[cfg(notification_all)]
pub fn is_permission_granted(config: &Config) -> crate::Result<InvokeResponse> {
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 {
Expand All @@ -90,7 +90,7 @@ pub fn is_permission_granted(config: &Config) -> crate::Result<InvokeResponse> {

#[cfg(notification_all)]
pub fn request_permission(config: &Config) -> crate::Result<String> {
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()
Expand Down
23 changes: 13 additions & 10 deletions core/tauri/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::{
api::{
file::read_string,
file::read_binary,
path::{resolve_path, BaseDirectory},
},
Config,
Expand All @@ -27,7 +27,7 @@ pub struct Settings {

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

/// Write the settings to the file system.
Expand All @@ -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<Settings> {
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()
}
}

0 comments on commit 455c550

Please sign in to comment.