article.rs (2226B)
1 #[cfg(not(feature = "std"))] 2 use alloc::{string::String, vec::Vec}; 3 4 use crate::social::{RadrootsSocialFarmAnchor, RadrootsSocialLocation}; 5 6 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 7 #[derive(Clone, Debug)] 8 pub struct RadrootsArticle { 9 pub d_tag: String, 10 pub title: String, 11 pub content: String, 12 #[cfg_attr( 13 feature = "serde", 14 serde(default, skip_serializing_if = "Option::is_none") 15 )] 16 pub summary: Option<String>, 17 #[cfg_attr( 18 feature = "serde", 19 serde(default, skip_serializing_if = "Option::is_none") 20 )] 21 pub image: Option<String>, 22 #[cfg_attr( 23 feature = "serde", 24 serde(default, skip_serializing_if = "Option::is_none") 25 )] 26 pub published_at: Option<u64>, 27 #[cfg_attr( 28 feature = "serde", 29 serde(default, skip_serializing_if = "Option::is_none") 30 )] 31 pub farm: Option<RadrootsSocialFarmAnchor>, 32 #[cfg_attr( 33 feature = "serde", 34 serde(default, skip_serializing_if = "Option::is_none") 35 )] 36 pub location: Option<RadrootsSocialLocation>, 37 #[cfg_attr( 38 feature = "serde", 39 serde(default, skip_serializing_if = "Option::is_none") 40 )] 41 pub topics: Option<Vec<String>>, 42 } 43 44 #[cfg(all(test, feature = "std", feature = "serde"))] 45 mod tests { 46 use super::*; 47 48 #[test] 49 fn article_represents_required_nip23_fields() { 50 let article = RadrootsArticle { 51 d_tag: "soil-notes".to_string(), 52 title: "soil notes".to_string(), 53 content: "# soil notes".to_string(), 54 summary: None, 55 image: None, 56 published_at: Some(1_700_000_000), 57 farm: None, 58 location: Some(RadrootsSocialLocation { 59 name: Some("field edge".to_string()), 60 geohash: Some("c23nb62w20st".to_string()), 61 }), 62 topics: Some(vec!["soil".to_string(), "cover-crops".to_string()]), 63 }; 64 65 assert_eq!(article.d_tag, "soil-notes"); 66 assert_eq!(article.title, "soil notes"); 67 assert_eq!(article.published_at, Some(1_700_000_000)); 68 assert_eq!(article.topics.as_ref().expect("topics").len(), 2); 69 } 70 }