#![warn(clippy::all, clippy::nursery)]
#![doc = include_str!("../README.md")]
extern crate self as filterable_enum;
pub use enumflags2;
pub use filterable_enum_derive::filterable_enum;
pub trait FilterableEnum: Sized {
type Id;
type Filter: EnumFilter<Self::Id>;
fn filterable_id(&self) -> Self::Id;
fn filter_ref(&self, filter: impl Into<Self::Filter>) -> Option<&Self> {
if filter.into().contains(self.filterable_id()) {
Some(self)
} else {
None
}
}
fn filter_and_take(self, filter: impl Into<Self::Filter>) -> Option<Self> {
if filter.into().contains(self.filterable_id()) {
Some(self)
} else {
None
}
}
}
pub trait EnumFilter<Id> {
fn contains(&self, id: Id) -> bool;
}
impl<Id: enumflags2::BitFlag> EnumFilter<Id> for enumflags2::BitFlags<Id> {
fn contains(&self, id: Id) -> bool {
self.intersects(id)
}
}