Skip to main content

miden_protocol/account/account_id/
asset_callback_flag.rs

1// ASSET CALLBACK FLAG
2// ================================================================================================
3
4/// Indicates whether a faucet's assets trigger
5/// [`AssetCallbacks`](crate::asset::AssetCallbacks) when they are added to an account or note.
6///
7/// The flag is an immutable property of the issuing faucet's [`AccountId`](super::AccountId),
8/// encoded as a single bit of the ID prefix and fixed at account creation. Because every asset
9/// carries its faucet's ID prefix, the kernel can derive this capability from the asset alone
10/// without reading the faucet's account state.
11///
12/// When [`Enabled`](Self::Enabled), the kernel dispatches the faucet's callbacks whenever one of
13/// its assets is added to a vault or note. When [`Disabled`](Self::Disabled), callbacks are skipped
14/// entirely and no foreign-account read is performed.
15#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
16#[repr(u8)]
17pub enum AssetCallbackFlag {
18    #[default]
19    Disabled = Self::DISABLED,
20
21    Enabled = Self::ENABLED,
22}
23
24impl AssetCallbackFlag {
25    pub(crate) const DISABLED: u8 = 0;
26    pub(crate) const ENABLED: u8 = 1;
27
28    /// Encodes the flag as a `u8`, where [`Disabled`](Self::Disabled) is `0` and
29    /// [`Enabled`](Self::Enabled) is `1`.
30    pub const fn as_u8(&self) -> u8 {
31        *self as u8
32    }
33
34    /// Returns `true` if the flag is [`Enabled`](Self::Enabled), `false` otherwise.
35    pub const fn is_enabled(&self) -> bool {
36        matches!(self, Self::Enabled)
37    }
38}
39
40impl From<bool> for AssetCallbackFlag {
41    /// Maps `true` to [`Enabled`](Self::Enabled) and `false` to [`Disabled`](Self::Disabled).
42    fn from(enabled: bool) -> Self {
43        if enabled { Self::Enabled } else { Self::Disabled }
44    }
45}