Skip to main content

miden_protocol/account/delta/
delta_op.rs

1use crate::errors::AssetError;
2
3/// Describes whether an asset was added or removed in an
4/// [`AccountVaultDelta`](crate::account::AccountVaultDelta).
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[repr(u8)]
7pub enum AssetDeltaOperation {
8    Add = Self::ADD,
9    Remove = Self::REMOVE,
10}
11
12impl AssetDeltaOperation {
13    // The encoding starts at 1 to leave 0 to encode a possible default `None` operation ("nothing
14    // has changed").
15    const ADD: u8 = 1;
16    const REMOVE: u8 = 2;
17
18    /// Encodes the delta operation as a `u8`.
19    pub const fn as_u8(&self) -> u8 {
20        *self as u8
21    }
22}
23
24impl TryFrom<u8> for AssetDeltaOperation {
25    type Error = AssetError;
26
27    /// Decodes a delta operation from a `u8`.
28    ///
29    /// # Errors
30    ///
31    /// Returns an error if the value is not a valid delta operation.
32    fn try_from(value: u8) -> Result<Self, Self::Error> {
33        match value {
34            Self::ADD => Ok(Self::Add),
35            Self::REMOVE => Ok(Self::Remove),
36            _ => Err(AssetError::UnknownAssetDeltaOperation(value)),
37        }
38    }
39}