lib

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

message.rs (3694B)


      1 //! Bounded logical private chat data, without authenticated installation facts.
      2 use crate::{MessageId, PublicKey, SourceId, ThreadId, UnixSeconds};
      3 use alloc::string::String;
      4 use core::fmt;
      5 use zeroize::Zeroizing;
      6 
      7 /// Maximum plain UTF-8 chat bytes before JSON or encryption expansion.
      8 pub const CHAT_MAX_BYTES: usize = 4096;
      9 
     10 /// Input-free logical message failures.
     11 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     12 pub enum MessageError {
     13     /// The plain text budget was exceeded.
     14     LimitExceeded,
     15 }
     16 impl fmt::Display for MessageError {
     17     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     18         f.write_str("chat text limit exceeded")
     19     }
     20 }
     21 impl core::error::Error for MessageError {}
     22 
     23 /// A logical relation. Selectors alone authenticate no source or parent history.
     24 #[derive(Clone, Copy, Eq, PartialEq)]
     25 #[non_exhaustive]
     26 pub enum ChatRelation {
     27     /// An inquiry, optionally referring to the exact public source event.
     28     Inquire {
     29         /// Exact referenced public event, when supplied.
     30         source: Option<SourceId>,
     31     },
     32     /// A reply to an exact logical message, never an outer envelope ID.
     33     Reply {
     34         /// Original logical parent, whose history requires authentication.
     35         parent: MessageId,
     36     },
     37 }
     38 impl fmt::Debug for ChatRelation {
     39     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     40         f.write_str("ChatRelation(<redacted>)")
     41     }
     42 }
     43 
     44 /// Owned logical participant view. Construction checks bounds only; it supplies
     45 /// no signature, authenticated history, durable admission or installation proof.
     46 /// Private text is explicitly borrowed and is never included in Debug or an
     47 /// automatic serialization implementation.
     48 #[derive(Clone, Eq, PartialEq)]
     49 pub struct ChatMessage {
     50     id: MessageId,
     51     thread: ThreadId,
     52     author: PublicKey,
     53     peer: PublicKey,
     54     created: UnixSeconds,
     55     relation: ChatRelation,
     56     text: Zeroizing<String>,
     57 }
     58 impl ChatMessage {
     59     /// Checks the UTF-8 budget before copying. Empty text and Unicode contents
     60     /// remain representable without trimming or normalization.
     61     pub fn new(
     62         id: MessageId,
     63         thread: ThreadId,
     64         author: PublicKey,
     65         peer: PublicKey,
     66         created: UnixSeconds,
     67         relation: ChatRelation,
     68         text: &str,
     69     ) -> Result<Self, MessageError> {
     70         if text.len() > CHAT_MAX_BYTES {
     71             return Err(MessageError::LimitExceeded);
     72         }
     73         Ok(Self {
     74             id,
     75             thread,
     76             author,
     77             peer,
     78             created,
     79             relation,
     80             text: Zeroizing::new(String::from(text)),
     81         })
     82     }
     83     /// Logical rumor identity, separate from seals and outer envelopes.
     84     pub const fn id(&self) -> MessageId {
     85         self.id
     86     }
     87     /// Exact application thread.
     88     pub const fn thread_id(&self) -> ThreadId {
     89         self.thread
     90     }
     91     /// Original participant author.
     92     pub const fn author(&self) -> PublicKey {
     93         self.author
     94     }
     95     /// Intended peer; a self copy does not replace the peer's outcome.
     96     pub const fn peer(&self) -> PublicKey {
     97         self.peer
     98     }
     99     /// Original logical creation time, not a later installation observation.
    100     pub const fn created_at(&self) -> UnixSeconds {
    101         self.created
    102     }
    103     /// Original inquiry/source or logical reply relation.
    104     pub const fn relation(&self) -> ChatRelation {
    105         self.relation
    106     }
    107     /// Borrows private text for an authorized participant view.
    108     pub fn text(&self) -> &str {
    109         &self.text
    110     }
    111 }
    112 impl fmt::Debug for ChatMessage {
    113     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    114         f.write_str("ChatMessage(<redacted>)")
    115     }
    116 }