lib

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

encode.rs (2344B)


      1 use radroots_events::{job_request::RadrootsJobRequest, kinds::is_request_kind};
      2 
      3 use crate::job::encode::{JobEncodeError, WireEventParts, canonicalize_tags};
      4 use crate::job::util::{job_input_type_tag, push_bid_tag_sat};
      5 
      6 #[cfg(not(feature = "std"))]
      7 use alloc::{
      8     string::{String, ToString},
      9     vec,
     10     vec::Vec,
     11 };
     12 
     13 pub fn job_request_build_tags(req: &RadrootsJobRequest) -> Vec<Vec<String>> {
     14     let mut tags: Vec<Vec<String>> = Vec::with_capacity(
     15         req.inputs.len()
     16             + req.params.len()
     17             + req.relays.len()
     18             + req.providers.len()
     19             + req.topics.len()
     20             + usize::from(req.output.is_some())
     21             + usize::from(req.bid_sat.is_some())
     22             + usize::from(req.encrypted),
     23     );
     24 
     25     for i in &req.inputs {
     26         let mut t = Vec::with_capacity(5);
     27         t.push("i".to_string());
     28         t.push(i.data.clone());
     29         t.push(job_input_type_tag(i.input_type).to_string());
     30         if let Some(relay) = &i.relay {
     31             t.push(relay.clone());
     32         }
     33         if let Some(marker) = &i.marker {
     34             t.push(marker.clone());
     35         }
     36         tags.push(t);
     37     }
     38 
     39     if let Some(out) = &req.output {
     40         tags.push(vec!["output".into(), out.clone()]);
     41     }
     42 
     43     for p in &req.params {
     44         tags.push(vec!["param".into(), p.key.clone(), p.value.clone()]);
     45     }
     46 
     47     if let Some(bid_sat) = req.bid_sat {
     48         push_bid_tag_sat(&mut tags, bid_sat);
     49     }
     50 
     51     for r in &req.relays {
     52         tags.push(vec!["relays".into(), r.clone()]);
     53     }
     54 
     55     for p in &req.providers {
     56         tags.push(vec!["p".into(), p.clone()]);
     57     }
     58 
     59     for t in &req.topics {
     60         tags.push(vec!["t".into(), t.clone()]);
     61     }
     62 
     63     if req.encrypted {
     64         tags.push(vec!["encrypted".into()]);
     65     }
     66 
     67     tags
     68 }
     69 
     70 pub fn to_wire_parts(
     71     req: &RadrootsJobRequest,
     72     content: &str,
     73 ) -> Result<WireEventParts, JobEncodeError> {
     74     let kind = req.kind as u32;
     75     if !is_request_kind(kind) {
     76         return Err(JobEncodeError::InvalidKind(kind));
     77     }
     78     if req.encrypted && req.providers.is_empty() {
     79         return Err(JobEncodeError::MissingProvidersForEncrypted);
     80     }
     81 
     82     let mut tags = job_request_build_tags(req);
     83     canonicalize_tags(&mut tags);
     84 
     85     Ok(WireEventParts {
     86         kind,
     87         content: content.to_string(),
     88         tags,
     89     })
     90 }