time/error/
component_range.rs1use core::fmt;
4
5use crate::error;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct ComponentRange {
12 pub(crate) name: &'static str,
14 pub(crate) is_conditional: bool,
17}
18
19impl ComponentRange {
20 #[inline]
22 pub(crate) const fn unconditional(name: &'static str) -> Self {
23 Self {
24 name,
25 is_conditional: false,
26 }
27 }
28
29 #[inline]
31 pub(crate) const fn conditional(name: &'static str) -> Self {
32 Self {
33 name,
34 is_conditional: true,
35 }
36 }
37
38 #[inline]
40 pub const fn name(self) -> &'static str {
41 self.name
42 }
43
44 #[inline]
47 pub const fn is_conditional(self) -> bool {
48 self.is_conditional
49 }
50}
51
52impl fmt::Display for ComponentRange {
53 #[inline]
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 write!(f, "{} was not in range", self.name)
56 }
57}
58
59impl From<ComponentRange> for crate::Error {
60 #[inline]
61 fn from(original: ComponentRange) -> Self {
62 Self::ComponentRange(original)
63 }
64}
65
66impl TryFrom<crate::Error> for ComponentRange {
67 type Error = error::DifferentVariant;
68
69 #[inline]
70 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
71 match err {
72 crate::Error::ComponentRange(err) => Ok(err),
73 _ => Err(error::DifferentVariant),
74 }
75 }
76}
77
78#[cfg(feature = "serde")]
80impl serde_core::de::Expected for ComponentRange {
81 #[inline]
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 f.write_str("an in-range value")
84 }
85}
86
87#[cfg(feature = "serde")]
88impl ComponentRange {
89 #[inline]
91 pub(crate) fn into_de_error<E>(self) -> E
92 where
93 E: serde_core::de::Error,
94 {
95 serde_core::de::Error::custom(format_args!(
96 "invalid {}, expected an in-range value",
97 self.name
98 ))
99 }
100}
101
102impl core::error::Error for ComponentRange {}