mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
refactor(core): Settings serialization using bincode (#1758)
This commit is contained in:
committed by
GitHub
parent
8d2e4c63c7
commit
455c550f34
5
.changes/refactor-settings.md
Normal file
5
.changes/refactor-settings.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
`Settings` is now serialized using `bincode`.
|
||||
@@ -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"
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user