refactor(core): Settings serialization using bincode (#1758)

This commit is contained in:
Lucas Fernandes Nogueira
2021-05-09 22:09:32 -03:00
committed by GitHub
parent 8d2e4c63c7
commit 455c550f34
5 changed files with 24 additions and 12 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
`Settings` is now serialized using `bincode`.

View File

@@ -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"

View File

@@ -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),

View File

@@ -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 {
@@ -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()

View File

@@ -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<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.
@@ -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()
}
}