lib

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

value.rs (7260B)


      1 use super::CommerceError;
      2 use crate::{AgreementLineId, Decimal, Money, Quantity, RecordId};
      3 use alloc::{boxed::Box, string::String, vec::Vec};
      4 use core::fmt;
      5 use zeroize::Zeroizing;
      6 
      7 /// An exact nonnegative line assertion, not a physical movement or authorization.
      8 #[derive(Clone, Copy, Eq, PartialEq)]
      9 pub struct LineQuantity {
     10     line_id: AgreementLineId,
     11     amount: Decimal,
     12 }
     13 impl LineQuantity {
     14     /// Captures exact data. Operation policy must separately admit a positive
     15     /// movement where required; a zero observation is not a stock release.
     16     pub const fn new(line_id: AgreementLineId, amount: Decimal) -> Self {
     17         Self { line_id, amount }
     18     }
     19     /// Returns the exact line identity claim.
     20     pub const fn line_id(self) -> AgreementLineId {
     21         self.line_id
     22     }
     23     /// Returns the exact asserted amount, without conversion or stock effect.
     24     pub const fn amount(self) -> Decimal {
     25         self.amount
     26     }
     27 }
     28 
     29 /// A measurement/price assertion pending agreement-relative verification.
     30 #[derive(Clone, Copy, Eq, PartialEq)]
     31 pub struct FinalMeasurement {
     32     line_id: AgreementLineId,
     33     quantity: Quantity,
     34     line_total: Money,
     35 }
     36 impl FinalMeasurement {
     37     /// Captures validated representations. The admitting executor must check
     38     /// unit, range, price and cumulative quantities against the exact agreement.
     39     pub const fn new(line_id: AgreementLineId, quantity: Quantity, line_total: Money) -> Self {
     40         Self {
     41             line_id,
     42             quantity,
     43             line_total,
     44         }
     45     }
     46     /// Returns the line identity claim.
     47     pub const fn line_id(self) -> AgreementLineId {
     48         self.line_id
     49     }
     50     /// Returns the exact measured quantity claim.
     51     pub const fn quantity(self) -> Quantity {
     52         self.quantity
     53     }
     54     /// Returns the claimed price; it is never a trusted total override.
     55     pub const fn line_total(self) -> Money {
     56         self.line_total
     57     }
     58 }
     59 
     60 /// Closed issue vocabulary, without a unilateral change to agreement terms.
     61 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     62 #[non_exhaustive]
     63 pub enum IssueCode {
     64     /// A participant reports a shortage.
     65     Shortage,
     66     /// A participant reports a quality concern.
     67     Quality,
     68     /// A participant reports a substitution concern.
     69     Substitution,
     70     /// A participant reports a collection concern.
     71     Collection,
     72     /// A participant reports an excess.
     73     Excess,
     74     /// Another explicitly described issue.
     75     Other,
     76 }
     77 impl IssueCode {
     78     /// Returns the exact supported profile token.
     79     pub const fn as_str(self) -> &'static str {
     80         match self {
     81             Self::Shortage => "shortage",
     82             Self::Quality => "quality",
     83             Self::Substitution => "substitution",
     84             Self::Collection => "collection",
     85             Self::Excess => "excess",
     86             Self::Other => "other",
     87         }
     88     }
     89 }
     90 
     91 /// Complete bounded issue data; it establishes neither physical truth nor consent.
     92 #[derive(Clone, Eq, PartialEq)]
     93 pub struct IssueReport {
     94     code: IssueCode,
     95     description: Zeroizing<String>,
     96     lines: Box<[LineQuantity]>,
     97     source_records: Box<[RecordId]>,
     98 }
     99 impl IssueReport {
    100     /// Validates bounds and duplicate identities before owned text capture.
    101     pub fn new(
    102         code: IssueCode,
    103         description: &str,
    104         lines: Vec<LineQuantity>,
    105         source_records: Vec<RecordId>,
    106     ) -> Result<Self, CommerceError> {
    107         check_text(description, 2048)?;
    108         check_lines(&lines, 0)?;
    109         check_records(&source_records, 0)?;
    110         Ok(Self {
    111             code,
    112             description: Zeroizing::new(description.into()),
    113             lines: lines.into_boxed_slice(),
    114             source_records: source_records.into_boxed_slice(),
    115         })
    116     }
    117     /// Returns the explicit issue vocabulary value.
    118     pub const fn code(&self) -> IssueCode {
    119         self.code
    120     }
    121     /// Explicitly borrows private issue text.
    122     pub fn description(&self) -> &str {
    123         &self.description
    124     }
    125     /// Borrows the complete ordered line assertions.
    126     pub fn lines(&self) -> &[LineQuantity] {
    127         &self.lines
    128     }
    129     /// Borrows exact structured-record references; they remain unauthenticated.
    130     pub fn source_records(&self) -> &[RecordId] {
    131         &self.source_records
    132     }
    133 }
    134 
    135 /// Complete proposed resolution, without agreement installation or release.
    136 #[derive(Clone, Eq, PartialEq)]
    137 pub struct Resolution {
    138     issues: Box<[RecordId]>,
    139     accepted_receipts: Box<[RecordId]>,
    140     release_residual: Box<[LineQuantity]>,
    141     summary: Zeroizing<String>,
    142 }
    143 impl Resolution {
    144     /// Bounds the complete proposal and rejects duplicate references/line IDs.
    145     pub fn new(
    146         issues: Vec<RecordId>,
    147         accepted_receipts: Vec<RecordId>,
    148         release_residual: Vec<LineQuantity>,
    149         summary: &str,
    150     ) -> Result<Self, CommerceError> {
    151         check_records(&issues, 1)?;
    152         check_records(&accepted_receipts, 0)?;
    153         check_lines(&release_residual, 0)?;
    154         check_text(summary, 2048)?;
    155         Ok(Self {
    156             issues: issues.into_boxed_slice(),
    157             accepted_receipts: accepted_receipts.into_boxed_slice(),
    158             release_residual: release_residual.into_boxed_slice(),
    159             summary: Zeroizing::new(summary.into()),
    160         })
    161     }
    162     /// Borrows the nonempty exact issue-reference list.
    163     pub fn issues(&self) -> &[RecordId] {
    164         &self.issues
    165     }
    166     /// Borrows explicitly accepted receipt references, without inferred truth.
    167     pub fn accepted_receipts(&self) -> &[RecordId] {
    168         &self.accepted_receipts
    169     }
    170     /// Borrows proposed residual quantities; nothing is released by construction.
    171     pub fn release_residual(&self) -> &[LineQuantity] {
    172         &self.release_residual
    173     }
    174     /// Explicitly borrows private resolution text.
    175     pub fn summary(&self) -> &str {
    176         &self.summary
    177     }
    178 }
    179 
    180 pub(super) fn check_text(value: &str, maximum: usize) -> Result<(), CommerceError> {
    181     if value.len() > maximum * 4 || value.chars().count() > maximum {
    182         return Err(CommerceError::InvalidText);
    183     }
    184     Ok(())
    185 }
    186 pub(super) fn check_lines(lines: &[LineQuantity], minimum: usize) -> Result<(), CommerceError> {
    187     if lines.len() < minimum || lines.len() > 32 {
    188         return Err(CommerceError::InvalidLines);
    189     }
    190     for (index, line) in lines.iter().enumerate() {
    191         if lines[..index]
    192             .iter()
    193             .any(|previous| previous.line_id == line.line_id)
    194         {
    195             return Err(CommerceError::InvalidLines);
    196         }
    197     }
    198     Ok(())
    199 }
    200 pub(super) fn check_records(records: &[RecordId], minimum: usize) -> Result<(), CommerceError> {
    201     if records.len() < minimum || records.len() > 32 {
    202         return Err(CommerceError::InvalidReferences);
    203     }
    204     for (index, record) in records.iter().enumerate() {
    205         if records[..index].contains(record) {
    206             return Err(CommerceError::InvalidReferences);
    207         }
    208     }
    209     Ok(())
    210 }
    211 
    212 macro_rules! redacted {
    213     ($($name:ident),+)=> { $(impl fmt::Debug for $name {
    214         fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result { f.write_str(concat!(stringify!($name),"(<redacted>)")) }
    215     })+ };
    216 }
    217 redacted!(LineQuantity, FinalMeasurement, IssueReport, Resolution);