1//! Component range error
23use core::{fmt, hash};
45use crate::error;
67/// 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(#[automatically_derived]
impl ::core::fmt::Debug for ComponentRange {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f,
"ComponentRange", "name", &self.name, "minimum", &self.minimum,
"maximum", &self.maximum, "value", &self.value,
"conditional_message", &&self.conditional_message)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ComponentRange {
#[inline]
fn clone(&self) -> ComponentRange {
let _: ::core::clone::AssertParamIsClone<&'static str>;
let _: ::core::clone::AssertParamIsClone<i64>;
let _: ::core::clone::AssertParamIsClone<Option<&'static str>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ComponentRange { }Copy, #[automatically_derived]
impl ::core::cmp::Eq for ComponentRange {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<&'static str>;
let _: ::core::cmp::AssertParamIsEq<i64>;
let _: ::core::cmp::AssertParamIsEq<Option<&'static str>>;
}
}Eq)]
11pub struct ComponentRange {
12/// Name of the component.
13pub(crate) name: &'static str,
14/// Minimum allowed value, inclusive.
15pub(crate) minimum: i64,
16/// Maximum allowed value, inclusive.
17pub(crate) maximum: i64,
18/// Value that was provided.
19pub(crate) value: i64,
20/// The minimum and/or maximum value is conditional on the value of other
21 /// parameters.
22pub(crate) conditional_message: Option<&'static str>,
23}
2425impl ComponentRange {
26/// Obtain the name of the component whose value was out of range.
27#[inline]
28pub const fn name(self) -> &'static str {
29self.name
30 }
3132/// 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]
35pub const fn is_conditional(self) -> bool {
36self.conditional_message.is_some()
37 }
38}
3940impl PartialEqfor ComponentRange {
41#[inline]
42fn eq(&self, other: &Self) -> bool {
43self.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}
5152impl hash::Hashfor ComponentRange {
53#[inline]
54fn hash<H: hash::Hasher>(&self, state: &mut H) {
55self.name.hash(state);
56self.minimum.hash(state);
57self.maximum.hash(state);
58self.value.hash(state);
59// Skip the contents of the message when comparing for equality.
60self.conditional_message.is_some().hash(state);
61 }
62}
6364impl fmt::Displayfor ComponentRange {
65#[inline]
66fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67f.write_fmt(format_args!("{0} must be in the range {1}..={2}", self.name,
self.minimum, self.maximum))write!(
68f,
69"{} must be in the range {}..={}",
70self.name, self.minimum, self.maximum
71 )?;
7273if let Some(message) = self.conditional_message {
74f.write_fmt(format_args!(" {0}", message))write!(f, " {message}")?;
75 }
7677Ok(())
78 }
79}
8081impl From<ComponentRange> for crate::Error {
82#[inline]
83fn from(original: ComponentRange) -> Self {
84Self::ComponentRange(original)
85 }
86}
8788impl TryFrom<crate::Error> for ComponentRange {
89type Error = error::DifferentVariant;
9091#[inline]
92fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
93match err {
94crate::Error::ComponentRange(err) => Ok(err),
95_ => Err(error::DifferentVariant),
96 }
97 }
98}
99100/// **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]
104fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105write!(
106 f,
107"a value in the range {}..={}",
108self.minimum, self.maximum
109 )
110 }
111}
112113#[cfg(feature = "serde")]
114impl ComponentRange {
115/// Convert the error to a deserialization error.
116#[inline]
117pub(crate) fn into_de_error<E: serde::de::Error>(self) -> E {
118 E::invalid_value(serde::de::Unexpected::Signed(self.value), &self)
119 }
120}
121122impl core::error::Errorfor ComponentRange {}