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
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusCode {
    Undefined = 900,

    FetchFailed = 901,
    FetchTimeout = 902,
    DecodeFailed = 903,

    Ok = 200,
    Created = 201,
    NoContent = 204,

    NotModified = 304,

    BadRequest = 400,
    Unauthorized = 401,
    Forbidden = 403,
    NotFound = 404,
    MethodNotAllowed = 405,
    PayloadTooBig = 413,
    UnsupportedMediaType = 415,
    RateLimited = 429,

    InternalServerError = 500,
    NotImplemented = 501,
}

impl StatusCode {
    pub fn is_success(&self) -> bool {
        matches!(
            self,
            Self::Ok | Self::Created | Self::NoContent | Self::NotModified
        )
    }

    pub fn is_failure(&self) -> bool {
        !self.is_success()
    }

    pub fn is_local(&self) -> bool {
        matches!(self, Self::FetchFailed | Self::FetchTimeout)
    }
}

impl From<bool> for StatusCode {
    fn from(success: bool) -> Self {
        if success {
            StatusCode::Ok
        } else {
            StatusCode::BadRequest
        }
    }
}

impl From<u16> for StatusCode {
    fn from(code: u16) -> Self {
        match code {
            200 => Self::Ok,
            201 => Self::Created,
            204 => Self::NoContent,
            304 => Self::NotModified,
            400 => Self::BadRequest,
            401 => Self::Unauthorized,
            403 => Self::Forbidden,
            404 => Self::NotFound,
            405 => Self::MethodNotAllowed,
            413 => Self::PayloadTooBig,
            415 => Self::UnsupportedMediaType,
            429 => Self::RateLimited,
            500 => Self::InternalServerError,
            501 => Self::NotImplemented,
            901 => Self::FetchFailed,
            902 => Self::FetchTimeout,
            903 => Self::DecodeFailed,
            _ => Self::Undefined,
        }
    }
}