Skip to content

Commit

Permalink
feat: allow pretty print (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferreira-tb authored Sep 26, 2024
1 parent 9d371c5 commit 1375414
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.html linguist-detectable=false
*.js text eol=lf linguist-detectable=false
*.js linguist-detectable=false
8 changes: 8 additions & 0 deletions crates/tauri-plugin-pinia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use std::collections::HashSet;
#[derive(Default)]
pub struct Builder {
path: Option<PathBuf>,
pretty: bool,
sync_denylist: HashSet<String>,

#[cfg(feature = "unstable-async")]
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-store-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-store/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 12 additions & 2 deletions crates/tauri-store/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ pub(crate) static RESOURCE_ID: OnceLock<ResourceId> = OnceLock::new();
pub struct StoreCollection<R: Runtime> {
pub(crate) app: AppHandle<R>,
pub(crate) path: PathBuf,
pub(crate) sync_denylist: StdMutex<HashSet<String>>,

#[cfg(not(feature = "unstable-async"))]
pub(crate) stores: StdMutex<HashMap<String, Store<R>>>,
#[cfg(feature = "unstable-async")]
pub(crate) stores: TokioMutex<HashMap<String, Store<R>>>,

pub(crate) pretty: bool,
pub(crate) sync_denylist: StdMutex<HashSet<String>>,

#[cfg(feature = "unstable-async")]
pub(crate) autosave: StdMutex<Option<AbortHandle>>,
}
Expand Down Expand Up @@ -387,12 +389,13 @@ impl<R: Runtime> Drop for StoreCollection<R> {
#[derive(Debug, Default)]
pub struct StoreCollectionBuilder {
path: Option<PathBuf>,
pretty: bool,
sync_denylist: Option<HashSet<String>>,
}

impl StoreCollectionBuilder {
pub fn new() -> Self {
Self { path: None, sync_denylist: None }
Self::default()
}

#[must_use]
Expand All @@ -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<String>) -> Self {
self.sync_denylist = Some(sync_denylist);
Expand All @@ -421,6 +430,7 @@ impl StoreCollectionBuilder {
let collection = Arc::new(StoreCollection::<R> {
app: app.clone(),
path,
pretty: self.pretty,
sync_denylist: StdMutex::new(sync_denylist),

#[cfg(not(feature = "unstable-async"))]
Expand Down
19 changes: 15 additions & 4 deletions crates/tauri-store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -44,7 +45,7 @@ impl<R: Runtime> Store<R> {
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)?;

Expand All @@ -60,9 +61,8 @@ impl<R: Runtime> Store<R> {
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(())
Expand Down Expand Up @@ -201,3 +201,14 @@ fn store_path<R: Runtime>(app: &AppHandle<R>, id: &str) -> PathBuf {
.path()
.join(format!("{id}.json"))
}

fn to_bytes<T>(value: &T, pretty: bool) -> Result<Vec<u8>>
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)
}
}
4 changes: 2 additions & 2 deletions examples/pinia/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "example-pinia",
"version": "0.6.0",
"version": "0.6.1",
"type": "module",
"packageManager": "[email protected]",
"private": true,
Expand All @@ -18,4 +18,4 @@
"tauri-plugin-pinia": "workspace:*",
"vue": "^3.5.9"
}
}
}
2 changes: 1 addition & 1 deletion examples/pinia/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "example-pinia"
version = "0.6.0"
version = "0.6.1"
publish = false

[package.edition]
Expand Down
4 changes: 2 additions & 2 deletions examples/pinia/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -31,4 +31,4 @@
"icons/icon.ico"
]
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monorepo",
"version": "0.6.0",
"version": "0.6.1",
"type": "module",
"private": true,
"packageManager": "[email protected]",
Expand Down Expand Up @@ -34,4 +34,4 @@
"vite-plugin-dts": "^4.2.2",
"vue-tsc": "^2.1.6"
}
}
}

0 comments on commit 1375414

Please sign in to comment.