use crate::TypeReader;
use std::collections::BTreeSet;
pub struct TypeLimits<'a> {
reader: &'a TypeReader,
inner: BTreeSet<NamespaceTypes>,
}
impl<'a> TypeLimits<'a> {
pub fn new(reader: &'a TypeReader) -> Self {
Self {
reader,
inner: BTreeSet::new(),
}
}
pub fn insert(&mut self, mut limit: NamespaceTypes) -> Result<(), String> {
let namespace = match self
.reader
.types
.iter()
.find(|(name, _)| name.to_lowercase() == limit.namespace)
{
Some((n, _)) => n,
None => return Err(limit.namespace),
};
limit.namespace = namespace.clone();
self.inner.insert(limit);
Ok(())
}
pub fn limits(&self) -> impl Iterator<Item = &NamespaceTypes> {
self.inner.iter()
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct NamespaceTypes {
pub namespace: String,
pub limit: TypeLimit,
}
impl NamespaceTypes {
pub fn contains_type(&self, typ: &str) -> bool {
match &self.limit {
TypeLimit::All => true,
TypeLimit::Some(types) => types.iter().any(|t| t == typ),
}
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum TypeLimit {
All,
Some(Vec<String>),
}