diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index 701f6731..26698cdd 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -37,7 +37,11 @@ pub fn run() { .plugin(tauri_plugin_os::init()) .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_shell::init()) - .plugin(tauri_plugin_store::Builder::default().build()) + .plugin( + tauri_plugin_store::Builder::default() + .register_serialize_fn("pretty-json".to_owned(), pretty_json) + .build(), + ) .setup(move |app| { #[cfg(desktop)] { @@ -159,3 +163,9 @@ pub fn run() { } }) } + +fn pretty_json( + cache: &std::collections::HashMap, +) -> Result, Box> { + Ok(serde_json::to_vec_pretty(&cache)?) +} diff --git a/examples/api/src/views/Store.svelte b/examples/api/src/views/Store.svelte index 75583a53..3d8c55b2 100644 --- a/examples/api/src/views/Store.svelte +++ b/examples/api/src/views/Store.svelte @@ -7,7 +7,7 @@ let key; let value; - let store = new LazyStore("cache.json"); + let store = new LazyStore("cache.json", { serializeFnName: "pretty-json" }); let cache = {}; async function refreshEntries() { diff --git a/plugins/store/src/lib.rs b/plugins/store/src/lib.rs index ec58a00b..0d97d493 100644 --- a/plugins/store/src/lib.rs +++ b/plugins/store/src/lib.rs @@ -254,6 +254,23 @@ impl Builder { } /// Register a serialize function to access it from the JavaScript side + /// + /// # Examples + /// + /// ``` + /// fn pretty_json( + /// cache: &std::collections::HashMap, + /// ) -> Result, Box> { + /// Ok(serde_json::to_vec_pretty(&cache)?) + /// } + /// + /// tauri::Builder::default() + /// .plugin( + /// tauri_plugin_store::Builder::default() + /// .register_serialize_fn("pretty-json".to_owned(), pretty_json) + /// .build(), + /// ) + /// ``` pub fn register_serialize_fn(mut self, name: String, serialize_fn: SerializeFn) -> Self { self.serialize_fns.insert(name, serialize_fn); self