/rust/registry/src/index.crates.io-1949cf8c6b5b557f/value-bag-1.12.0/src/error.rs
Line | Count | Source |
1 | | use crate::std::fmt; |
2 | | |
3 | | /// An error encountered while working with structured data. |
4 | | #[derive(Debug)] |
5 | | pub struct Error { |
6 | | inner: Inner, |
7 | | } |
8 | | |
9 | | #[derive(Debug)] |
10 | | enum Inner { |
11 | | #[cfg(feature = "std")] |
12 | | Boxed(std_support::BoxedError), |
13 | | Msg(&'static str), |
14 | | Fmt, |
15 | | } |
16 | | |
17 | | impl Error { |
18 | | /// Create an error from a message. |
19 | 0 | pub fn msg(msg: &'static str) -> Self { |
20 | 0 | Error { |
21 | 0 | inner: Inner::Msg(msg), |
22 | 0 | } |
23 | 0 | } |
24 | | |
25 | | #[cfg(feature = "serde1")] |
26 | 0 | pub(crate) fn try_boxed(msg: &'static str, e: impl fmt::Display) -> Self { |
27 | | #[cfg(feature = "std")] |
28 | | { |
29 | 0 | Error::boxed(format!("{msg}: {e}")) |
30 | | } |
31 | | #[cfg(not(feature = "std"))] |
32 | | { |
33 | | let _ = e; |
34 | | Error::msg(msg) |
35 | | } |
36 | 0 | } |
37 | | } |
38 | | |
39 | | impl fmt::Display for Error { |
40 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
41 | | use self::Inner::*; |
42 | 0 | match self.inner { |
43 | | #[cfg(feature = "std")] |
44 | 0 | Boxed(ref err) => err.fmt(f), |
45 | 0 | Msg(ref msg) => msg.fmt(f), |
46 | 0 | Fmt => fmt::Error.fmt(f), |
47 | | } |
48 | 0 | } |
49 | | } |
50 | | |
51 | | impl From<fmt::Error> for Error { |
52 | 0 | fn from(_: fmt::Error) -> Self { |
53 | 0 | Error { inner: Inner::Fmt } |
54 | 0 | } |
55 | | } |
56 | | |
57 | | #[cfg(feature = "std")] |
58 | | mod std_support { |
59 | | use super::*; |
60 | | use crate::std::{boxed::Box, error, io}; |
61 | | |
62 | | pub(crate) type BoxedError = Box<dyn error::Error + Send + Sync>; |
63 | | |
64 | | impl Error { |
65 | | /// Create an error from a standard error type. |
66 | 0 | pub fn boxed<E>(err: E) -> Self |
67 | 0 | where |
68 | 0 | E: Into<BoxedError>, |
69 | | { |
70 | 0 | Error { |
71 | 0 | inner: Inner::Boxed(err.into()), |
72 | 0 | } |
73 | 0 | } Unexecuted instantiation: <value_bag::error::Error>::boxed::<log::kv::error::Error> Unexecuted instantiation: <value_bag::error::Error>::boxed::<std::io::error::Error> |
74 | | } |
75 | | |
76 | | impl error::Error for Error {} |
77 | | |
78 | | impl From<io::Error> for Error { |
79 | 0 | fn from(err: io::Error) -> Self { |
80 | 0 | Error::boxed(err) |
81 | 0 | } |
82 | | } |
83 | | } |