proto_types/rpc/
http.rs

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  /// Returns true if the [`HttpRequest`] contains the given header.
10  pub fn has_header(&self, header: &HttpHeader) -> bool {
11    self.headers.contains(header)
12  }
13}
14
15impl HttpResponse {
16  /// Returns true if the `status` matches the argument.
17  pub fn has_status(&self, status: i32) -> bool {
18    self.status == status
19  }
20
21  /// Returns true if the [`HttpResponse`] contains the given header.
22  pub fn has_header(&self, header: &HttpHeader) -> bool {
23    self.headers.contains(header)
24  }
25
26  has_impl!(reason);
27}
28
29impl Code {
30  /// Checks if the code is `Ok`.
31  pub fn is_200_ok(&self) -> bool {
32    matches!(self, Self::Ok)
33  }
34
35  /// Checks if the code is `Cancelled`.
36  pub fn is_cancelled(&self) -> bool {
37    matches!(self, Self::Cancelled)
38  }
39
40  /// Checks if the code is `Unknown`.
41  pub fn is_unknown(&self) -> bool {
42    matches!(self, Self::Unknown)
43  }
44
45  /// Checks if the code is `InvalidArgument`.
46  pub fn is_invalid_argument(&self) -> bool {
47    matches!(self, Self::InvalidArgument)
48  }
49
50  /// Checks if the code is `DeadlineExceeded`.
51  pub fn is_deadline_exceeded(&self) -> bool {
52    matches!(self, Self::DeadlineExceeded)
53  }
54
55  /// Checks if the code is `NotFound`.
56  pub fn is_not_found(&self) -> bool {
57    matches!(self, Self::NotFound)
58  }
59
60  /// Checks if the code is `AlreadyExists`.
61  pub fn is_already_exists(&self) -> bool {
62    matches!(self, Self::AlreadyExists)
63  }
64
65  /// Checks if the code is `PermissionDenied`.
66  pub fn is_permission_denied(&self) -> bool {
67    matches!(self, Self::PermissionDenied)
68  }
69
70  /// Checks if the code is `Unauthenticated`.
71  pub fn is_unauthenticated(&self) -> bool {
72    matches!(self, Self::Unauthenticated)
73  }
74
75  /// Checks if the code is `ResourceExhausted`.
76  pub fn is_resource_exhausted(&self) -> bool {
77    matches!(self, Self::ResourceExhausted)
78  }
79
80  /// Checks if the code is `FailedPrecondition`.
81  pub fn is_failed_precondition(&self) -> bool {
82    matches!(self, Self::FailedPrecondition)
83  }
84
85  /// Checks if the code is `Aborted`.
86  pub fn is_aborted(&self) -> bool {
87    matches!(self, Self::Aborted)
88  }
89
90  /// Checks if the code is `OutOfRange`.
91  pub fn is_out_of_range(&self) -> bool {
92    matches!(self, Self::OutOfRange)
93  }
94
95  /// Checks if the code is `Unimplemented`.
96  pub fn is_unimplemented(&self) -> bool {
97    matches!(self, Self::Unimplemented)
98  }
99
100  /// Checks if the code is `Internal`.
101  pub fn is_internal(&self) -> bool {
102    matches!(self, Self::Internal)
103  }
104
105  /// Checks if the code is `Unavailable`.
106  pub fn is_unavailable(&self) -> bool {
107    matches!(self, Self::Unavailable)
108  }
109
110  /// Checks if the code is `DataLoss`.
111  pub fn is_data_loss(&self) -> bool {
112    matches!(self, Self::DataLoss)
113  }
114
115  /// Returns the name of the code variant in title case.
116  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  /// Returns the corresponding HTTP status code mapping.
139  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}