lib

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

encode.rs (1822B)


      1 use radroots_event::{envelope::kind::KIND_JOB_FEEDBACK, social::job_feedback::JobFeedback};
      2 
      3 use crate::job::encode::{JobEncodeError, canonicalize_tags};
      4 use crate::job::util::{feedback_status_tag, push_amount_tag_msat};
      5 use radroots_event::wire::Nip01EventWireParts;
      6 
      7 #[cfg(not(feature = "std"))]
      8 use alloc::{
      9     string::{String, ToString},
     10     vec,
     11     vec::Vec,
     12 };
     13 
     14 pub fn job_feedback_build_tags(fb: &JobFeedback) -> Vec<Vec<String>> {
     15     let mut tags: Vec<Vec<String>> = Vec::with_capacity(
     16         2 + usize::from(fb.customer_pubkey.is_some())
     17             + usize::from(fb.payment.is_some())
     18             + usize::from(fb.encrypted),
     19     );
     20 
     21     let mut st = Vec::with_capacity(3);
     22     st.push("status".to_string());
     23     st.push(feedback_status_tag(fb.status).to_string());
     24     if let Some(info) = &fb.extra_info {
     25         st.push(info.clone());
     26     }
     27     tags.push(st);
     28 
     29     if let Some(pay) = &fb.payment {
     30         push_amount_tag_msat(&mut tags, pay.amount_sat, pay.bolt11.clone());
     31     }
     32 
     33     let mut e = Vec::with_capacity(3);
     34     e.push("e".to_string());
     35     e.push(fb.request_event.id.clone());
     36     if let Some(r) = &fb.request_event.relays {
     37         e.push(r.clone());
     38     }
     39     tags.push(e);
     40 
     41     if let Some(p) = &fb.customer_pubkey {
     42         tags.push(vec!["p".into(), p.clone()]);
     43     }
     44 
     45     if fb.encrypted {
     46         tags.push(vec!["encrypted".into()]);
     47     }
     48 
     49     tags
     50 }
     51 
     52 pub fn to_wire_parts(
     53     fb: &JobFeedback,
     54     content: &str,
     55 ) -> Result<Nip01EventWireParts, JobEncodeError> {
     56     let kind = fb.kind as u32;
     57     if kind != KIND_JOB_FEEDBACK {
     58         return Err(JobEncodeError::InvalidKind(kind));
     59     }
     60 
     61     let mut tags = job_feedback_build_tags(fb);
     62     canonicalize_tags(&mut tags);
     63 
     64     Ok(Nip01EventWireParts {
     65         kind,
     66         content: content.to_string(),
     67         tags,
     68     })
     69 }