| 1 |
//! Error converting a [`Parsed`](crate::parsing::Parsed) struct to another type |
| 2 |
|
| 3 |
use core::fmt; |
| 4 |
|
| 5 |
use crate::error; |
| 6 |
|
| 7 |
/// An error that occurred when converting a [`Parsed`](crate::parsing::Parsed) to another type. |
| 8 |
#[non_exhaustive] |
| 9 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 10 |
pub enum TryFromParsed { |
| 11 |
/// The [`Parsed`](crate::parsing::Parsed) did not include enough information to construct the |
| 12 |
/// type. |
| 13 |
InsufficientInformation, |
| 14 |
/// Some component contained an invalid value for the type. |
| 15 |
ComponentRange(error::ComponentRange), |
| 16 |
} |
| 17 |
|
| 18 |
impl fmt::Display for TryFromParsed { |
| 19 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 20 |
match self { |
| 21 |
Self::InsufficientInformation => f.write_str( |
| 22 |
"the `Parsed` struct did not include enough information to construct the type", |
| 23 |
), |
| 24 |
Self::ComponentRange(err) => err.fmt(f), |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl From<error::ComponentRange> for TryFromParsed { |
| 30 |
fn from(v: error::ComponentRange) -> Self { |
| 31 |
Self::ComponentRange(v) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl TryFrom<TryFromParsed> for error::ComponentRange { |
| 36 |
type Error = error::DifferentVariant; |
| 37 |
|
| 38 |
fn try_from(err: TryFromParsed) -> Result<Self, Self::Error> { |
| 39 |
match err { |
| 40 |
TryFromParsed::ComponentRange(err) => Ok(err), |
| 41 |
_ => Err(error::DifferentVariant), |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(feature = "std")] |
| 47 |
impl std::error::Error for TryFromParsed { |
| 48 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 49 |
match self { |
| 50 |
Self::InsufficientInformation => None, |
| 51 |
Self::ComponentRange(err) => Some(err), |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl From<TryFromParsed> for crate::Error { |
| 57 |
fn from(original: TryFromParsed) -> Self { |
| 58 |
Self::TryFromParsed(original) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl TryFrom<crate::Error> for TryFromParsed { |
| 63 |
type Error = error::DifferentVariant; |
| 64 |
|
| 65 |
fn try_from(err: crate::Error) -> Result<Self, Self::Error> { |
| 66 |
match err { |
| 67 |
crate::Error::TryFromParsed(err) => Ok(err), |
| 68 |
_ => Err(error::DifferentVariant), |
| 69 |
} |
| 70 |
} |
| 71 |
} |