use core::{
fmt::{self, Display},
ops::Range,
};
#[derive(Debug)]
pub struct InvalidLength {
expected: usize,
got: usize,
}
impl InvalidLength {
pub(super) fn new(expected: usize, got: usize) -> Self {
assert!(expected != got);
Self { expected, got }
}
}
impl Display for InvalidLength {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"invalid length: expected {} but got {}",
self.expected, self.got,
))
}
}
#[derive(Debug)]
pub struct InvalidRange<Idx: fmt::Debug> {
expected: Range<Idx>,
got: Idx,
}
impl<Idx: fmt::Debug + PartialOrd<Idx>> InvalidRange<Idx> {
pub(super) fn new(expected: Range<Idx>, got: Idx) -> Self {
assert!(!expected.contains(&got));
Self { expected, got }
}
}
impl<Idx: fmt::Debug + Display> Display for InvalidRange<Idx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"invalid range: expected a value between {} and {} but got {}",
self.expected.start, self.expected.end, self.got,
))
}
}