1use std::error::Error as StdError;
4use std::fmt;
5use std::str::Utf8Error;
6use std::string::FromUtf8Error;
7
8use self::Error::{
9 Method,
10 Version,
11 Header,
12 Status,
13 TooLarge,
14 Utf8
15};
16
17pub type Result<T> = ::std::result::Result<T, Error>;
19
20#[derive(Debug)]
22pub enum Error {
23 Method,
25 Version,
27 Header,
29 TooLarge,
31 Status,
33 Utf8(Utf8Error),
35
36 #[doc(hidden)]
37 __Nonexhaustive(Void)
38}
39
40#[doc(hidden)]
41pub struct Void(());
42
43impl fmt::Debug for Void {
44 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
45 unreachable!()
46 }
47}
48
49impl fmt::Display for Error {
50 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51 match *self {
52 Utf8(ref e) => fmt::Display::fmt(e, f),
53 ref e => f.write_str(e.static_description()),
54 }
55 }
56}
57
58impl Error {
59 fn static_description(&self) -> &str {
60 match *self {
61 Method => "invalid Method specified",
62 Version => "invalid HTTP version specified",
63 Header => "invalid Header provided",
64 TooLarge => "message head is too large",
65 Status => "invalid Status provided",
66 Utf8(_) => "invalid UTF-8 string",
67 Error::__Nonexhaustive(..) => unreachable!(),
68 }
69 }
70}
71
72impl StdError for Error {
73 fn description(&self) -> &str {
74 self.static_description()
75 }
76
77 fn cause(&self) -> Option<&dyn StdError> {
78 match *self {
79 Utf8(ref error) => Some(error),
80 Error::__Nonexhaustive(..) => unreachable!(),
81 _ => None,
82 }
83 }
84}
85
86impl From<Utf8Error> for Error {
87 fn from(err: Utf8Error) -> Error {
88 Utf8(err)
89 }
90}
91
92impl From<FromUtf8Error> for Error {
93 fn from(err: FromUtf8Error) -> Error {
94 Utf8(err.utf8_error())
95 }
96}
97
98#[doc(hidden)]
99trait AssertSendSync: Send + Sync + 'static {}
100#[doc(hidden)]
101impl AssertSendSync for Error {}