time/error/
component_range.rs1use core::{fmt, hash};
4
5use crate::error;
6
7)]
11pub struct ComponentRange {
12    pub(crate) name: &'static str,
14    pub(crate) minimum: i64,
16    pub(crate) maximum: i64,
18    pub(crate) value: i64,
20    pub(crate) conditional_message: Option<&'static str>,
23}
24
25impl ComponentRange {
26    #[inline]
28    pub const fn name(self) -> &'static str {
29        self.name
30    }
31
32    #[inline]
35    pub const fn is_conditional(self) -> bool {
36        self.conditional_message.is_some()
37    }
38}
39
40impl PartialEq for ComponentRange {
41    #[inline]
42    fn eq(&self, other: &Self) -> bool {
43        self.name == other.name
44        && self.minimum == other.minimum
45        && self.maximum == other.maximum
46        && self.value == other.value
47        && self.conditional_message.is_some() == other.conditional_message.is_some()
49    }
50}
51
52impl hash::Hash for ComponentRange {
53    #[inline]
54    fn hash<H: hash::Hasher>(&self, state: &mut H) {
55        self.name.hash(state);
56        self.minimum.hash(state);
57        self.maximum.hash(state);
58        self.value.hash(state);
59        self.conditional_message.is_some().hash(state);
61    }
62}
63
64impl fmt::Display for ComponentRange {
65    #[inline]
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        f.write_fmt(format_args!("{0} must be in the range {1}..={2}", self.name,
        self.minimum, self.maximum))write!(
68            f,
69            "{} must be in the range {}..={}",
70            self.name, self.minimum, self.maximum
71        )?;
72
73        if let Some(message) = self.conditional_message {
74            f.write_fmt(format_args!(" {0}", message))write!(f, " {message}")?;
75        }
76
77        Ok(())
78    }
79}
80
81impl From<ComponentRange> for crate::Error {
82    #[inline]
83    fn from(original: ComponentRange) -> Self {
84        Self::ComponentRange(original)
85    }
86}
87
88impl TryFrom<crate::Error> for ComponentRange {
89    type Error = error::DifferentVariant;
90
91    #[inline]
92    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
93        match err {
94            crate::Error::ComponentRange(err) => Ok(err),
95            _ => Err(error::DifferentVariant),
96        }
97    }
98}
99
100#[cfg(feature = "serde")]
102impl serde::de::Expected for ComponentRange {
103    #[inline]
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        write!(
106            f,
107            "a value in the range {}..={}",
108            self.minimum, self.maximum
109        )
110    }
111}
112
113#[cfg(feature = "serde")]
114impl ComponentRange {
115    #[inline]
117    pub(crate) fn into_de_error<E: serde::de::Error>(self) -> E {
118        E::invalid_value(serde::de::Unexpected::Signed(self.value), &self)
119    }
120}
121
122impl core::error::Error for ComponentRange {}