use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterWeight {
Auto,
Range(WeightRange),
Exact(u64),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WeightRange(u8);
impl WeightRange {
pub const fn get(self) -> u8 {
self.0
}
}
impl TryFrom<u8> for WeightRange {
type Error = WeightRangeError;
fn try_from(val: u8) -> Result<Self, Self::Error> {
if val > 15 {
Err(WeightRangeError(val))
} else {
Ok(Self(val))
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct WeightRangeError(u8);
impl fmt::Display for WeightRangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"weight range value {} is out of bounds (must be 0..=15)",
self.0
)
}
}
impl std::error::Error for WeightRangeError {}
impl From<WeightRange> for FilterWeight {
fn from(range: WeightRange) -> Self {
FilterWeight::Range(range)
}
}