/ src / utils / store.rs
store.rs
 1  use matrix_sdk::Client;
 2  use serde::{Serialize, de::DeserializeOwned};
 3  
 4  #[derive(Clone)]
 5  pub struct Store {
 6      client: Client,
 7  }
 8  
 9  impl Store {
10      pub fn new(client: Client) -> Self {
11          Self { client }
12      }
13  
14      pub async fn get<T: DeserializeOwned>(&self, key: &[u8]) -> anyhow::Result<Option<T>> {
15          Ok(self
16              .client
17              .state_store()
18              .get_custom_value(key)
19              .await?
20              .and_then(|x| rmp_serde::from_slice(&x).ok()))
21      }
22  
23      pub async fn set<T: Serialize>(&self, key: &[u8], value: &T) -> anyhow::Result<()> {
24          self.client
25              .state_store()
26              .set_custom_value_no_read(key, rmp_serde::to_vec(value)?)
27              .await?;
28          Ok(())
29      }
30  }