1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::protocol::{HttpMethod, HttpStatus};
#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::fmt;
use core::num;
use core::str;

#[derive(Debug)]
pub enum Error {
    ParseError(String),
    ParseIntError(num::ParseIntError),
    Utf8Error(str::Utf8Error),
    UnexpectedScheme(String),
    UnexpectedEof(String),
    UnexpectedStatus(HttpStatus),
    UnexpectedMethod(HttpMethod),
    UrlError(String),
    LengthRequired,
    Other(String),

    #[cfg(feature = "std")]
    /// *This variant is available if http_io is built with the `"std"` feature.*
    IoError(std::io::Error),

    #[cfg(feature = "ssl")]
    /// *This variant is available if http_io is built with the `"ssl"` feature.*
    SslError(crate::ssl::Error),
}

pub type Result<R> = core::result::Result<R, Error>;

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl From<str::Utf8Error> for Error {
    fn from(e: str::Utf8Error) -> Self {
        Error::Utf8Error(e)
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::IoError(e)
    }
}

impl From<num::ParseIntError> for Error {
    fn from(e: num::ParseIntError) -> Self {
        Error::ParseIntError(e)
    }
}

#[cfg(feature = "std")]
impl<W> From<std::io::IntoInnerError<W>> for Error {
    fn from(e: std::io::IntoInnerError<W>) -> Self {
        Error::IoError(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("{}", e.error()),
        ))
    }
}

#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
    fn from(e: Error) -> Self {
        std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
    }
}

#[cfg(feature = "ssl")]
impl From<crate::ssl::Error> for Error {
    fn from(e: crate::ssl::Error) -> Self {
        Self::SslError(e)
    }
}