summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bhttp/src/err.rs
blob: b37fa886980bbc601ce2ee0283676517439bfc6c (plain)
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
#[derive(Debug)]
pub enum Error {
    /// A request used the CONNECT method.
    ConnectUnsupported,
    /// A field contained invalid Unicode.
    CharacterEncoding(std::string::FromUtf8Error),
    /// A field contained an integer value that was out of range.
    IntRange(std::num::TryFromIntError),
    /// The mode of the message was invalid.
    InvalidMode,
    /// An IO error.
    Io(std::io::Error),
    /// A field or line was missing a necessary character.
    Missing(u8),
    /// A URL was missing a key component.
    MissingUrlComponent,
    /// An obs-fold line was the first line of a field section.
    ObsFold,
    /// A field contained a non-integer value.
    ParseInt(std::num::ParseIntError),
    /// A field was truncated.
    Truncated,
    /// A message included the Upgrade field.
    UpgradeUnsupported,
    /// A URL could not be parsed into components.
    UrlParse(url::ParseError),
}

macro_rules! forward_errors {
    {$($(#[$a:meta])* $t:path => $v:ident),* $(,)?} => {
        $(
            impl From<$t> for Error {
                fn from(e: $t) -> Self {
                    Self::$v(e)
                }
            }
        )*

        impl std::error::Error for Error {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                match self {
                    $( $(#[$a])* Self::$v(e) => Some(e), )*
                    _ => None,
                }
            }
        }
    };
}

forward_errors! {
    std::io::Error => Io,
    std::string::FromUtf8Error => CharacterEncoding,
    std::num::ParseIntError => ParseInt,
    std::num::TryFromIntError => IntRange,
    url::ParseError => UrlParse,
}

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

#[cfg(any(
    feature = "read-http",
    feature = "write-http",
    feature = "read-bhttp",
    feature = "write-bhttp"
))]
pub type Res<T> = Result<T, Error>;