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