tauri_store/store/
options.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use super::save::SaveStrategy;
use super::Store;
use serde::{Deserialize, Serialize};
use tauri::Runtime;

/// Options to configure the store behavior.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StoreOptions {
  pub save_on_exit: Option<bool>,
  pub save_on_change: Option<bool>,
  pub save_strategy: Option<SaveStrategy>,
}

impl<R: Runtime> From<&Store<R>> for StoreOptions {
  fn from(store: &Store<R>) -> Self {
    Self {
      save_on_exit: Some(store.save_on_exit),
      save_on_change: Some(store.save_on_change),
      save_strategy: store.save_strategy,
    }
  }
}

#[expect(
  clippy::needless_pass_by_value,
  reason = "We are just anticipating the need for it."
)]
pub(super) fn set_options<R>(store: &mut Store<R>, options: StoreOptions)
where
  R: Runtime,
{
  if let Some(enabled) = options.save_on_exit {
    store.save_on_exit = enabled;
  }

  if let Some(enabled) = options.save_on_change {
    store.save_on_change = enabled;
  }

  if let Some(strategy) = options.save_strategy {
    store.set_save_strategy(strategy);
  }
}