stock_commitment.rs (4667B)
1 //! Closed common capture for the one stock commitment execution pipeline. 2 use super::{ 3 AllocationSelection, CapturedOrderAcceptance, CapturedTermsInstallation, CommandError, 4 check_frame, parse_object, 5 }; 6 use alloc::string::String; 7 use core::fmt; 8 use radroots_domain::{ 9 ApprovalIntentDigest, CommandContextData, CommandDigest, RecordId, TermsDigest, 10 }; 11 use serde::Deserialize; 12 13 /// Exact known stock commitment commands. These are captured data, never 14 /// execution, signing, consent, installed-state or allocation capabilities. 15 pub enum CapturedStockCommitment { 16 /// Initial buyer-request acceptance. 17 Acceptance(CapturedOrderAcceptance), 18 /// Installation of exact mutually authorized complete proposed terms. 19 TermsInstallation(CapturedTermsInstallation), 20 } 21 impl From<CapturedOrderAcceptance> for CapturedStockCommitment { 22 fn from(value: CapturedOrderAcceptance) -> Self { 23 Self::Acceptance(value) 24 } 25 } 26 impl From<CapturedTermsInstallation> for CapturedStockCommitment { 27 fn from(value: CapturedTermsInstallation) -> Self { 28 Self::TermsInstallation(value) 29 } 30 } 31 impl CapturedStockCommitment { 32 /// Dispatches only to the corresponding exact bounded closed parser. 33 pub fn parse(raw: &[u8]) -> Result<Self, CommandError> { 34 check_frame(raw)?; 35 #[derive(Deserialize)] 36 struct Probe { 37 #[serde(rename = "type")] 38 kind: String, 39 } 40 let probe: Probe = parse_object(raw)?; 41 match probe.kind.as_str() { 42 "orders.accept" => CapturedOrderAcceptance::parse(raw).map(Self::Acceptance), 43 "orders.install_terms" => { 44 CapturedTermsInstallation::parse(raw).map(Self::TermsInstallation) 45 } 46 _ => Err(CommandError::UnsupportedCommand), 47 } 48 } 49 /// Exact command scope; not proof that its approval is genuine/current. 50 pub const fn command_name(&self) -> &'static str { 51 match self { 52 Self::Acceptance(_) => "orders.accept", 53 Self::TermsInstallation(_) => "orders.install_terms", 54 } 55 } 56 /// Captured context data; current authority must be independently checked. 57 pub const fn context(&self) -> &CommandContextData { 58 match self { 59 Self::Acceptance(value) => value.context(), 60 Self::TermsInstallation(value) => value.context(), 61 } 62 } 63 /// Exact normalized protected command bytes. 64 pub fn protected_wire(&self) -> &[u8] { 65 match self { 66 Self::Acceptance(value) => value.protected_wire(), 67 Self::TermsInstallation(value) => value.protected_wire(), 68 } 69 } 70 /// Command identity bound to its exact semantic input and approval. 71 pub const fn digest(&self) -> CommandDigest { 72 match self { 73 Self::Acceptance(value) => value.digest(), 74 Self::TermsInstallation(value) => value.digest(), 75 } 76 } 77 /// Displayed intent identity before approval and observer correlation. 78 pub const fn approval_intent_digest(&self) -> ApprovalIntentDigest { 79 match self { 80 Self::Acceptance(value) => value.approval_intent_digest(), 81 Self::TermsInstallation(value) => value.approval_intent_digest(), 82 } 83 } 84 /// Exact buyer request or terms authorization selected by this command. 85 pub const fn authorization(&self) -> RecordId { 86 match self { 87 Self::Acceptance(value) => value.input().request(), 88 Self::TermsInstallation(value) => value.input().authorization(), 89 } 90 } 91 /// Exact proposal when the command installs mutually authorized terms. 92 pub const fn proposal(&self) -> Option<RecordId> { 93 match self { 94 Self::Acceptance(_) => None, 95 Self::TermsInstallation(value) => Some(value.input().proposal()), 96 } 97 } 98 /// Expected complete terms digest; source recomputation belongs to execution. 99 pub const fn terms_digest(&self) -> TermsDigest { 100 match self { 101 Self::Acceptance(value) => value.input().terms_digest(), 102 Self::TermsInstallation(value) => value.input().terms_digest(), 103 } 104 } 105 /// Bounded allocation selection data for native planning. 106 pub const fn allocation(&self) -> &AllocationSelection { 107 match self { 108 Self::Acceptance(value) => value.input().allocation(), 109 Self::TermsInstallation(value) => value.input().allocation(), 110 } 111 } 112 } 113 impl fmt::Debug for CapturedStockCommitment { 114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 115 f.write_str("CapturedStockCommitment(<redacted>)") 116 } 117 }