encode.rs (3398B)
1 #[cfg(not(feature = "std"))] 2 use alloc::{ 3 string::{String, ToString}, 4 vec, 5 vec::Vec, 6 }; 7 8 use radroots_event::{ 9 envelope::kind::{KIND_FARM, KIND_PLOT}, 10 farm::FarmRef, 11 farm::plot::Plot, 12 tag::name::TAG_D, 13 }; 14 15 use crate::d_tag::validate_d_tag; 16 use crate::error::EventEncodeError; 17 18 #[cfg(feature = "json")] 19 use radroots_event::wire::Nip01EventWireParts; 20 21 const TAG_T: &str = "t"; 22 const TAG_G: &str = "g"; 23 const TAG_A: &str = "a"; 24 const TAG_P: &str = "p"; 25 26 fn push_tag(tags: &mut Vec<Vec<String>>, key: &str, value: &str) { 27 tags.push(vec![key.to_string(), value.to_string()]); 28 } 29 30 fn farm_address(farm: &FarmRef) -> String { 31 let mut value = String::new(); 32 value.push_str(&KIND_FARM.to_string()); 33 value.push(':'); 34 value.push_str(&farm.pubkey); 35 value.push(':'); 36 value.push_str(&farm.d_tag); 37 value 38 } 39 40 pub fn plot_address(author_pubkey: &str, plot_id: &str) -> Result<String, EventEncodeError> { 41 let author_pubkey = author_pubkey.trim(); 42 if author_pubkey.is_empty() { 43 return Err(EventEncodeError::EmptyRequiredField("plot.author_pubkey")); 44 } 45 let plot_id = plot_id.trim(); 46 if plot_id.is_empty() { 47 return Err(EventEncodeError::EmptyRequiredField("plot.d_tag")); 48 } 49 validate_d_tag(plot_id, "plot.d_tag")?; 50 let mut value = String::new(); 51 value.push_str(&KIND_PLOT.to_string()); 52 value.push(':'); 53 value.push_str(author_pubkey); 54 value.push(':'); 55 value.push_str(plot_id); 56 Ok(value) 57 } 58 59 pub fn plot_build_tags(plot: &Plot) -> Result<Vec<Vec<String>>, EventEncodeError> { 60 if plot.d_tag.trim().is_empty() { 61 return Err(EventEncodeError::EmptyRequiredField("d_tag")); 62 } 63 validate_d_tag(&plot.d_tag, "d_tag")?; 64 if plot.name.trim().is_empty() { 65 return Err(EventEncodeError::EmptyRequiredField("name")); 66 } 67 if plot.farm.pubkey.trim().is_empty() { 68 return Err(EventEncodeError::EmptyRequiredField("farm.pubkey")); 69 } 70 if plot.farm.d_tag.trim().is_empty() { 71 return Err(EventEncodeError::EmptyRequiredField("farm.d_tag")); 72 } 73 validate_d_tag(&plot.farm.d_tag, "farm.d_tag")?; 74 let mut tags = Vec::new(); 75 push_tag(&mut tags, TAG_D, &plot.d_tag); 76 push_tag(&mut tags, TAG_A, &farm_address(&plot.farm)); 77 push_tag(&mut tags, TAG_P, &plot.farm.pubkey); 78 if let Some(items) = plot.tags.as_ref() { 79 for item in items.iter().filter(|v| !v.trim().is_empty()) { 80 push_tag(&mut tags, TAG_T, item); 81 } 82 } 83 if let Some(location) = plot.location.as_ref() { 84 let geohash = location.gcs.geohash.trim(); 85 if geohash.is_empty() { 86 return Err(EventEncodeError::EmptyRequiredField("location.gcs.geohash")); 87 } 88 push_tag(&mut tags, TAG_G, geohash); 89 } 90 Ok(tags) 91 } 92 93 #[cfg(feature = "json")] 94 pub fn to_wire_parts(plot: &Plot) -> Result<Nip01EventWireParts, EventEncodeError> { 95 to_wire_parts_with_kind(plot, KIND_PLOT) 96 } 97 98 #[cfg(feature = "json")] 99 pub fn to_wire_parts_with_kind( 100 plot: &Plot, 101 kind: u32, 102 ) -> Result<Nip01EventWireParts, EventEncodeError> { 103 if kind != KIND_PLOT { 104 return Err(EventEncodeError::InvalidKind(kind)); 105 } 106 let tags = plot_build_tags(plot)?; 107 let content = serde_json::to_string(plot).map_err(|_| EventEncodeError::Json)?; 108 Ok(Nip01EventWireParts { 109 kind, 110 content, 111 tags, 112 }) 113 }