| 1 |
//! Component range error |
| 2 |
|
| 3 |
use core::fmt; |
| 4 |
|
| 5 |
use 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, PartialEq, Eq, Hash)] |
| 11 |
pub struct ComponentRange { |
| 12 |
/// Name of the component. |
| 13 |
pub(crate) name: &'static str, |
| 14 |
/// Whether an input with the same value could have succeeded if the values of other components |
| 15 |
/// were different. |
| 16 |
pub(crate) is_conditional: bool, |
| 17 |
} |
| 18 |
|
| 19 |
impl ComponentRange { |
| 20 |
/// Create a new `ComponentRange` error that is not conditional. |
| 21 |
#[inline] |
| 22 |
pub(crate) const fn unconditional(name: &'static str) -> Self { |
| 23 |
Self { |
| 24 |
name, |
| 25 |
is_conditional: false, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/// Create a new `ComponentRange` error that is conditional. |
| 30 |
#[inline] |
| 31 |
pub(crate) const fn conditional(name: &'static str) -> Self { |
| 32 |
Self { |
| 33 |
name, |
| 34 |
is_conditional: true, |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/// Obtain the name of the component whose value was out of range. |
| 39 |
#[inline] |
| 40 |
pub const fn name(self) -> &'static str { |
| 41 |
self.name |
| 42 |
} |
| 43 |
|
| 44 |
/// Whether the value's permitted range is conditional, i.e. whether an input with this |
| 45 |
/// value could have succeeded if the values of other components were different. |
| 46 |
#[inline] |
| 47 |
pub const fn is_conditional(self) -> bool { |
| 48 |
self.is_conditional |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl 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 |
|
| 59 |
impl From<ComponentRange> for crate::Error { |
| 60 |
#[inline] |
| 61 |
fn from(original: ComponentRange) -> Self { |
| 62 |
Self::ComponentRange(original) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl 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 |
/// **This trait implementation is deprecated and will be removed in a future breaking release.** |
| 79 |
#[cfg(feature = "serde")] |
| 80 |
impl 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")] |
| 88 |
impl ComponentRange { |
| 89 |
/// Convert the error to a deserialization error. |
| 90 |
#[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 |
|
| 102 |
impl core::error::Error for ComponentRange {} |