encode.rs (1767B)
1 use core::fmt; 2 3 #[cfg(not(feature = "std"))] 4 use alloc::{string::String, vec, vec::Vec}; 5 6 pub use crate::wire::{WireEventParts, canonicalize_tags, empty_content, to_frozen_draft}; 7 pub use radroots_events::draft::RadrootsFrozenEventDraft; 8 9 #[derive(Debug)] 10 pub enum JobEncodeError { 11 MissingProvidersForEncrypted, 12 InvalidKind(u32), 13 EmptyRequiredField(&'static str), 14 } 15 16 impl fmt::Display for JobEncodeError { 17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 18 match self { 19 JobEncodeError::MissingProvidersForEncrypted => { 20 write!(f, "encrypted=true requires at least one provider ('p') tag") 21 } 22 JobEncodeError::InvalidKind(k) => write!(f, "invalid job event kind: {}", k), 23 JobEncodeError::EmptyRequiredField(n) => write!(f, "empty required field: {}", n), 24 } 25 } 26 } 27 28 #[cfg(feature = "std")] 29 impl std::error::Error for JobEncodeError {} 30 31 #[cfg(feature = "serde_json")] 32 pub fn json_content<T: serde::Serialize>(value: &T) -> Result<String, JobEncodeError> { 33 serde_json::to_string(value).map_err(|_| JobEncodeError::EmptyRequiredField("content-json")) 34 } 35 36 pub fn push_status_tag(tags: &mut Vec<Vec<String>>, status: &str, extra: Option<&str>) { 37 let mut v = vec!["status".into(), status.into()]; 38 if let Some(e) = extra { 39 v.push(e.into()); 40 } 41 tags.push(v); 42 } 43 44 pub fn push_provider_tag(tags: &mut Vec<Vec<String>>, p: &str) { 45 tags.push(vec!["p".into(), p.into()]); 46 } 47 48 pub fn push_relay_tag(tags: &mut Vec<Vec<String>>, r: &str) { 49 tags.push(vec!["relays".into(), r.into()]); 50 } 51 52 pub fn assert_no_inputs_when_encrypted(tags: &[Vec<String>]) -> bool { 53 !tags 54 .iter() 55 .any(|t| t.first().map(|s| s == "i").unwrap_or(false)) 56 }