tauri_store/store/
options.rs

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