lib

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

wire.rs (1178B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::{
      3     string::{String, ToString},
      4     vec::Vec,
      5 };
      6 
      7 use radroots_events::draft::{RadrootsDraftError, RadrootsFrozenEventDraft};
      8 
      9 #[derive(Debug, Clone)]
     10 pub struct WireEventParts {
     11     pub kind: u32,
     12     pub content: String,
     13     pub tags: Vec<Vec<String>>,
     14 }
     15 
     16 pub fn to_frozen_draft(
     17     parts: WireEventParts,
     18     contract_id: impl Into<String>,
     19     expected_pubkey: impl AsRef<str>,
     20     created_at: u32,
     21 ) -> Result<RadrootsFrozenEventDraft, RadrootsDraftError> {
     22     RadrootsFrozenEventDraft::new(
     23         contract_id,
     24         parts.kind,
     25         created_at,
     26         parts.tags,
     27         parts.content,
     28         expected_pubkey,
     29     )
     30 }
     31 
     32 pub fn canonicalize_tags(tags: &mut Vec<Vec<String>>) {
     33     tags.retain(|t| t.first().map(|s| !s.trim().is_empty()).unwrap_or(false));
     34     for t in tags.iter_mut() {
     35         for s in t.iter_mut() {
     36             let trimmed = s.trim();
     37             if trimmed.len() != s.len() {
     38                 *s = trimmed.to_string();
     39             }
     40         }
     41     }
     42     tags.sort_by(|a, b| a.first().cmp(&b.first()).then_with(|| a.cmp(b)));
     43     tags.dedup();
     44 }
     45 
     46 pub fn empty_content() -> String {
     47     String::new()
     48 }