1use core::fmt;
4use std::io;
5
6use crate::error;
7
8#[non_exhaustive]
10#[allow(missing_copy_implementations)]
11#[derive(Debug)]
12pub enum Format {
13 #[non_exhaustive]
15 InsufficientTypeInformation,
16 InvalidComponent(&'static str),
20 StdIo(io::Error),
22}
23
24impl fmt::Display for Format {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 Self::InsufficientTypeInformation => f.write_str(
28 "The type being formatted does not contain sufficient information to format a \
29 component.",
30 ),
31 Self::InvalidComponent(component) => write!(
32 f,
33 "The {component} component cannot be formatted into the requested format."
34 ),
35 Self::StdIo(err) => err.fmt(f),
36 }
37 }
38}
39
40impl From<io::Error> for Format {
41 fn from(err: io::Error) -> Self {
42 Self::StdIo(err)
43 }
44}
45
46impl TryFrom<Format> for io::Error {
47 type Error = error::DifferentVariant;
48
49 fn try_from(err: Format) -> Result<Self, Self::Error> {
50 match err {
51 Format::StdIo(err) => Ok(err),
52 _ => Err(error::DifferentVariant),
53 }
54 }
55}
56
57#[cfg(feature = "std")]
58#[allow(clippy::std_instead_of_core)]
59impl std::error::Error for Format {
60 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61 match *self {
62 Self::InsufficientTypeInformation | Self::InvalidComponent(_) => None,
63 Self::StdIo(ref err) => Some(err),
64 }
65 }
66}
67
68impl From<Format> for crate::Error {
69 fn from(original: Format) -> Self {
70 Self::Format(original)
71 }
72}
73
74impl TryFrom<crate::Error> for Format {
75 type Error = error::DifferentVariant;
76
77 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
78 match err {
79 crate::Error::Format(err) => Ok(err),
80 _ => Err(error::DifferentVariant),
81 }
82 }
83}
84
85#[cfg(feature = "serde")]
86impl Format {
87 #[doc(hidden)] pub fn into_invalid_serde_value<S: serde::Serializer>(self) -> S::Error {
90 use serde::ser::Error;
91 S::Error::custom(self)
92 }
93}