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