From 1375414763bf98332c102a7f65f7f512963d8515 Mon Sep 17 00:00:00 2001 From: Andrew Ferreira Date: Thu, 26 Sep 2024 17:34:09 -0300 Subject: [PATCH] feat: allow pretty print (#25) --- .gitattributes | 2 +- crates/tauri-plugin-pinia/src/lib.rs | 8 ++++++++ crates/tauri-store-macros/Cargo.toml | 2 +- crates/tauri-store/Cargo.toml | 2 +- crates/tauri-store/src/collection.rs | 14 ++++++++++++-- crates/tauri-store/src/store.rs | 19 +++++++++++++++---- examples/pinia/package.json | 4 ++-- examples/pinia/src-tauri/Cargo.toml | 2 +- examples/pinia/src-tauri/tauri.conf.json | 4 ++-- package.json | 4 ++-- 10 files changed, 45 insertions(+), 16 deletions(-) diff --git a/.gitattributes b/.gitattributes index a34b9b3d..e99d3542 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ *.html linguist-detectable=false -*.js text eol=lf linguist-detectable=false \ No newline at end of file +*.js linguist-detectable=false \ No newline at end of file diff --git a/crates/tauri-plugin-pinia/src/lib.rs b/crates/tauri-plugin-pinia/src/lib.rs index fcf739c4..069ccb32 100644 --- a/crates/tauri-plugin-pinia/src/lib.rs +++ b/crates/tauri-plugin-pinia/src/lib.rs @@ -46,6 +46,7 @@ use std::collections::HashSet; #[derive(Default)] pub struct Builder { path: Option, + pretty: bool, sync_denylist: HashSet, #[cfg(feature = "unstable-async")] @@ -65,6 +66,13 @@ impl Builder { self } + /// Sets whether the store files should be pretty printed. + #[must_use] + pub fn pretty(mut self, yes: bool) -> Self { + self.pretty = yes; + self + } + /// Sets a list of stores that should not be synchronized across windows. #[must_use] pub fn sync_denylist(mut self, denylist: &[&str]) -> Self { diff --git a/crates/tauri-store-macros/Cargo.toml b/crates/tauri-store-macros/Cargo.toml index bbab89bb..e82ff169 100644 --- a/crates/tauri-store-macros/Cargo.toml +++ b/crates/tauri-store-macros/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tauri-store-macros" description = "Persistent stores for Tauri" -version = "0.1.0" +version = "0.1.1" documentation = "https://docs.rs/tauri-store" homepage = "https://tb.dev.br/tauri-store" repository = "https://github.com/ferreira-tb/tauri-store" diff --git a/crates/tauri-store/Cargo.toml b/crates/tauri-store/Cargo.toml index 3f92d96e..ae693b7b 100644 --- a/crates/tauri-store/Cargo.toml +++ b/crates/tauri-store/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tauri-store" description = "Persistent stores for Tauri" -version = "0.1.0" +version = "0.1.1" documentation = "https://docs.rs/tauri-store" homepage = "https://tb.dev.br/tauri-store" repository = "https://github.com/ferreira-tb/tauri-store" diff --git a/crates/tauri-store/src/collection.rs b/crates/tauri-store/src/collection.rs index b9d21f62..ae37590f 100644 --- a/crates/tauri-store/src/collection.rs +++ b/crates/tauri-store/src/collection.rs @@ -29,13 +29,15 @@ pub(crate) static RESOURCE_ID: OnceLock = OnceLock::new(); pub struct StoreCollection { pub(crate) app: AppHandle, pub(crate) path: PathBuf, - pub(crate) sync_denylist: StdMutex>, #[cfg(not(feature = "unstable-async"))] pub(crate) stores: StdMutex>>, #[cfg(feature = "unstable-async")] pub(crate) stores: TokioMutex>>, + pub(crate) pretty: bool, + pub(crate) sync_denylist: StdMutex>, + #[cfg(feature = "unstable-async")] pub(crate) autosave: StdMutex>, } @@ -387,12 +389,13 @@ impl Drop for StoreCollection { #[derive(Debug, Default)] pub struct StoreCollectionBuilder { path: Option, + pretty: bool, sync_denylist: Option>, } impl StoreCollectionBuilder { pub fn new() -> Self { - Self { path: None, sync_denylist: None } + Self::default() } #[must_use] @@ -401,6 +404,12 @@ impl StoreCollectionBuilder { self } + #[must_use] + pub fn pretty(mut self, yes: bool) -> Self { + self.pretty = yes; + self + } + #[must_use] pub fn sync_denylist(mut self, sync_denylist: HashSet) -> Self { self.sync_denylist = Some(sync_denylist); @@ -421,6 +430,7 @@ impl StoreCollectionBuilder { let collection = Arc::new(StoreCollection:: { app: app.clone(), path, + pretty: self.pretty, sync_denylist: StdMutex::new(sync_denylist), #[cfg(not(feature = "unstable-async"))] diff --git a/crates/tauri-store/src/store.rs b/crates/tauri-store/src/store.rs index 0134bfe3..d63cd29c 100644 --- a/crates/tauri-store/src/store.rs +++ b/crates/tauri-store/src/store.rs @@ -3,6 +3,7 @@ use crate::event::{Payload, STORE_UPDATED_EVENT}; use crate::io_err; use crate::manager::ManagerExt; use serde::de::DeserializeOwned; +use serde::Serialize; use serde_json::Value as Json; use std::path::PathBuf; use std::{fmt, io}; @@ -44,7 +45,7 @@ impl Store { let collection = self.app.store_collection(); fs::create_dir_all(collection.path())?; - let bytes = serde_json::to_vec(&self.state)?; + let bytes = to_bytes(&self.state, collection.pretty)?; let mut file = File::create(self.path())?; file.write_all(&bytes)?; @@ -60,9 +61,8 @@ impl Store { let collection = self.app.store_collection(); fs::create_dir_all(collection.path()).await?; - let path = store_path(&self.app, &self.id); - let bytes = serde_json::to_vec(&self.state)?; - let mut file = File::create(path).await?; + let bytes = to_bytes(&self.state, collection.pretty)?; + let mut file = File::create(self.path()).await?; file.write_all(&bytes).await?; Ok(()) @@ -201,3 +201,14 @@ fn store_path(app: &AppHandle, id: &str) -> PathBuf { .path() .join(format!("{id}.json")) } + +fn to_bytes(value: &T, pretty: bool) -> Result> +where + T: ?Sized + Serialize, +{ + if pretty { + serde_json::to_vec_pretty(value).map_err(Into::into) + } else { + serde_json::to_vec(value).map_err(Into::into) + } +} diff --git a/examples/pinia/package.json b/examples/pinia/package.json index 294e0c94..522c1517 100644 --- a/examples/pinia/package.json +++ b/examples/pinia/package.json @@ -1,6 +1,6 @@ { "name": "example-pinia", - "version": "0.6.0", + "version": "0.6.1", "type": "module", "packageManager": "pnpm@9.11.0", "private": true, @@ -18,4 +18,4 @@ "tauri-plugin-pinia": "workspace:*", "vue": "^3.5.9" } -} +} \ No newline at end of file diff --git a/examples/pinia/src-tauri/Cargo.toml b/examples/pinia/src-tauri/Cargo.toml index 8be4ee14..ad80eb0f 100644 --- a/examples/pinia/src-tauri/Cargo.toml +++ b/examples/pinia/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "example-pinia" -version = "0.6.0" +version = "0.6.1" publish = false [package.edition] diff --git a/examples/pinia/src-tauri/tauri.conf.json b/examples/pinia/src-tauri/tauri.conf.json index d5164a89..6b732ec4 100644 --- a/examples/pinia/src-tauri/tauri.conf.json +++ b/examples/pinia/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "example-pinia", - "version": "0.6.0", + "version": "0.6.1", "identifier": "tsukilabs.example.pinia", "build": { "beforeDevCommand": "pnpm run -F example-pinia vite:dev", @@ -31,4 +31,4 @@ "icons/icon.ico" ] } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 4acd50e6..a798664a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "monorepo", - "version": "0.6.0", + "version": "0.6.1", "type": "module", "private": true, "packageManager": "pnpm@9.11.0", @@ -34,4 +34,4 @@ "vite-plugin-dts": "^4.2.2", "vue-tsc": "^2.1.6" } -} +} \ No newline at end of file