lib

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

fulfillment.rs (2874B)


      1 use crate::{Window, listing::FulfillmentMode};
      2 use alloc::string::String;
      3 use core::fmt;
      4 use zeroize::Zeroizing;
      5 
      6 /// Explicit private fulfillment data, without agreement or logistics authority.
      7 /// Address, instructions and contact are zeroizing owned text; ordinary debug
      8 /// and errors disclose none of it. Wire encoding belongs to private protocols.
      9 #[derive(Clone, Eq, PartialEq)]
     10 pub struct PrivateFulfillment {
     11     mode: FulfillmentMode,
     12     window: Window,
     13     address: Zeroizing<String>,
     14     instructions: Zeroizing<String>,
     15     contact: Option<Zeroizing<String>>,
     16 }
     17 impl PrivateFulfillment {
     18     /// Bounds exact private claims before copying. Contact is explicitly absent
     19     /// or provided; no endpoint, delivery or address-verification trust is minted.
     20     pub fn new(
     21         mode: FulfillmentMode,
     22         window: Window,
     23         address: &str,
     24         instructions: &str,
     25         contact: Option<&str>,
     26     ) -> Result<Self, FulfillmentError> {
     27         text(address, 2048)?;
     28         text(instructions, 2048)?;
     29         if let Some(contact) = contact {
     30             text(contact, 256)?;
     31         }
     32         Ok(Self {
     33             mode,
     34             window,
     35             address: Zeroizing::new(address.into()),
     36             instructions: Zeroizing::new(instructions.into()),
     37             contact: contact.map(|v| Zeroizing::new(v.into())),
     38         })
     39     }
     40     /// Returns the explicitly selected private mode.
     41     pub const fn mode(&self) -> FulfillmentMode {
     42         self.mode
     43     }
     44     /// Returns the exact required window without ambient time conversion.
     45     pub const fn window(&self) -> Window {
     46         self.window
     47     }
     48     /// Explicitly borrows private address text; callers own any disclosure.
     49     pub fn address(&self) -> &str {
     50         &self.address
     51     }
     52     /// Explicitly borrows private instructions.
     53     pub fn instructions(&self) -> &str {
     54         &self.instructions
     55     }
     56     /// Explicitly borrows the optional private contact claim.
     57     pub fn contact(&self) -> Option<&str> {
     58         self.contact.as_ref().map(|v| v.as_str())
     59     }
     60 }
     61 impl fmt::Debug for PrivateFulfillment {
     62     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     63         f.write_str("PrivateFulfillment(<redacted>)")
     64     }
     65 }
     66 fn text(value: &str, maximum: usize) -> Result<(), FulfillmentError> {
     67     if value.len() > maximum * 4 || value.chars().count() > maximum {
     68         return Err(FulfillmentError::InvalidText);
     69     }
     70     Ok(())
     71 }
     72 /// Input-free private fulfillment construction failure.
     73 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     74 #[non_exhaustive]
     75 pub enum FulfillmentError {
     76     /// Private text exceeds its declared limit.
     77     InvalidText,
     78 }
     79 impl fmt::Display for FulfillmentError {
     80     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     81         f.write_str("invalid private fulfillment text")
     82     }
     83 }
     84 impl core::error::Error for FulfillmentError {}