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