miden_objects/account/account_id/
storage_mode.rs1use alloc::string::String;
2use core::{fmt, str::FromStr};
3
4use crate::errors::AccountIdError;
5
6pub(super) const PUBLIC: u8 = 0b00;
10pub(super) const PRIVATE: u8 = 0b10;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[repr(u8)]
15pub enum AccountStorageMode {
16 Public = PUBLIC,
18 Private = PRIVATE,
20}
21
22impl fmt::Display for AccountStorageMode {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 AccountStorageMode::Public => write!(f, "public"),
26 AccountStorageMode::Private => write!(f, "private"),
27 }
28 }
29}
30
31impl TryFrom<&str> for AccountStorageMode {
32 type Error = AccountIdError;
33
34 fn try_from(value: &str) -> Result<Self, AccountIdError> {
35 match value.to_lowercase().as_str() {
36 "public" => Ok(AccountStorageMode::Public),
37 "private" => Ok(AccountStorageMode::Private),
38 _ => Err(AccountIdError::UnknownAccountStorageMode(value.into())),
39 }
40 }
41}
42
43impl TryFrom<String> for AccountStorageMode {
44 type Error = AccountIdError;
45
46 fn try_from(value: String) -> Result<Self, AccountIdError> {
47 AccountStorageMode::from_str(&value)
48 }
49}
50
51impl FromStr for AccountStorageMode {
52 type Err = AccountIdError;
53
54 fn from_str(input: &str) -> Result<AccountStorageMode, AccountIdError> {
55 AccountStorageMode::try_from(input)
56 }
57}
58
59#[cfg(any(feature = "testing", test))]
60impl rand::distr::Distribution<AccountStorageMode> for rand::distr::StandardUniform {
61 fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AccountStorageMode {
63 match rng.random_range(0..2) {
64 0 => AccountStorageMode::Public,
65 1 => AccountStorageMode::Private,
66 _ => unreachable!("gen_range should not produce higher values"),
67 }
68 }
69}