miden_objects/account/account_id/
storage_mode.rs

1use alloc::string::String;
2use core::fmt;
3use core::str::FromStr;
4
5use crate::errors::AccountIdError;
6
7// ACCOUNT STORAGE MODE
8// ================================================================================================
9
10// This leaves room for an ENCRYPTED = 0b11.
11// This way, the storage modes where the full state is public on-chain do not have the first
12// bit set, which may be useful as a way to group the storage modes.
13pub(super) const PUBLIC: u8 = 0b00;
14pub(super) const NETWORK: u8 = 0b01;
15pub(super) const PRIVATE: u8 = 0b10;
16
17/// Describes where the state of the account is stored.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[repr(u8)]
20pub enum AccountStorageMode {
21    /// The account's full state is stored on-chain.
22    Public = PUBLIC,
23    /// The account's full state is stored on-chain. Additionally, the network monitors this account
24    /// and creates network transactions against it. It is otherwise the same as [`Self::Public`].
25    Network = NETWORK,
26    /// The account's state is stored off-chain, and only a commitment to it is stored on-chain.
27    Private = PRIVATE,
28}
29
30impl AccountStorageMode {
31    /// Returns `true` if the full state of the account is public on chain, i.e. if the modes are
32    /// [`Self::Public`] or [`Self::Network`], `false` otherwise.
33    pub fn has_public_state(&self) -> bool {
34        matches!(self, Self::Public | Self::Network)
35    }
36
37    /// Returns `true` if the storage mode is [`Self::Public`], `false` otherwise.
38    pub fn is_public(&self) -> bool {
39        matches!(self, Self::Public)
40    }
41
42    /// Returns `true` if the storage mode is [`Self::Network`], `false` otherwise.
43    pub fn is_network(&self) -> bool {
44        matches!(self, Self::Network)
45    }
46
47    /// Returns `true` if the storage mode is [`Self::Private`], `false` otherwise.
48    pub fn is_private(&self) -> bool {
49        matches!(self, Self::Private)
50    }
51}
52
53impl fmt::Display for AccountStorageMode {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            AccountStorageMode::Public => write!(f, "public"),
57            AccountStorageMode::Network => write!(f, "network"),
58            AccountStorageMode::Private => write!(f, "private"),
59        }
60    }
61}
62
63impl TryFrom<&str> for AccountStorageMode {
64    type Error = AccountIdError;
65
66    fn try_from(value: &str) -> Result<Self, AccountIdError> {
67        match value.to_lowercase().as_str() {
68            "public" => Ok(AccountStorageMode::Public),
69            "network" => Ok(AccountStorageMode::Network),
70            "private" => Ok(AccountStorageMode::Private),
71            _ => Err(AccountIdError::UnknownAccountStorageMode(value.into())),
72        }
73    }
74}
75
76impl TryFrom<String> for AccountStorageMode {
77    type Error = AccountIdError;
78
79    fn try_from(value: String) -> Result<Self, AccountIdError> {
80        AccountStorageMode::from_str(&value)
81    }
82}
83
84impl FromStr for AccountStorageMode {
85    type Err = AccountIdError;
86
87    fn from_str(input: &str) -> Result<AccountStorageMode, AccountIdError> {
88        AccountStorageMode::try_from(input)
89    }
90}
91
92#[cfg(any(feature = "testing", test))]
93impl rand::distr::Distribution<AccountStorageMode> for rand::distr::StandardUniform {
94    /// Samples a uniformly random [`AccountStorageMode`] from the given `rng`.
95    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AccountStorageMode {
96        match rng.random_range(0..3) {
97            0 => AccountStorageMode::Public,
98            1 => AccountStorageMode::Network,
99            2 => AccountStorageMode::Private,
100            _ => unreachable!("gen_range should not produce higher values"),
101        }
102    }
103}