From bf7f4d5381203d5c27b2811d62c5b1781533aa2b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 19 Nov 2025 17:01:36 +0000 Subject: fix some clippy fmt warnings --- grasp-audit/src/audit.rs | 84 +++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 40 deletions(-) (limited to 'grasp-audit/src/audit.rs') diff --git a/grasp-audit/src/audit.rs b/grasp-audit/src/audit.rs index 8afe660..5e84409 100644 --- a/grasp-audit/src/audit.rs +++ b/grasp-audit/src/audit.rs @@ -7,13 +7,13 @@ use nostr_sdk::prelude::*; pub struct AuditConfig { /// Unique ID for this audit run pub run_id: String, - + /// Mode: CI (isolated) or Production (live) pub mode: AuditMode, - + /// Cleanup timestamp (events can be cleaned after this) pub cleanup_after: Timestamp, - + /// Whether to actually create events or just query pub read_only: bool, } @@ -23,7 +23,7 @@ pub struct AuditConfig { pub enum AuditMode { /// Isolated CI/CD tests - only see own events CI, - + /// Production audit - see all events, minimal writes Production, } @@ -39,7 +39,7 @@ impl AuditConfig { read_only: false, } } - + /// Create config for production audit pub fn production() -> Self { let run_id = format!("prod-audit-{}", Timestamp::now().as_u64()); @@ -47,10 +47,10 @@ impl AuditConfig { run_id, mode: AuditMode::Production, cleanup_after: Timestamp::now() + 300, // 5 minutes from now - read_only: true, // Default to read-only for production + read_only: true, // Default to read-only for production } } - + /// Create config with custom run ID pub fn with_run_id(run_id: String, mode: AuditMode) -> Self { Self { @@ -60,7 +60,7 @@ impl AuditConfig { read_only: mode == AuditMode::Production, } } - + /// Get audit tags that are automatically added to all events /// /// These tags are automatically added to all events created via [`AuditEventBuilder`]. @@ -102,22 +102,22 @@ impl AuditConfig { /// ``` pub fn audit_tags(&self) -> Vec { use nostr_sdk::prelude::{Alphabet, SingleLetterTag}; - + // Use "t" tags for categorization (standard NIP-01 hashtag type) let t_tag = SingleLetterTag::lowercase(Alphabet::T); - + vec![ + Tag::custom(TagKind::SingleLetter(t_tag), vec!["grasp-audit-test-event"]), Tag::custom( TagKind::SingleLetter(t_tag), - vec!["grasp-audit-test-event"] + vec![format!("audit-{}", self.run_id)], ), Tag::custom( TagKind::SingleLetter(t_tag), - vec![format!("audit-{}", self.run_id)] - ), - Tag::custom( - TagKind::SingleLetter(t_tag), - vec![format!("audit-cleanup-after-{}", self.cleanup_after.as_u64())] + vec![format!( + "audit-cleanup-after-{}", + self.cleanup_after.as_u64() + )], ), ] } @@ -141,28 +141,28 @@ impl AuditEventBuilder { config, } } - + /// Add a tag pub fn tag(mut self, tag: Tag) -> Self { self.tags.push(tag); self } - + /// Add multiple tags pub fn tags(mut self, tags: Vec) -> Self { self.tags.extend(tags); self } - + /// Build the event with audit tags pub fn build(self, keys: &Keys) -> anyhow::Result { let mut all_tags = self.tags; all_tags.extend(self.config.audit_tags()); - + let event = EventBuilder::new(self.kind, self.content) .tags(all_tags) .sign_with_keys(keys)?; - + Ok(event) } } @@ -170,7 +170,7 @@ impl AuditEventBuilder { #[cfg(test)] mod tests { use super::*; - + #[test] fn test_ci_config() { let config = AuditConfig::ci(); @@ -178,7 +178,7 @@ mod tests { assert!(!config.read_only); assert!(config.run_id.starts_with("ci-")); } - + #[test] fn test_production_config() { let config = AuditConfig::production(); @@ -186,18 +186,18 @@ mod tests { assert!(config.read_only); assert!(config.run_id.starts_with("prod-audit-")); } - + #[test] fn test_audit_tags() { use nostr_sdk::prelude::{Alphabet, SingleLetterTag}; - + let config = AuditConfig::ci(); let tags = config.audit_tags(); - + assert_eq!(tags.len(), 3); - + let t_tag = SingleLetterTag::lowercase(Alphabet::T); - + // All tags should be "t" tags (hashtags) for tag in &tags { if let TagKind::SingleLetter(letter) = tag.kind() { @@ -206,36 +206,40 @@ mod tests { panic!("Expected SingleLetter tag"); } } - + // Check for "t" tag with "grasp-audit-test-event" - assert!(tags.iter().any(|t| { - t.content() == Some("grasp-audit-test-event") - })); - + assert!(tags + .iter() + .any(|t| { t.content() == Some("grasp-audit-test-event") })); + // Check for "t" tag with "audit-{run_id}" assert!(tags.iter().any(|t| { - t.content().map(|c| c.starts_with("audit-ci-")).unwrap_or(false) + t.content() + .map(|c| c.starts_with("audit-ci-")) + .unwrap_or(false) })); - + // Check for "t" tag with "audit-cleanup-after-{timestamp}" assert!(tags.iter().any(|t| { - t.content().map(|c| c.starts_with("audit-cleanup-after-")).unwrap_or(false) + t.content() + .map(|c| c.starts_with("audit-cleanup-after-")) + .unwrap_or(false) })); } - + #[test] fn test_audit_event_builder() { let config = AuditConfig::ci(); let keys = Keys::generate(); - + let event = AuditEventBuilder::new(Kind::TextNote, "test", config.clone()) .tag(Tag::custom(TagKind::Custom("test".into()), vec!["value"])) .build(&keys) .unwrap(); - + // Should have our custom tag + 3 audit tags assert!(event.tags.len() >= 4); - + // Verify event is valid assert!(event.verify().is_ok()); } -- cgit v1.2.3