time/error/
component_range.rs

1//! Component range error
2
3use core::{fmt, hash};
4
5use crate::error;
6
7/// An error type indicating that a component provided to a method was out of range, causing a
8/// failure.
9// i64 is the narrowest type fitting all use cases. This eliminates the need for a type parameter.
10#[derive(Debug, Clone, Copy, Eq)]
11pub struct ComponentRange {
12    /// Name of the component.
13    pub(crate) name: &'static str,
14    /// Minimum allowed value, inclusive.
15    pub(crate) minimum: i64,
16    /// Maximum allowed value, inclusive.
17    pub(crate) maximum: i64,
18    /// Value that was provided.
19    pub(crate) value: i64,
20    /// The minimum and/or maximum value is conditional on the value of other
21    /// parameters.
22    pub(crate) conditional_message: Option<&'static str>,
23}
24
25impl ComponentRange {
26    /// Obtain the name of the component whose value was out of range.
27    #[inline]
28    pub const fn name(self) -> &'static str {
29        self.name
30    }
31
32    /// Whether the value's permitted range is conditional, i.e. whether an input with this
33    /// value could have succeeded if the values of other components were different.
34    #[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        // Skip the contents of the message when comparing for equality.
48        && 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        // Skip the contents of the message when comparing for equality.
60        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        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            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/// **This trait implementation is deprecated and will be removed in a future breaking release.**
101#[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    /// Convert the error to a deserialization error.
116    #[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 {}