acme_lib/
error.rs

1//
2use std::fmt;
3use std::io;
4
5use crate::api::ApiProblem;
6
7/// acme-lib result.
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// acme-lib errors.
11#[derive(Debug)]
12pub enum Error {
13    /// An API call failed.
14    ApiProblem(ApiProblem),
15    /// An API call failed.
16    Call(String),
17    /// Base64 decoding failed.
18    Base64Decode(base64::DecodeError),
19    /// JSON serialization/deserialization error.
20    Json(serde_json::Error),
21    /// std::io error.
22    Io(io::Error),
23    /// Some other error. Notice that `Error` is
24    /// `From<String>` and `From<&str>` and it becomes `Other`.
25    Other(String),
26}
27impl std::error::Error for Error {}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        match self {
32            Error::ApiProblem(a) => write!(f, "{}", a),
33            Error::Call(s) => write!(f, "{}", s),
34            Error::Base64Decode(e) => write!(f, "{}", e),
35            Error::Json(e) => write!(f, "{}", e),
36            Error::Io(e) => write!(f, "{}", e),
37            Error::Other(s) => write!(f, "{}", s),
38        }
39    }
40}
41
42impl From<ApiProblem> for Error {
43    fn from(e: ApiProblem) -> Self {
44        Error::ApiProblem(e)
45    }
46}
47
48impl From<serde_json::Error> for Error {
49    fn from(e: serde_json::Error) -> Self {
50        Error::Json(e)
51    }
52}
53
54impl From<io::Error> for Error {
55    fn from(e: io::Error) -> Self {
56        Error::Io(e)
57    }
58}
59
60impl From<String> for Error {
61    fn from(s: String) -> Self {
62        Error::Other(s)
63    }
64}
65
66impl From<&str> for Error {
67    fn from(s: &str) -> Self {
68        Error::Other(s.to_string())
69    }
70}