Skip to main content

miko_core/
shared.rs

1use bytes::Bytes;
2use http_body_util::combinators::UnsyncBoxBody;
3use hyper::{Request, Response};
4use std::fmt;
5
6pub struct MikoError(pub Box<dyn std::error::Error + Send + Sync + 'static>);
7
8impl fmt::Debug for MikoError {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        fmt::Debug::fmt(&self.0, f)
11    }
12}
13
14impl fmt::Display for MikoError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        fmt::Display::fmt(&self.0, f)
17    }
18}
19
20impl std::error::Error for MikoError {
21    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
22        self.0.source()
23    }
24}
25
26impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for MikoError {
27    fn from(err: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
28        MikoError(err)
29    }
30}
31
32impl From<std::convert::Infallible> for MikoError {
33    fn from(err: std::convert::Infallible) -> Self {
34        match err {}
35    }
36}
37
38impl From<hyper::Error> for MikoError {
39    fn from(err: hyper::Error) -> Self {
40        MikoError(Box::new(err))
41    }
42}
43
44impl From<std::io::Error> for MikoError {
45    fn from(err: std::io::Error) -> Self {
46        MikoError(Box::new(err))
47    }
48}
49
50pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
51
52pub enum HTTPStatusCode {
53    OK = 200,
54    Created = 201,
55    Accepted = 202,
56    NoContent = 204,
57    BadRequest = 400,
58    Unauthorized = 401,
59    Forbidden = 403,
60    NotFound = 404,
61    MethodNotAllowed = 405,
62    NotAcceptable = 406,
63    ProxyAuthenticationRequired = 407,
64    RequestTimeout = 408,
65    Conflict = 409,
66    Gone = 410,
67    LengthRequired = 411,
68    PreconditionFailed = 412,
69    PayloadTooLarge = 413,
70    URITooLong = 414,
71    UnsupportedMediaType = 415,
72    RangeNotSatisfiable = 416,
73    ExpectationFailed = 417,
74    ImATeapot = 418,
75    MisdirectedRequest = 421,
76    UnprocessableEntity = 422,
77    Locked = 423,
78    FailedDependency = 424,
79    TooEarly = 425,
80    UpgradeRequired = 426,
81    PreconditionRequired = 428,
82    TooManyRequests = 429,
83    RequestHeaderFieldsTooLarge = 431,
84    UnavailableForLegalReasons = 451,
85    InternalServerError = 500,
86}
87
88pub type RespBody = UnsyncBoxBody<Bytes, MikoError>;
89pub type ReqBody = UnsyncBoxBody<Bytes, MikoError>;
90pub type Resp = Response<RespBody>;
91pub type Req = Request<ReqBody>;