tauri_store/store/
options.rs

1use super::save::SaveStrategy;
2use super::Store;
3use crate::CollectionMarker;
4use serde::{Deserialize, Serialize};
5use tauri::Runtime;
6
7/// Options to configure the store behavior.
8#[non_exhaustive]
9#[derive(Clone, Debug, Default, Deserialize, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct StoreOptions {
12  pub save_on_exit: Option<bool>,
13  pub save_on_change: Option<bool>,
14  pub save_strategy: Option<SaveStrategy>,
15}
16
17impl<R, C> From<&Store<R, C>> for StoreOptions
18where
19  R: Runtime,
20  C: CollectionMarker,
21{
22  fn from(store: &Store<R, C>) -> Self {
23    Self {
24      save_on_exit: Some(store.save_on_exit),
25      save_on_change: Some(store.save_on_change),
26      save_strategy: store.save_strategy,
27    }
28  }
29}
30
31#[allow(clippy::needless_pass_by_value)]
32pub(super) fn set_options<R, C>(store: &mut Store<R, C>, options: StoreOptions)
33where
34  R: Runtime,
35  C: CollectionMarker,
36{
37  if let Some(enabled) = options.save_on_exit {
38    store.save_on_exit = enabled;
39  }
40
41  if let Some(enabled) = options.save_on_change {
42    store.save_on_change = enabled;
43  }
44
45  if let Some(strategy) = options.save_strategy {
46    store.set_save_strategy(strategy);
47  }
48}