lit_sev_snp_utils/
error.rs

1#![cfg_attr(target_arch = "wasm32", allow(unused))]
2use std::error::Error as StdError;
3use std::fmt;
4use std::io;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8pub struct Error {
9    inner: Box<Inner>,
10}
11
12pub(crate) type BoxError = Box<dyn StdError + Send + Sync>;
13
14struct Inner {
15    kind: Kind,
16    msg: Option<String>,
17    source: Option<BoxError>,
18}
19
20impl Error {
21    pub(crate) fn new<E>(kind: Kind, msg: Option<String>, source: Option<E>) -> Error
22        where
23            E: Into<BoxError>,
24    {
25        Error {
26            inner: Box::new(Inner {
27                kind,
28                msg,
29                source: source.map(Into::into),
30            }),
31        }
32    }
33
34    pub(crate) fn new_msg(kind: Kind, msg: Option<String>) -> Error
35    {
36        Error {
37            inner: Box::new(Inner {
38                kind,
39                msg,
40                source: None
41            }),
42        }
43    }
44
45    #[allow(unused)]
46    pub(crate) fn into_io(self) -> io::Error {
47        io::Error::new(io::ErrorKind::Other, self)
48    }
49}
50
51impl fmt::Debug for Error {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        let mut builder = f.debug_struct("sev_snp_utils::Error");
54
55        builder.field("kind", &self.inner.kind);
56
57        if let Some(ref msg) = self.inner.msg {
58            builder.field("msg", msg);
59        }
60
61        if let Some(ref source) = self.inner.source {
62            builder.field("source", source);
63        }
64
65        builder.finish()
66    }
67}
68
69impl fmt::Display for Error {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        match self.inner.kind {
72            Kind::Conversion => f.write_str("conversion error")?,
73            Kind::Validation => f.write_str("validation error")?,
74            Kind::Fetch => f.write_str("fetch error")?,
75            Kind::Io => f.write_str("io error")?,
76            Kind::Cert => f.write_str("cert error")?,
77            Kind::OpenSSL => f.write_str("OpenSSL error")?,
78        };
79
80        if let Some(msg) = &self.inner.msg {
81            write!(f, ": {}", msg)?;
82        }
83
84        if let Some(e) = &self.inner.source {
85            write!(f, ": {}", e)?;
86        }
87
88        Ok(())
89    }
90}
91
92impl StdError for Error {
93    fn source(&self) -> Option<&(dyn StdError + 'static)> {
94        self.inner.source.as_ref().map(|e| &**e as _)
95    }
96}
97
98#[derive(Debug)]
99pub(crate) enum Kind {
100    Conversion,
101    Validation,
102    Fetch,
103    Io,
104    Cert,
105    OpenSSL
106}
107
108// constructors
109
110pub(crate) fn conversion<E: Into<BoxError>>(e: E, msg: Option<String>) -> Error {
111    Error::new(Kind::Conversion, msg, Some(e))
112}
113
114pub(crate) fn validation<E: Into<BoxError>>(e: E, msg: Option<String>) -> Error {
115    Error::new(Kind::Validation, msg, Some(e))
116}
117
118pub(crate) fn fetch<E: Into<BoxError>>(e: E, msg: Option<String>) -> Error {
119    Error::new(Kind::Fetch, msg, Some(e))
120}
121
122pub(crate) fn io<E: Into<BoxError>>(e: E, msg: Option<String>) -> Error {
123    Error::new(Kind::Io, msg, Some(e))
124}
125
126pub(crate) fn map_io_err<E: Into<BoxError>>(e: E) -> Error {
127    io(e, None)
128}
129
130pub(crate) fn cert(msg: Option<String>) -> Error {
131    Error::new_msg(Kind::Cert, msg)
132}
133
134pub(crate) fn map_conversion_err<E: Into<BoxError>>(e: E) -> Error {
135    conversion(e, None)
136}
137
138pub(crate) fn openssl<E: Into<BoxError>>(e: E, msg: Option<String>) -> Error {
139    Error::new(Kind::OpenSSL, msg, Some(e))
140}