rusnap_api/
state.rs

1use serde::{Deserialize, Serialize};
2
3use crate::Result;
4
5use super::request;
6
7#[derive(Debug, Serialize, Deserialize)]
8struct ManageState<D> {
9    operation: &'static str,
10    #[serde(rename = "newState")]
11    state: Option<D>,
12}
13
14#[derive(Debug, Serialize, Deserialize)]
15struct ManageStateBare {
16    operation: &'static str,
17}
18
19/// Update state.
20///
21/// This function will store data into disk.
22///
23/// Snap Document: [snap_manageState](https://docs.metamask.io/snaps/reference/rpc-api/#snap_managestate)
24pub async fn state_update<D>(state: D) -> Result<()>
25where
26    D: Serialize,
27{
28    let arg = ManageState {
29        operation: "update",
30        state: Some(state),
31    };
32
33    request("snap_manageState", arg).await?;
34
35    Ok(())
36}
37
38/// Clear state
39///
40/// This function will clear data in disk.
41///
42/// Snap Document: [snap_manageState](https://docs.metamask.io/snaps/reference/rpc-api/#snap_managestate)
43pub async fn state_clear() -> Result<()> {
44    request("snap_manageState", ManageStateBare { operation: "clear" }).await?;
45
46    Ok(())
47}
48
49/// Get state
50///
51/// This function will get data in disk.
52///
53/// Snap Document: [snap_manageState](https://docs.metamask.io/snaps/reference/rpc-api/#snap_managestate)
54pub async fn state_get<D>() -> Result<D>
55where
56    D: for<'de> Deserialize<'de>,
57{
58    Ok(request("snap_manageState", ManageStateBare { operation: "get" }).await?)
59}