filter.rs (1290B)
1 //! Explicit construction of portable Nostr subscription filters. 2 //! 3 //! Filters are passive protocol values. Constructing one does not subscribe, 4 //! contact a relay, install a clock, or retain runtime state. 5 6 use crate::Error; 7 use crate::event::Kind; 8 #[cfg(feature = "std")] 9 use crate::event::Timestamp; 10 use alloc::{string::String, string::ToString, vec::Vec}; 11 12 /// Upstream Nostr filter used only at the explicit protocol boundary. 13 pub type Filter = nostr::Filter; 14 15 pub fn with_tag(filter: Filter, tag: &str, values: Vec<String>) -> Result<Filter, Error> { 16 let mut chars = tag.chars(); 17 let tag_char = chars 18 .next() 19 .ok_or_else(|| Error::FilterTagError("tag is empty".to_string()))?; 20 if chars.next().is_some() { 21 return Err(Error::FilterTagError( 22 "tag must be a single letter".to_string(), 23 )); 24 } 25 let tag_key = nostr::filter::SingleLetterTag::from_char(tag_char) 26 .map_err(|err| Error::FilterTagError(err.to_string()))?; 27 Ok(filter.custom_tags(tag_key, values)) 28 } 29 30 pub fn kind(kind: u16) -> Kind { 31 Kind::Custom(kind) 32 } 33 34 pub fn for_kind(kind: u16) -> Filter { 35 Filter::new().kind(Kind::Custom(kind)) 36 } 37 38 #[cfg(feature = "std")] 39 pub fn since_now(filter: Filter) -> Filter { 40 filter.since(Timestamp::now()) 41 }