Skip to content

Commit

Permalink
fix doctests, further simplification of api
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 1, 2024
1 parent 84351a3 commit 5aaa591
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
3 changes: 3 additions & 0 deletions plugins/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ tokio = { version = "1", features = ["sync", "time", "macros"] }

[target.'cfg(target_os = "ios")'.dependencies]
tauri = { workspace = true, features = ["wry"] }

[dev-dependencies]
tauri = { workspace = true, features = ["wry"] }
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use std::time::Duration;

use serde_json::json;
use tauri_plugin_store::{Builder, StoreExt};
use tauri_plugin_store::StoreExt;

mod app;
use app::settings::AppSettings;

fn main() {
tauri::Builder::default()
.plugin(Builder::new().build())
.plugin(tauri_plugin_store::Builder::new().build())
.setup(|app| {
// Init store and load it from disk
let store = app
Expand Down
10 changes: 4 additions & 6 deletions plugins/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ pub trait StoreExt<R: Runtime> {

impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn store(&self, path: impl AsRef<Path>) -> Store<R> {
StoreBuilder::new(self.app_handle().clone(), path).build()
StoreBuilder::new(self.app_handle(), path).build()
}

fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R> {
StoreBuilder::new(self.app_handle().clone(), path)
StoreBuilder::new(self.app_handle(), path)
}
}

Expand Down Expand Up @@ -246,12 +246,10 @@ impl<R: Runtime> Builder<R> {
/// # Examples
///
/// ```
/// use tauri_plugin_store::{StoreBuilder, Builder};
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
/// app.handle().plugin(Builder::default().build());
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build();
/// Ok(())
/// });
/// ```
Expand Down
102 changes: 52 additions & 50 deletions plugins/store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use tauri_plugin_store::StoreBuilder;
///
/// let builder = StoreBuilder::<tauri::Wry>::new("store.bin");
///
/// # Ok(())
/// # }
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let builder = tauri_plugin_store::StoreBuilder::new(app, "store.bin");
/// Ok(())
/// });
/// ```
pub fn new<P: AsRef<Path>>(app: AppHandle<R>, path: P) -> Self {
pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
Self {
app,
app: manager.app_handle().clone(),
// Since Store.path is only exposed to the user in emit calls we may as well simplify it here already.
path: dunce::simplified(path.as_ref()).to_path_buf(),
defaults: None,
Expand All @@ -77,19 +76,18 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use tauri_plugin_store::StoreBuilder;
/// use std::collections::HashMap;
///
/// let mut defaults = HashMap::new();
///
/// defaults.insert("foo".to_string(), "bar".into());
///
/// let builder = StoreBuilder::<tauri::Wry>::new("store.bin")
/// .defaults(defaults);
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let mut defaults = std::collections::HashMap::new();
/// defaults.insert("foo".to_string(), "bar".into());
///
/// # Ok(())
/// # }
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .defaults(defaults)
/// .build();
/// Ok(())
/// });
/// ```
pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self {
self.cache.clone_from(&defaults);
self.defaults = Some(defaults);
Expand All @@ -100,14 +98,15 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use tauri_plugin_store::StoreBuilder;
///
/// let builder = StoreBuilder::<tauri::Wry>::new("store.bin")
/// .default("foo".to_string(), "bar");
///
/// # Ok(())
/// # }
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .default("foo".to_string(), "bar")
/// .build();
/// Ok(())
/// });
/// ```
pub fn default(mut self, key: impl Into<String>, value: impl Into<JsonValue>) -> Self {
let key = key.into();
let value = value.into();
Expand All @@ -122,14 +121,15 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use tauri_plugin_store::StoreBuilder;
///
/// let builder = StoreBuilder::<tauri::Wry>::new("store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into));
///
/// # Ok(())
/// # }
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
/// .build();
/// Ok(())
/// });
/// ```
pub fn serialize(mut self, serialize: SerializeFn) -> Self {
self.serialize = serialize;
self
Expand All @@ -139,14 +139,15 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use tauri_plugin_store::StoreBuilder;
///
/// let builder = StoreBuilder::<tauri::Wry>::new("store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into));
///
/// # Ok(())
/// # }
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
/// .build();
/// Ok(())
/// });
/// ```
pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self {
self.deserialize = deserialize;
self
Expand All @@ -158,14 +159,14 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// # Examples
/// ```
/// use tauri_plugin_store::{Builder, StoreBuilder};
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = StoreBuilder::new("store.json")
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .auto_save(std::time::Duration::from_millis(100))
/// .build(app.handle().clone());
/// app.handle().plugin(Builder::default().store(store).build())
/// .build();
/// Ok(())
/// });
/// ```
pub fn auto_save(mut self, debounce_duration: Duration) -> Self {
Expand All @@ -178,8 +179,9 @@ impl<R: Runtime> StoreBuilder<R> {
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new("store.json").build(app.handle().clone());
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build();
/// Ok(())
/// });
/// ```
Expand Down

0 comments on commit 5aaa591

Please sign in to comment.