lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

inventory.rs (5565B)


      1 //! Closed owner-private inventory command dispatch, without host authority.
      2 use super::{
      3     CapturedPoolCorrection, CapturedPoolCreation, CommandError, check_frame, parse_object,
      4 };
      5 use alloc::string::String;
      6 use radroots_domain::{
      7     ApprovalIntentDigest, CommandContextData, CommandDigest, stock::AvailableStockCorrection,
      8 };
      9 use serde::Deserialize;
     10 
     11 /// Effect class claimed by an exact captured owner-private inventory command.
     12 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     13 pub enum InventoryCommandKind {
     14     /// Creation of a new fixed-unit pool.
     15     CreatePool,
     16     /// Positive correction of introduced stock.
     17     AddStock,
     18     /// Audited removal of physical stock.
     19     RemoveStock,
     20     /// Audited physical spoilage.
     21     SpoilStock,
     22     /// Owner-private listing option configuration, without a physical movement.
     23     MapOption,
     24 }
     25 /// Original bounded captured inventory data. Its metadata establishes no grant,
     26 /// physical stock, transaction completion or permission to change obligations.
     27 pub enum CapturedInventoryCommand {
     28     /// Original exact pool-creation profile.
     29     CreatePool(CapturedPoolCreation),
     30     /// Exact adjustment or spoilage profile.
     31     CorrectPool(CapturedPoolCorrection),
     32     /// Exact private option mapping configuration.
     33     MapOption(super::CapturedOptionMapping),
     34 }
     35 impl CapturedInventoryCommand {
     36     pub fn parse(raw: &[u8]) -> Result<Self, CommandError> {
     37         check_frame(raw)?;
     38         #[derive(Deserialize)]
     39         struct Header {
     40             #[serde(rename = "type")]
     41             kind: String,
     42         }
     43         let header: Header = parse_object(raw)?;
     44         match header.kind.as_str() {
     45             "stock.map_option" => super::CapturedOptionMapping::parse(raw).map(Self::MapOption),
     46             "stock.create_pool" => CapturedPoolCreation::parse(raw).map(Self::CreatePool),
     47             "stock.adjust" | "stock.spoil" => {
     48                 CapturedPoolCorrection::parse(raw).map(Self::CorrectPool)
     49             }
     50             _ => Err(CommandError::UnsupportedCommand),
     51         }
     52     }
     53     pub const fn context(&self) -> &CommandContextData {
     54         match self {
     55             Self::CreatePool(v) => v.context(),
     56             Self::CorrectPool(v) => v.context(),
     57             Self::MapOption(v) => v.context(),
     58         }
     59     }
     60     pub const fn digest(&self) -> CommandDigest {
     61         match self {
     62             Self::CreatePool(v) => v.digest(),
     63             Self::CorrectPool(v) => v.digest(),
     64             Self::MapOption(v) => v.digest(),
     65         }
     66     }
     67     pub const fn approval_intent_digest(&self) -> ApprovalIntentDigest {
     68         match self {
     69             Self::CreatePool(v) => v.approval_intent_digest(),
     70             Self::CorrectPool(v) => v.approval_intent_digest(),
     71             Self::MapOption(v) => v.approval_intent_digest(),
     72         }
     73     }
     74     pub fn protected_wire(&self) -> &[u8] {
     75         match self {
     76             Self::CreatePool(v) => v.protected_wire(),
     77             Self::CorrectPool(v) => v.protected_wire(),
     78             Self::MapOption(v) => v.protected_wire(),
     79         }
     80     }
     81     pub const fn kind(&self) -> InventoryCommandKind {
     82         match self {
     83             Self::CreatePool(_) => InventoryCommandKind::CreatePool,
     84             Self::MapOption(_) => InventoryCommandKind::MapOption,
     85             Self::CorrectPool(v) => match v.input().kind() {
     86                 AvailableStockCorrection::Add => InventoryCommandKind::AddStock,
     87                 AvailableStockCorrection::Remove => InventoryCommandKind::RemoveStock,
     88                 AvailableStockCorrection::Spoil => InventoryCommandKind::SpoilStock,
     89             },
     90         }
     91     }
     92     /// Referenced private pool identity, without authenticating its existence.
     93     pub const fn pool_id(&self) -> radroots_domain::PoolId {
     94         match self {
     95             Self::CreatePool(v) => v.input().id(),
     96             Self::CorrectPool(v) => v.input().pool_id(),
     97             Self::MapOption(v) => v.input().pool_id(),
     98         }
     99     }
    100     /// Borrows actual mapping configuration, without manufacturing it for other commands.
    101     pub const fn mapping_input(&self) -> Option<&super::MapOptionInput> {
    102         match self {
    103             Self::MapOption(v) => Some(v.input()),
    104             Self::CreatePool(_) | Self::CorrectPool(_) => None,
    105         }
    106     }
    107     /// Borrows actual creation data; correction commands never fabricate it.
    108     pub const fn creation_input(&self) -> Option<&super::CreatePoolInput> {
    109         match self {
    110             Self::CreatePool(v) => Some(v.input()),
    111             Self::CorrectPool(_) | Self::MapOption(_) => None,
    112         }
    113     }
    114     /// Borrows actual correction data; creation commands never fabricate it.
    115     pub const fn correction_input(&self) -> Option<&super::PoolCorrectionInput> {
    116         match self {
    117             Self::CorrectPool(v) => Some(v.input()),
    118             Self::CreatePool(_) | Self::MapOption(_) => None,
    119         }
    120     }
    121 }
    122 impl From<CapturedPoolCreation> for CapturedInventoryCommand {
    123     fn from(value: CapturedPoolCreation) -> Self {
    124         Self::CreatePool(value)
    125     }
    126 }
    127 impl From<super::CapturedOptionMapping> for CapturedInventoryCommand {
    128     fn from(value: super::CapturedOptionMapping) -> Self {
    129         Self::MapOption(value)
    130     }
    131 }
    132 impl From<CapturedPoolCorrection> for CapturedInventoryCommand {
    133     fn from(value: CapturedPoolCorrection) -> Self {
    134         Self::CorrectPool(value)
    135     }
    136 }
    137 impl core::fmt::Debug for CapturedInventoryCommand {
    138     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    139         f.write_str("CapturedInventoryCommand(<redacted>)")
    140     }
    141 }