miden_objects/account/account_id/
storage_mode.rs

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