1use std::fmt::Display;
2
3use crate::rpc::{Code, HttpHeader, HttpRequest, HttpResponse};
4
5impl HttpRequest {
6 has_impl!(method);
7 has_impl!(uri);
8
9 pub fn has_header(&self, header: &HttpHeader) -> bool {
11 self.headers.contains(header)
12 }
13}
14
15impl HttpResponse {
16 pub fn has_status(&self, status: i32) -> bool {
18 self.status == status
19 }
20
21 pub fn has_header(&self, header: &HttpHeader) -> bool {
23 self.headers.contains(header)
24 }
25
26 has_impl!(reason);
27}
28
29impl Code {
30 pub fn is_200_ok(&self) -> bool {
32 matches!(self, Self::Ok)
33 }
34
35 pub fn is_cancelled(&self) -> bool {
37 matches!(self, Self::Cancelled)
38 }
39
40 pub fn is_unknown(&self) -> bool {
42 matches!(self, Self::Unknown)
43 }
44
45 pub fn is_invalid_argument(&self) -> bool {
47 matches!(self, Self::InvalidArgument)
48 }
49
50 pub fn is_deadline_exceeded(&self) -> bool {
52 matches!(self, Self::DeadlineExceeded)
53 }
54
55 pub fn is_not_found(&self) -> bool {
57 matches!(self, Self::NotFound)
58 }
59
60 pub fn is_already_exists(&self) -> bool {
62 matches!(self, Self::AlreadyExists)
63 }
64
65 pub fn is_permission_denied(&self) -> bool {
67 matches!(self, Self::PermissionDenied)
68 }
69
70 pub fn is_unauthenticated(&self) -> bool {
72 matches!(self, Self::Unauthenticated)
73 }
74
75 pub fn is_resource_exhausted(&self) -> bool {
77 matches!(self, Self::ResourceExhausted)
78 }
79
80 pub fn is_failed_precondition(&self) -> bool {
82 matches!(self, Self::FailedPrecondition)
83 }
84
85 pub fn is_aborted(&self) -> bool {
87 matches!(self, Self::Aborted)
88 }
89
90 pub fn is_out_of_range(&self) -> bool {
92 matches!(self, Self::OutOfRange)
93 }
94
95 pub fn is_unimplemented(&self) -> bool {
97 matches!(self, Self::Unimplemented)
98 }
99
100 pub fn is_internal(&self) -> bool {
102 matches!(self, Self::Internal)
103 }
104
105 pub fn is_unavailable(&self) -> bool {
107 matches!(self, Self::Unavailable)
108 }
109
110 pub fn is_data_loss(&self) -> bool {
112 matches!(self, Self::DataLoss)
113 }
114
115 pub fn as_title_case(&self) -> &str {
117 match self {
118 Code::Ok => "Ok",
119 Code::Cancelled => "Cancelled",
120 Code::Unknown => "Unknown",
121 Code::InvalidArgument => "Invalid Argument",
122 Code::DeadlineExceeded => "Deadline Exceeded",
123 Code::NotFound => "Not Found",
124 Code::AlreadyExists => "Already Exists",
125 Code::PermissionDenied => "Permission Denied",
126 Code::Unauthenticated => "Unauthenticated",
127 Code::ResourceExhausted => "Resource Exhausted",
128 Code::FailedPrecondition => "Failed Precondition",
129 Code::Aborted => "Aborted",
130 Code::OutOfRange => "Out Of Range",
131 Code::Unimplemented => "Unimplemented",
132 Code::Internal => "Internal",
133 Code::Unavailable => "Unavailable",
134 Code::DataLoss => "Data Loss",
135 }
136 }
137
138 pub fn to_http_status(&self) -> u16 {
140 match self {
141 Code::Ok => 200,
142 Code::Cancelled => 499,
143 Code::Unknown => 500,
144 Code::InvalidArgument => 400,
145 Code::DeadlineExceeded => 504,
146 Code::NotFound => 404,
147 Code::AlreadyExists => 409,
148 Code::PermissionDenied => 403,
149 Code::Unauthenticated => 401,
150 Code::ResourceExhausted => 429,
151 Code::FailedPrecondition => 400,
152 Code::Aborted => 409,
153 Code::OutOfRange => 400,
154 Code::Unimplemented => 501,
155 Code::Internal => 500,
156 Code::Unavailable => 503,
157 Code::DataLoss => 500,
158 }
159 }
160}
161
162impl Display for Code {
163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164 write!(f, "{}", self.as_title_case())
165 }
166}