lib

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

values.rs (3255B)


      1 use crate::Error;
      2 use std::sync::Arc;
      3 
      4 #[derive(Clone, uniffi::Enum)]
      5 pub enum ChatRelation {
      6     Inquire { source: Option<String> },
      7     Reply { parent: String },
      8 }
      9 
     10 /// An actual installed family-specific shared value, never a private placeholder.
     11 #[derive(Clone, uniffi::Enum)]
     12 pub enum Value {
     13     Chat { message: Arc<ChatValue> },
     14     Listing { listing: Arc<ListingValue> },
     15 }
     16 
     17 #[derive(uniffi::Object)]
     18 pub struct ListingValue {
     19     inner: radroots::listings::Listing,
     20 }
     21 impl ListingValue {
     22     pub(crate) fn captured(inner: &radroots::listings::Listing) -> Arc<Self> {
     23         Arc::new(Self {
     24             inner: inner.clone(),
     25         })
     26     }
     27 }
     28 #[uniffi::export]
     29 impl ListingValue {
     30     pub fn id(&self) -> String {
     31         self.inner.id().to_string()
     32     }
     33     pub fn author(&self) -> String {
     34         self.inner.author().to_hex()
     35     }
     36     pub fn revision(&self) -> String {
     37         self.inner.revision().to_string()
     38     }
     39     pub fn created_at(&self) -> String {
     40         self.inner.created_at().to_string()
     41     }
     42     pub fn published_at(&self) -> String {
     43         self.inner.published_at().to_string()
     44     }
     45     pub fn title(&self) -> String {
     46         self.inner.text().title().to_owned()
     47     }
     48     pub fn summary(&self) -> String {
     49         self.inner.text().summary().to_owned()
     50     }
     51     pub fn description_markdown(&self) -> String {
     52         self.inner.text().description_markdown().to_owned()
     53     }
     54     /// Source-owned validated terms data; this is not a new wire producer.
     55     pub fn terms_json(&self) -> Result<Vec<u8>, Error> {
     56         serde_json::to_vec(self.inner.terms()).map_err(|_| Error::UnsupportedState {
     57             axis: "listing_terms".to_owned(),
     58             operation_id: None,
     59             signer_rejection: None,
     60         })
     61     }
     62 }
     63 
     64 #[derive(uniffi::Object)]
     65 pub struct ChatValue {
     66     inner: radroots::domain::ChatMessage,
     67 }
     68 impl ChatValue {
     69     pub(crate) fn captured(inner: &radroots::domain::ChatMessage) -> Arc<Self> {
     70         Arc::new(Self {
     71             inner: inner.clone(),
     72         })
     73     }
     74 }
     75 #[uniffi::export]
     76 impl ChatValue {
     77     pub fn id(&self) -> String {
     78         self.inner.id().to_string()
     79     }
     80     pub fn thread_id(&self) -> String {
     81         self.inner.thread_id().to_string()
     82     }
     83     pub fn author(&self) -> String {
     84         self.inner.author().to_hex()
     85     }
     86     pub fn peer(&self) -> String {
     87         self.inner.peer().to_hex()
     88     }
     89     pub fn created_at(&self) -> String {
     90         self.inner.created_at().to_string()
     91     }
     92     pub fn relation(&self) -> Result<ChatRelation, Error> {
     93         match self.inner.relation() {
     94             radroots::domain::ChatRelation::Inquire { source } => Ok(ChatRelation::Inquire {
     95                 source: source.map(|v| v.to_string()),
     96             }),
     97             radroots::domain::ChatRelation::Reply { parent } => Ok(ChatRelation::Reply {
     98                 parent: parent.to_string(),
     99             }),
    100             _ => Err(Error::UnsupportedState {
    101                 axis: "chat_relation".to_owned(),
    102                 operation_id: None,
    103                 signer_rejection: None,
    104             }),
    105         }
    106     }
    107     /// Explicit authorized presentation copy; Rust retains zeroizing storage.
    108     pub fn text(&self) -> String {
    109         self.inner.text().to_owned()
    110     }
    111 }