use std::{
collections::{BTreeSet, btree_set},
iter::Extend,
};
use serde::{Deserialize, Serialize};
use crate::spell::CanonicalWordId;
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Hash)]
pub struct DerivedFrom {
inner: BTreeSet<CanonicalWordId>,
}
impl DerivedFrom {
pub fn insert(&mut self, id: CanonicalWordId) {
self.inner.insert(id);
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn is_none(&self) -> bool {
self.is_empty()
}
pub fn contains(&self, id: CanonicalWordId) -> bool {
self.inner.contains(&id)
}
pub fn from_canonical_word_id(word_id: CanonicalWordId) -> Self {
Self {
inner: BTreeSet::from([word_id]),
}
}
pub fn iter(&self) -> btree_set::Iter<'_, CanonicalWordId> {
self.inner.iter()
}
}
impl Extend<CanonicalWordId> for DerivedFrom {
fn extend<T: IntoIterator<Item = CanonicalWordId>>(&mut self, iter: T) {
self.inner.extend(iter);
}
}
impl<'a> Extend<&'a CanonicalWordId> for DerivedFrom {
fn extend<T: IntoIterator<Item = &'a CanonicalWordId>>(&mut self, iter: T) {
self.inner.extend(iter);
}
}
impl IntoIterator for DerivedFrom {
type Item = CanonicalWordId;
type IntoIter = btree_set::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}