lib

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

view.rs (3992B)


      1 //! Immutable public values. Construction authenticates no source or installation.
      2 use super::{ListingCoordinate, ListingError, ListingId, ListingRevision, ListingTerms, text};
      3 use crate::{EnvelopeEventId, PublicKey, UnixSeconds};
      4 use alloc::string::String;
      5 use core::fmt;
      6 
      7 /// Bounded public presentation fields shared by authoring and retained views.
      8 #[derive(Clone, Eq, PartialEq)]
      9 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
     10 pub struct ListingText {
     11     title: String,
     12     summary: String,
     13     description_markdown: String,
     14 }
     15 impl ListingText {
     16     /// Validates before copying, preserving exact Unicode and whitespace.
     17     pub fn new(
     18         title: &str,
     19         summary: &str,
     20         description_markdown: &str,
     21     ) -> Result<Self, ListingError> {
     22         text(title, 160, false)?;
     23         text(summary, 280, false)?;
     24         text(description_markdown, 16_384, true)?;
     25         Ok(Self {
     26             title: title.into(),
     27             summary: summary.into(),
     28             description_markdown: description_markdown.into(),
     29         })
     30     }
     31     /// Original public title.
     32     pub fn title(&self) -> &str {
     33         &self.title
     34     }
     35     /// Original public summary.
     36     pub fn summary(&self) -> &str {
     37         &self.summary
     38     }
     39     /// Original public Markdown, without rendering or sanitization effects.
     40     pub fn description_markdown(&self) -> &str {
     41         &self.description_markdown
     42     }
     43 }
     44 impl fmt::Debug for ListingText {
     45     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     46         f.write_str("ListingText(<redacted>)")
     47     }
     48 }
     49 
     50 /// One immutable public listing revision. This value proves neither authenticated
     51 /// observation, local installation, relay freshness nor stock authority.
     52 #[derive(Clone, Eq, PartialEq)]
     53 pub struct Listing {
     54     id: ListingId,
     55     author: PublicKey,
     56     revision: EnvelopeEventId,
     57     created_at: UnixSeconds,
     58     published_at: UnixSeconds,
     59     text: ListingText,
     60     terms: ListingTerms,
     61 }
     62 impl Listing {
     63     /// Captures explicit original values. Concrete execution must separately
     64     /// authenticate the original command, signed revision and installation.
     65     pub fn new(
     66         id: ListingId,
     67         author: PublicKey,
     68         revision: EnvelopeEventId,
     69         created_at: UnixSeconds,
     70         published_at: UnixSeconds,
     71         text: ListingText,
     72         terms: ListingTerms,
     73     ) -> Result<Self, ListingError> {
     74         if published_at > created_at {
     75             return Err(ListingError::InvalidTime);
     76         }
     77         Ok(Self {
     78             id,
     79             author,
     80             revision,
     81             created_at,
     82             published_at,
     83             text,
     84             terms,
     85         })
     86     }
     87     /// Stable coordinate discriminator, separate from its signed revision.
     88     pub const fn id(&self) -> ListingId {
     89         self.id
     90     }
     91     /// Original public author.
     92     pub const fn author(&self) -> PublicKey {
     93         self.author
     94     }
     95     /// Exact signed event identity for this revision.
     96     pub const fn revision(&self) -> EnvelopeEventId {
     97         self.revision
     98     }
     99     /// Returns the exact structural source reference, without new source proof.
    100     pub const fn source(&self) -> ListingRevision {
    101         ListingRevision::new(ListingCoordinate::new(self.author, self.id), self.revision)
    102     }
    103     /// Original signed event time, not local installation or current time.
    104     pub const fn created_at(&self) -> UnixSeconds {
    105         self.created_at
    106     }
    107     /// Original declared publication time.
    108     pub const fn published_at(&self) -> UnixSeconds {
    109         self.published_at
    110     }
    111     /// Original bounded presentation values.
    112     pub const fn text(&self) -> &ListingText {
    113         &self.text
    114     }
    115     /// Exact original validated offer terms; these allocate no private stock.
    116     pub const fn terms(&self) -> &ListingTerms {
    117         &self.terms
    118     }
    119 }
    120 impl fmt::Debug for Listing {
    121     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    122         f.write_str("Listing(<redacted>)")
    123     }
    124 }