lib

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

values.rs (4600B)


      1 use super::Error;
      2 use wasm_bindgen::prelude::*;
      3 
      4 /// Actual owned installed family value. Projections grant no execution authority.
      5 #[derive(Clone)]
      6 #[wasm_bindgen]
      7 pub struct Value {
      8     inner: radroots_execution::OperationValue,
      9 }
     10 impl Value {
     11     pub(super) fn listing(inner: &radroots_domain::listing::Listing) -> Self {
     12         Self {
     13             inner: radroots_execution::OperationValue::Listing(Box::new(inner.clone())),
     14         }
     15     }
     16     pub(super) fn chat(inner: &radroots_domain::ChatMessage) -> Self {
     17         Self {
     18             inner: radroots_execution::OperationValue::Chat(Box::new(inner.clone())),
     19         }
     20     }
     21 }
     22 #[wasm_bindgen]
     23 impl Value {
     24     pub fn listing_value(&self) -> Option<ListingValue> {
     25         match &self.inner {
     26             radroots_execution::OperationValue::Listing(inner) => Some(ListingValue {
     27                 inner: inner.as_ref().clone(),
     28             }),
     29             _ => None,
     30         }
     31     }
     32     pub fn chat_value(&self) -> Option<ChatValue> {
     33         match &self.inner {
     34             radroots_execution::OperationValue::Chat(inner) => Some(ChatValue {
     35                 inner: inner.as_ref().clone(),
     36             }),
     37             _ => None,
     38         }
     39     }
     40 }
     41 #[wasm_bindgen]
     42 pub struct ListingValue {
     43     inner: radroots_domain::listing::Listing,
     44 }
     45 #[wasm_bindgen]
     46 impl ListingValue {
     47     pub fn id(&self) -> String {
     48         self.inner.id().to_string()
     49     }
     50     pub fn author(&self) -> String {
     51         self.inner.author().to_hex()
     52     }
     53     pub fn revision(&self) -> String {
     54         self.inner.revision().to_string()
     55     }
     56     pub fn created_at(&self) -> String {
     57         self.inner.created_at().to_string()
     58     }
     59     pub fn published_at(&self) -> String {
     60         self.inner.published_at().to_string()
     61     }
     62     pub fn title(&self) -> String {
     63         self.inner.text().title().to_owned()
     64     }
     65     pub fn summary(&self) -> String {
     66         self.inner.text().summary().to_owned()
     67     }
     68     pub fn description_markdown(&self) -> String {
     69         self.inner.text().description_markdown().to_owned()
     70     }
     71     pub fn terms_json(&self) -> Result<Vec<u8>, JsValue> {
     72         serde_json::to_vec(self.inner.terms()).map_err(|_| {
     73             Error::UnsupportedState {
     74                 axis: "listing_terms".into(),
     75                 operation_id: None,
     76                 signer_rejection: None,
     77             }
     78             .into()
     79         })
     80     }
     81 }
     82 #[wasm_bindgen]
     83 pub struct ChatValue {
     84     inner: radroots_domain::ChatMessage,
     85 }
     86 #[wasm_bindgen]
     87 impl ChatValue {
     88     pub fn id(&self) -> String {
     89         self.inner.id().to_string()
     90     }
     91     pub fn thread_id(&self) -> String {
     92         self.inner.thread_id().to_string()
     93     }
     94     pub fn author(&self) -> String {
     95         self.inner.author().to_hex()
     96     }
     97     pub fn peer(&self) -> String {
     98         self.inner.peer().to_hex()
     99     }
    100     pub fn created_at(&self) -> String {
    101         self.inner.created_at().to_string()
    102     }
    103     /// Closed relation kind. A future source relation refuses projection.
    104     pub fn relation(&self) -> Result<String, JsValue> {
    105         match self.inner.relation() {
    106             radroots_domain::ChatRelation::Inquire { .. } => Ok("inquire".into()),
    107             radroots_domain::ChatRelation::Reply { .. } => Ok("reply".into()),
    108             _ => Err(Error::UnsupportedState {
    109                 axis: "chat_relation".into(),
    110                 operation_id: None,
    111                 signer_rejection: None,
    112             }
    113             .into()),
    114         }
    115     }
    116     pub fn source(&self) -> Result<Option<String>, JsValue> {
    117         match self.inner.relation() {
    118             radroots_domain::ChatRelation::Inquire { source } => Ok(source.map(|v| v.to_string())),
    119             radroots_domain::ChatRelation::Reply { .. } => Ok(None),
    120             _ => Err(Error::UnsupportedState {
    121                 axis: "chat_relation".into(),
    122                 operation_id: None,
    123                 signer_rejection: None,
    124             }
    125             .into()),
    126         }
    127     }
    128     pub fn parent(&self) -> Result<Option<String>, JsValue> {
    129         match self.inner.relation() {
    130             radroots_domain::ChatRelation::Reply { parent } => Ok(Some(parent.to_string())),
    131             radroots_domain::ChatRelation::Inquire { .. } => Ok(None),
    132             _ => Err(Error::UnsupportedState {
    133                 axis: "chat_relation".into(),
    134                 operation_id: None,
    135                 signer_rejection: None,
    136             }
    137             .into()),
    138         }
    139     }
    140     /// Explicit presentation copy; JS controls its own resulting string lifetime.
    141     pub fn text(&self) -> String {
    142         self.inner.text().to_owned()
    143     }
    144 }