tauri_store/store/
state.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value as Json;
use std::collections::HashMap;
use std::result::Result as StdResult;

/// Internal state of a store.
#[derive(Clone, Debug, Default)]
pub struct StoreState(pub(super) HashMap<String, Json>);

impl StoreState {
  /// Consumes the store state and returns the inner map.
  pub fn into_inner(self) -> HashMap<String, Json> {
    self.0
  }
}

impl Serialize for StoreState {
  fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
  where
    S: Serializer,
  {
    self.0.serialize(serializer)
  }
}

impl<'de> Deserialize<'de> for StoreState {
  fn deserialize<D>(deserializer: D) -> StdResult<Self, D::Error>
  where
    D: Deserializer<'de>,
  {
    type Map = HashMap<String, Json>;
    Ok(Self(Map::deserialize(deserializer)?))
  }
}

impl From<HashMap<String, Json>> for StoreState {
  fn from(map: HashMap<String, Json>) -> Self {
    Self(map)
  }
}

impl<K, V> FromIterator<(K, V)> for StoreState
where
  K: Into<String>,
  V: Into<Json>,
{
  fn from_iter<I>(iter: I) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
  {
    let inner = iter
      .into_iter()
      .map(|(k, v)| (k.into(), v.into()))
      .collect();

    Self(inner)
  }
}

impl<K, V> From<(K, V)> for StoreState
where
  K: Into<String>,
  V: Into<Json>,
{
  fn from((key, value): (K, V)) -> Self {
    Self::from_iter([(key, value)])
  }
}

impl<K, V> From<Vec<(K, V)>> for StoreState
where
  K: Into<String>,
  V: Into<Json>,
{
  fn from(pairs: Vec<(K, V)>) -> Self {
    Self::from_iter(pairs)
  }
}

impl<const N: usize, K, V> From<[(K, V); N]> for StoreState
where
  K: Into<String>,
  V: Into<Json>,
{
  fn from(pairs: [(K, V); N]) -> Self {
    Self::from_iter(pairs)
  }
}