use crate::Cache;
use sage_mqtt::{SubscriptionOptions, Topic};
use std::{collections::HashMap, sync::Arc};
#[derive(Debug, Clone)]
pub struct Subs {
db: HashMap<Topic, (SubscriptionOptions, Option<u32>)>,
cache: Arc<Cache>,
}
impl Subs {
pub fn new(cache: Arc<Cache>) -> Self {
Subs {
db: Default::default(),
cache,
}
}
pub fn len(&self) -> usize {
self.db.len()
}
pub fn add(
&mut self,
topic: Topic,
options: SubscriptionOptions,
identifier: Option<u32>,
) -> bool {
self.cache.clear();
log::warn!("Subscription Identifier: {:?}", identifier);
self.db.insert(topic, (options, identifier)).is_some()
}
pub fn has_filter(&self, topic: &Topic) -> bool {
self.db.contains_key(topic)
}
pub fn matches(&self, name: &Topic) -> bool {
self.has_filter(name)
}
}