use crate::{
collections::HashSet,
error::MatcherError,
event::Event,
subscription::{Subscription, SubscriptionKey},
tree::MatchingTree,
};
#[derive(Debug, Clone)]
pub struct Matcher<T: SubscriptionKey> {
tree: MatchingTree<T>,
}
impl<T: SubscriptionKey> Matcher<T> {
pub fn new() -> Self {
Self {
tree: MatchingTree::new(),
}
}
pub fn add_subscription(&mut self, subscription: Subscription<T>) {
self.tree.add_subscription(subscription)
}
pub fn remove_subscription(&mut self, subscription_id: &T) -> Result<(), MatcherError> {
self.tree.remove_subscription(subscription_id)
}
pub fn match_event(&self, event: &impl Event) -> Result<HashSet<T>, MatcherError> {
self.tree.match_event(event)
}
pub fn subscription_count(&self) -> usize {
self.tree.subscriptions.len()
}
pub fn tree(&self) -> &MatchingTree<T> {
&self.tree
}
pub fn tree_mut(&mut self) -> &mut MatchingTree<T> {
&mut self.tree
}
pub fn optimize(&mut self) -> Result<(), MatcherError> {
self.tree.optimize()
}
pub fn subscription_ids(&self) -> Vec<T> {
self.tree.subscriptions.keys().cloned().collect()
}
pub fn get_subscription(&self, subscription_id: &T) -> Option<&Subscription<T>> {
self.tree.subscriptions.get(subscription_id)
}
#[cfg(feature = "testing")]
pub fn get_subscriptions_for_testing(
&self,
) -> &crate::collections::HashMap<T, Subscription<T>> {
&self.tree.subscriptions
}
}
impl<T: SubscriptionKey> Default for Matcher<T> {
fn default() -> Self {
Self::new()
}
}