use jacquard_common::types::nsid::Nsid; use serde::{Deserialize, Serialize}; use std::sync::Arc; pub type FilterHandle = Arc>; pub fn new_handle(config: FilterConfig) -> FilterHandle { Arc::new(arc_swap::ArcSwap::new(Arc::new(config))) } /// apply a bool patch or set replacement for a single set update. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(untagged)] pub enum SetUpdate { /// replace the entire set with this list Set(Vec), /// patch: true = add, false = remove Patch(std::collections::HashMap), } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum FilterMode { Filter = 0, Full = 2, } #[derive(Debug, Clone, Serialize)] pub struct FilterConfig { pub mode: FilterMode, pub signals: Vec>, pub collections: Vec>, } impl FilterConfig { pub fn new(mode: FilterMode) -> Self { Self { mode, signals: Vec::new(), collections: Vec::new(), } } pub fn matches_collection(&self, collection: &str) -> bool { if self.collections.is_empty() { return true; } self.collections.iter().any(|p| nsid_matches(p, collection)) } pub fn matches_signal(&self, collection: &str) -> bool { self.signals.iter().any(|p| nsid_matches(p, collection)) } pub fn check_signals(&self) -> bool { self.mode == FilterMode::Filter && !self.signals.is_empty() } } fn nsid_matches(pattern: &str, collection: &str) -> bool { if let Some(prefix) = pattern.strip_suffix(".*") { collection == prefix || collection.starts_with(prefix) } else { collection == pattern } }