use core::marker::PhantomData;
use crate::utils::SimpleSpan;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct TooMany<O: ?Sized, S = SimpleSpan, Lang: ?Sized = ()> {
span: S,
nums: usize,
limit: usize,
_syn: PhantomData<O>,
_lang: PhantomData<Lang>,
}
impl<O: ?Sized, S> TooMany<O, S> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(span: S, nums: usize, maximum: usize) -> Self {
Self::of(span, nums, maximum)
}
}
impl<O: ?Sized, S, Lang: ?Sized> TooMany<O, S, Lang> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn of(span: S, nums: usize, maximum: usize) -> Self {
Self::new_in(span, nums, maximum)
}
}
impl<O: ?Sized, S, Lang: ?Sized> TooMany<O, S, Lang> {
const fn new_in(span: S, nums: usize, limit: usize) -> Self {
Self {
span,
nums,
limit,
_syn: PhantomData,
_lang: PhantomData,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn span(&self) -> &S {
&self.span
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn nums(&self) -> usize {
self.nums
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn limit(&self) -> usize {
self.limit
}
}
impl<O: ?Sized, S> From<TooMany<O, S>> for () {
#[cfg_attr(not(tarpaulin), inline(always))]
fn from(_: TooMany<O, S>) -> Self {}
}
impl<O: ?Sized, S, Lang> TooMany<O, S, Lang>
where
Lang: ?Sized,
{
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn display_fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
where
S: core::fmt::Display,
{
write!(
f,
"too many elements: found {}, but maximum is {} at {}",
self.nums, self.limit, self.span
)
}
}