lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

report.rs (2165B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::string::String;
      3 
      4 use crate::social::{ReportFileTarget, ReportType, SocialTarget};
      5 
      6 #[cfg_attr(
      7     any(feature = "serde", test),
      8     derive(serde::Serialize, serde::Deserialize)
      9 )]
     10 #[derive(Clone, Debug)]
     11 pub struct Report {
     12     pub reported_pubkey: String,
     13     pub report_type: ReportType,
     14     #[cfg_attr(
     15         feature = "serde",
     16         serde(default, skip_serializing_if = "Option::is_none")
     17     )]
     18     pub event: Option<SocialTarget>,
     19     #[cfg_attr(
     20         feature = "serde",
     21         serde(default, skip_serializing_if = "Option::is_none")
     22     )]
     23     pub file: Option<ReportFileTarget>,
     24     #[cfg_attr(
     25         feature = "serde",
     26         serde(default, skip_serializing_if = "Option::is_none")
     27     )]
     28     pub content: Option<String>,
     29 }
     30 
     31 #[cfg(test)]
     32 #[cfg_attr(coverage_nightly, coverage(off))]
     33 mod tests {
     34     use super::*;
     35 
     36     #[test]
     37     fn report_model_requires_reported_pubkey_field() {
     38         let report = Report {
     39             reported_pubkey: "a".repeat(64),
     40             report_type: ReportType::Spam,
     41             event: Some(SocialTarget::Event {
     42                 id: "b".repeat(64),
     43                 author: Some("a".repeat(64)),
     44                 event_kind: Some(1),
     45                 relays: None,
     46             }),
     47             file: None,
     48             content: Some("repeated spam".to_string()),
     49         };
     50 
     51         assert_eq!(report.reported_pubkey.len(), 64);
     52         assert_eq!(report.report_type, ReportType::Spam);
     53         assert!(matches!(report.event, Some(SocialTarget::Event { .. })));
     54     }
     55 
     56     #[test]
     57     fn report_model_supports_file_targets_with_required_pubkey() {
     58         let report = Report {
     59             reported_pubkey: "a".repeat(64),
     60             report_type: ReportType::Malware,
     61             event: None,
     62             file: Some(ReportFileTarget {
     63                 sha256: Some("b".repeat(64)),
     64                 url: Some("https://example.test/file".to_string()),
     65                 magnet: None,
     66             }),
     67             content: None,
     68         };
     69 
     70         assert_eq!(report.reported_pubkey.len(), 64);
     71         assert_eq!(report.file.expect("file").sha256.expect("hash").len(), 64);
     72     }
     73 }