lib

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

decode.rs (1693B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::{
      3     string::{String, ToString},
      4     vec::Vec,
      5 };
      6 
      7 use radroots_event::{envelope::kind::KIND_SEAL, social::seal::Seal};
      8 
      9 use crate::error::EventParseError;
     10 use crate::parsed::{RadrootsParsedData, RadrootsParsedEvent};
     11 
     12 const DEFAULT_KIND: u32 = KIND_SEAL;
     13 
     14 pub fn seal_from_parts(
     15     kind: u32,
     16     tags: &[Vec<String>],
     17     content: &str,
     18 ) -> Result<Seal, EventParseError> {
     19     if kind != DEFAULT_KIND {
     20         return Err(EventParseError::InvalidKind {
     21             expected: "13",
     22             got: kind,
     23         });
     24     }
     25     if !tags.is_empty() {
     26         return Err(EventParseError::InvalidTag("tags"));
     27     }
     28     if content.trim().is_empty() {
     29         return Err(EventParseError::InvalidTag("content"));
     30     }
     31     Ok(Seal {
     32         content: content.to_string(),
     33     })
     34 }
     35 
     36 pub fn data_from_event(
     37     id: String,
     38     author: String,
     39     published_at: u64,
     40     kind: u32,
     41     content: String,
     42     tags: Vec<Vec<String>>,
     43 ) -> Result<RadrootsParsedData<Seal>, EventParseError> {
     44     let seal = seal_from_parts(kind, &tags, &content)?;
     45     Ok(RadrootsParsedData::new(
     46         id,
     47         author,
     48         published_at,
     49         kind,
     50         seal,
     51     ))
     52 }
     53 
     54 pub fn parsed_from_event(
     55     id: String,
     56     author: String,
     57     published_at: u64,
     58     kind: u32,
     59     content: String,
     60     tags: Vec<Vec<String>>,
     61     sig: String,
     62 ) -> Result<RadrootsParsedEvent<Seal>, EventParseError> {
     63     let data = data_from_event(
     64         id.clone(),
     65         author.clone(),
     66         published_at,
     67         kind,
     68         content.clone(),
     69         tags.clone(),
     70     )?;
     71     RadrootsParsedEvent::from_event_parts(id, author, published_at, kind, content, tags, sig, data)
     72 }