lib

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

captured.rs (5231B)


      1 //! Common immutable command data. Domain-specific payloads remain explicit;
      2 //! no chat identity or recipient is fabricated for a public command.
      3 use super::{CapturedChatCommand, ChatKind, CommandError, check_frame};
      4 use alloc::string::String;
      5 use core::fmt;
      6 use radroots_domain::{ApprovalIntentDigest, CommandContextData, CommandDigest};
      7 use serde::Deserialize;
      8 
      9 /// Implemented known-kind command families, without execution permission.
     10 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     11 #[non_exhaustive]
     12 pub enum CommandKind {
     13     /// Private inquiry.
     14     Inquire,
     15     /// Private reply requiring authenticated history.
     16     Reply,
     17     /// Public listing publication.
     18     PublishListing,
     19 }
     20 impl CommandKind {
     21     /// Exact closed command discriminator.
     22     pub const fn as_str(self) -> &'static str {
     23         match self {
     24             Self::Inquire => "inbox.inquire",
     25             Self::Reply => "inbox.reply",
     26             Self::PublishListing => "listing.publish",
     27         }
     28     }
     29 }
     30 
     31 /// Bounded captured data shared by authoritative admission. Parsing supplies
     32 /// no grant, durable preparation, signer permission or installation evidence.
     33 pub enum CapturedCommand {
     34     /// Original chat profile, retaining its exact established commitments.
     35     Chat(CapturedChatCommand),
     36     /// Supported complete public listing profile.
     37     #[cfg(feature = "listing")]
     38     Listing(crate::listing::CapturedListingCommand),
     39 }
     40 impl CapturedCommand {
     41     /// Selects an implemented family only after bounded JSON frame admission.
     42     /// Each selected parser independently validates its entire closed shape.
     43     pub fn parse(raw: &[u8]) -> Result<Self, CommandError> {
     44         check_frame(raw)?;
     45         #[derive(Deserialize)]
     46         struct Header {
     47             #[serde(rename = "type")]
     48             kind: String,
     49         }
     50         let header: Header = super::parse_object(raw)?;
     51         if header.kind.len() > 80 {
     52             return Err(CommandError::LimitExceeded);
     53         }
     54         match header.kind.as_str() {
     55             "inbox.inquire" | "inbox.reply" => CapturedChatCommand::parse(raw).map(Self::Chat),
     56             #[cfg(feature = "listing")]
     57             "listing.publish" => {
     58                 crate::listing::CapturedListingCommand::parse(raw).map(Self::Listing)
     59             }
     60             _ => Err(CommandError::UnsupportedCommand),
     61         }
     62     }
     63     /// Original complete context data, requiring separate authentication.
     64     pub const fn context(&self) -> &CommandContextData {
     65         match self {
     66             Self::Chat(v) => v.context(),
     67             #[cfg(feature = "listing")]
     68             Self::Listing(v) => v.context(),
     69         }
     70     }
     71     /// Original semantic commitment, excluding diagnostic correlation.
     72     pub const fn digest(&self) -> CommandDigest {
     73         match self {
     74             Self::Chat(v) => v.digest(),
     75             #[cfg(feature = "listing")]
     76             Self::Listing(v) => v.digest(),
     77         }
     78     }
     79     /// Exact approval intent; its bytes authenticate no consent.
     80     pub const fn approval_intent_digest(&self) -> ApprovalIntentDigest {
     81         match self {
     82             Self::Chat(v) => v.approval_intent_digest(),
     83             #[cfg(feature = "listing")]
     84             Self::Listing(v) => v.approval_intent_digest(),
     85         }
     86     }
     87     /// Exact supported command family.
     88     pub const fn kind(&self) -> CommandKind {
     89         match self {
     90             Self::Chat(v) => match v.kind() {
     91                 ChatKind::Inquire => CommandKind::Inquire,
     92                 ChatKind::Reply => CommandKind::Reply,
     93             },
     94             #[cfg(feature = "listing")]
     95             Self::Listing(_) => CommandKind::PublishListing,
     96         }
     97     }
     98     /// Borrows retained canonical bytes for protected handling.
     99     pub fn protected_wire(&self) -> &[u8] {
    100         match self {
    101             Self::Chat(v) => v.protected_wire(),
    102             #[cfg(feature = "listing")]
    103             Self::Listing(v) => v.protected_wire(),
    104         }
    105     }
    106     /// Borrows actual chat data, never a synthesized peer or empty message.
    107     pub const fn chat(&self) -> Option<&CapturedChatCommand> {
    108         match self {
    109             Self::Chat(v) => Some(v),
    110             #[cfg(feature = "listing")]
    111             Self::Listing(_) => None,
    112         }
    113     }
    114     /// Borrows actual supported public listing data.
    115     #[cfg(feature = "listing")]
    116     pub const fn listing(&self) -> Option<&crate::listing::CapturedListingCommand> {
    117         match self {
    118             Self::Chat(_) => None,
    119             Self::Listing(v) => Some(v),
    120         }
    121     }
    122     /// Whether this capture names history the fresh outbox cannot invent.
    123     pub fn requires_history(&self) -> bool {
    124         self.context().expected().is_some() || self.chat().is_some_and(|v| v.parent().is_some())
    125     }
    126 }
    127 impl From<CapturedChatCommand> for CapturedCommand {
    128     fn from(v: CapturedChatCommand) -> Self {
    129         Self::Chat(v)
    130     }
    131 }
    132 #[cfg(feature = "listing")]
    133 impl From<crate::listing::CapturedListingCommand> for CapturedCommand {
    134     fn from(v: crate::listing::CapturedListingCommand) -> Self {
    135         Self::Listing(v)
    136     }
    137 }
    138 impl fmt::Debug for CapturedCommand {
    139     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    140         f.write_str("CapturedCommand(<redacted>)")
    141     }
    142 }