miden_objects/address/interface.rs
1use core::fmt::{self, Display, Formatter};
2
3use crate::AddressError;
4
5/// The account interface of an [`Address`](super::Address).
6///
7/// An interface specifies the set of procedures of an account, which determines which notes it is
8/// able to receive and consume.
9///
10/// The enum is non-exhaustive so it can be extended in the future without it being a breaking
11/// change. Users are expected to match on the variants that they are able to handle and ignore the
12/// remaining ones.
13///
14/// ## Guarantees
15///
16/// An interface encodes to a `u16`, but is guaranteed to take up at most 11 of its bits. This
17/// constraint allows encoding the interface into an address efficiently.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19#[repr(u16)]
20#[non_exhaustive]
21pub enum AddressInterface {
22 /// The basic wallet interface.
23 BasicWallet = Self::BASIC_WALLET,
24}
25
26impl AddressInterface {
27 // Constants for internal use only.
28 const BASIC_WALLET: u16 = 0;
29}
30
31impl TryFrom<u16> for AddressInterface {
32 type Error = AddressError;
33
34 /// Decodes an [`AddressInterface`] from its bytes representation.
35 fn try_from(value: u16) -> Result<Self, Self::Error> {
36 match value {
37 Self::BASIC_WALLET => Ok(Self::BasicWallet),
38 other => Err(AddressError::UnknownAddressInterface(other)),
39 }
40 }
41}
42
43impl Display for AddressInterface {
44 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45 match self {
46 Self::BasicWallet => write!(f, "BasicWallet"),
47 }
48 }
49}