time/error/
format.rs

1//! Error formatting a struct
2
3use alloc::boxed::Box;
4use core::fmt;
5use std::io;
6
7use crate::error;
8
9/// An error occurred when formatting.
10#[non_exhaustive]
11#[derive(Debug)]
12pub enum Format {
13    /// The type being formatted does not contain sufficient information to format a component.
14    #[non_exhaustive]
15    InsufficientTypeInformation,
16    /// The component named has a value that cannot be formatted into the requested format.
17    ///
18    /// This variant is only returned when using well-known formats.
19    InvalidComponent(&'static str),
20    /// A component provided was out of range.
21    ComponentRange(Box<error::ComponentRange>),
22    /// A value of `std::io::Error` was returned internally.
23    StdIo(io::Error),
24}
25
26impl fmt::Display for Format {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::InsufficientTypeInformation => f.write_str(
30                "The type being formatted does not contain sufficient information to format a \
31                 component.",
32            ),
33            Self::InvalidComponent(component) => write!(
34                f,
35                "The {component} component cannot be formatted into the requested format."
36            ),
37            Self::ComponentRange(err) => err.fmt(f),
38            Self::StdIo(err) => err.fmt(f),
39        }
40    }
41}
42
43impl From<error::ComponentRange> for Format {
44    fn from(err: error::ComponentRange) -> Self {
45        Self::ComponentRange(Box::new(err))
46    }
47}
48
49impl From<io::Error> for Format {
50    fn from(err: io::Error) -> Self {
51        Self::StdIo(err)
52    }
53}
54
55impl TryFrom<Format> for error::ComponentRange {
56    type Error = error::DifferentVariant;
57
58    fn try_from(err: Format) -> Result<Self, Self::Error> {
59        match err {
60            Format::ComponentRange(err) => Ok(*err),
61            _ => Err(error::DifferentVariant),
62        }
63    }
64}
65
66impl TryFrom<Format> for io::Error {
67    type Error = error::DifferentVariant;
68
69    fn try_from(err: Format) -> Result<Self, Self::Error> {
70        match err {
71            Format::StdIo(err) => Ok(err),
72            _ => Err(error::DifferentVariant),
73        }
74    }
75}
76
77#[cfg(feature = "std")]
78impl std::error::Error for Format {
79    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
80        match self {
81            Self::InsufficientTypeInformation | Self::InvalidComponent(_) => None,
82            Self::ComponentRange(err) => Some(&**err),
83            Self::StdIo(err) => Some(err),
84        }
85    }
86}
87
88impl From<Format> for crate::Error {
89    fn from(original: Format) -> Self {
90        Self::Format(original)
91    }
92}
93
94impl TryFrom<crate::Error> for Format {
95    type Error = error::DifferentVariant;
96
97    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
98        match err {
99            crate::Error::Format(err) => Ok(err),
100            _ => Err(error::DifferentVariant),
101        }
102    }
103}
104
105#[cfg(feature = "serde")]
106impl Format {
107    /// Obtain an error type for the serializer.
108    #[doc(hidden)] // Exposed only for the `declare_format_string` macro
109    pub fn into_invalid_serde_value<S: serde::Serializer>(self) -> S::Error {
110        use serde::ser::Error;
111        S::Error::custom(self)
112    }
113}