hateoas/
status.rs

1#[skip_serializing_none]
2#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
3pub struct Status {
4    pub(crate) message: Option<String>,
5    pub(crate) code: Option<u32>,
6    pub(crate) http_status_code: Option<u16>,
7    pub(crate) session: Option<uuid::Uuid>,
8}
9
10impl Status {
11    pub fn new(
12        message: Option<&str>,
13        code: Option<u32>,
14        http_status_code: Option<u16>,
15        session: Option<uuid::Uuid>,
16    ) -> Self {
17        Status {
18            message: message.map(|t| t.to_string()),
19            code,
20            http_status_code,
21            session,
22        }
23    }
24
25    pub const fn const_new(
26        message: Option<String>,
27        code: Option<u32>,
28        http_status_code: Option<u16>,
29        session: Option<uuid::Uuid>,
30    ) -> Self {
31        Status {
32            message,
33            code,
34            http_status_code,
35            session,
36        }
37    }
38
39    /// ## Getting Message
40    /// This is for getting the message field from the status object.
41    ///
42    /// ```
43    /// use hateoas::Status;
44    ///
45    /// let mut status = Status::new(Some("hello world"), None, None, None);
46    ///
47    /// assert_eq!(status.message(), &Some("hello world".to_string()));
48    /// ```
49    pub fn message(&self) -> &Option<String> {
50        &self.message
51    }
52
53    /// ## Getting Mutable Message
54    /// This is for getting the message field from the status object.
55    ///
56    /// ```
57    /// use hateoas::Status;
58    ///
59    /// let mut status = Status::new(Some("hello world"), None, None, None);
60    ///
61    /// let mut mut_message = status.message_mut();
62    /// *mut_message = Some("Hello Space".to_string());
63    ///
64    /// assert_eq!(status.message(), &Some("Hello Space".to_string()));
65    /// ```
66    pub fn message_mut(&mut self) -> &mut Option<String> {
67        &mut self.message
68    }
69
70    /// ## Getting code
71    /// Getting the internal status code from the stauts object
72    ///
73    /// ```
74    /// use hateoas::Status;
75    ///
76    /// let mut status = Status::new(None, Some(200), None, None);
77    ///
78    /// assert_eq!(status.code(), &Some(200));
79    /// ```
80    pub fn code(&self) -> &Option<u32> {
81        &self.code
82    }
83
84    /// ## Getting Mutable Code
85    /// Getting the internal status code from the stauts object as a mutable reference
86    /// allowing for modifications to the internal status code.
87    ///
88    /// ```
89    /// use hateoas::Status;
90    ///
91    /// let mut status = Status::new(None, Some(200), None, None);
92    ///
93    /// let mut status_code = status.code_mut();
94    /// *status_code = Some(100);
95    ///
96    /// assert_eq!(status.code(), &Some(100));
97    /// ```
98    pub fn code_mut(&mut self) -> &mut Option<u32> {
99        &mut self.code
100    }
101
102    /// ## Getter for the HTTP status code
103    /// This is for getting the http_status_code.
104    ///
105    /// ```
106    /// use hateoas::Status;
107    ///
108    /// let mut status = Status::new(None, None, Some(200), None);
109    ///
110    /// assert_eq!(status.http_status_code(), &Some(200));
111    /// ```
112    pub fn http_status_code(&self) -> &Option<u16> {
113        &self.http_status_code
114    }
115
116    /// ## Getter for mutable HTTP status code
117    /// This is for getting the http_status_code.
118    ///
119    /// ```
120    /// use hateoas::Status;
121    ///
122    /// let mut status = Status::new(None, None, Some(200), None);
123    ///
124    /// let mut http_code = status.http_status_code_mut();
125    /// *http_code = Some(100);
126    ///
127    /// assert_eq!(status.http_status_code(), &Some(100));
128    /// ```
129    pub fn http_status_code_mut(&mut self) -> &mut Option<u16> {
130        &mut self.http_status_code
131    }
132
133    /// ## Getter for the Session
134    ///
135    /// ```
136    /// use hateoas::Status;
137    ///
138    /// let uuid = uuid::Uuid::new_v4();
139    /// let mut status = Status::new(None, None, None, Some(uuid));
140    ///
141    /// assert_eq!(status.session(), &Some(uuid));
142    /// ```
143    pub fn session(&self) -> &Option<uuid::Uuid> {
144        &self.session
145    }
146
147    /// ## Getter for mutable Session id
148    ///
149    /// ```
150    /// use hateoas::Status;
151    ///
152    /// let uuid = uuid::Uuid::new_v4();
153    /// let uuid_2 = uuid::Uuid::new_v4();
154    /// let mut status = Status::default();
155    ///
156    /// let mut mut_session = status.session_mut();
157    /// *mut_session = Some(uuid_2);
158    ///
159    /// assert_eq!(status.session(), &Some(uuid_2));
160    /// ```
161    pub fn session_mut(&mut self) -> &mut Option<uuid::Uuid> {
162        &mut self.session
163    }
164
165    pub fn get(
166        &self,
167    ) -> (
168        &Option<String>,
169        &Option<u32>,
170        &Option<u16>,
171        &Option<uuid::Uuid>,
172    ) {
173        (
174            &self.message,
175            &self.code,
176            &self.http_status_code,
177            &self.session,
178        )
179    }
180
181    pub fn get_mut(
182        &mut self,
183    ) -> (
184        &mut Option<String>,
185        &mut Option<u32>,
186        &mut Option<u16>,
187        &mut Option<uuid::Uuid>,
188    ) {
189        (
190            &mut self.message,
191            &mut self.code,
192            &mut self.http_status_code,
193            &mut self.session,
194        )
195    }
196}
197
198macro_rules! automated_status_codes {
199    (
200        $(
201            $(#[$docs:meta])*
202            ($num:expr, $konst:ident, $phrase:expr);
203        )+
204    ) => {
205        impl Status {
206        $(
207            $(#[$docs])*
208            #[doc = " ```\n" ]
209            #[doc = " use hateoas::Status;\n"]
210            #[doc = " \n" ]
211            #[doc = concat!(" let status = Status::", stringify!($konst), "();\n") ]
212            #[doc = " \n" ]
213            #[doc = concat!(" assert_eq!(status, Status::const_new(Some(", stringify!($phrase), ".to_string()), None, Some(", stringify!($num), "), None));\n") ]
214            #[doc = " ``` "]
215            #[allow(non_snake_case)]
216             pub fn $konst() -> Self {
217                Self::const_new(
218                    Some($phrase.to_string()),
219                    None,
220                    Some($num),
221                    None
222                )
223            }
224        )+
225        }
226    }
227}
228
229automated_status_codes! {
230    /// 100 Continue
231    /// [[RFC7231, Section 6.2.1](https://tools.ietf.org/html/rfc7231#section-6.2.1)]
232    (100, CONTINUE, "Continue");
233    /// 101 Switching Protocols
234    /// [[RFC7231, Section 6.2.2](https://tools.ietf.org/html/rfc7231#section-6.2.2)]
235    (101, SWITCHING_PROTOCOLS, "Switching Protocols");
236    /// 102 Processing
237    /// [[RFC2518](https://tools.ietf.org/html/rfc2518)]
238    (102, PROCESSING, "Processing");
239
240    /// 200 OK
241    /// [[RFC7231, Section 6.3.1](https://tools.ietf.org/html/rfc7231#section-6.3.1)]
242    (200, OK, "OK");
243    /// 201 Created
244    /// [[RFC7231, Section 6.3.2](https://tools.ietf.org/html/rfc7231#section-6.3.2)]
245    (201, CREATED, "Created");
246    /// 202 Accepted
247    /// [[RFC7231, Section 6.3.3](https://tools.ietf.org/html/rfc7231#section-6.3.3)]
248    (202, ACCEPTED, "Accepted");
249    /// 203 Non-Authoritative Information
250    /// [[RFC7231, Section 6.3.4](https://tools.ietf.org/html/rfc7231#section-6.3.4)]
251    (203, NON_AUTHORITATIVE_INFORMATION, "Non Authoritative Information");
252    /// 204 No Content
253    /// [[RFC7231, Section 6.3.5](https://tools.ietf.org/html/rfc7231#section-6.3.5)]
254    (204, NO_CONTENT, "No Content");
255    /// 205 Reset Content
256    /// [[RFC7231, Section 6.3.6](https://tools.ietf.org/html/rfc7231#section-6.3.6)]
257    (205, RESET_CONTENT, "Reset Content");
258    /// 206 Partial Content
259    /// [[RFC7233, Section 4.1](https://tools.ietf.org/html/rfc7233#section-4.1)]
260    (206, PARTIAL_CONTENT, "Partial Content");
261    /// 207 Multi-Status
262    /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
263    (207, MULTI_STATUS, "Multi-Status");
264    /// 208 Already Reported
265    /// [[RFC5842](https://tools.ietf.org/html/rfc5842)]
266    (208, ALREADY_REPORTED, "Already Reported");
267
268    /// 226 IM Used
269    /// [[RFC3229](https://tools.ietf.org/html/rfc3229)]
270    (226, IM_USED, "IM Used");
271
272    /// 300 Multiple Choices
273    /// [[RFC7231, Section 6.4.1](https://tools.ietf.org/html/rfc7231#section-6.4.1)]
274    (300, MULTIPLE_CHOICES, "Multiple Choices");
275    /// 301 Moved Permanently
276    /// [[RFC7231, Section 6.4.2](https://tools.ietf.org/html/rfc7231#section-6.4.2)]
277    (301, MOVED_PERMANENTLY, "Moved Permanently");
278    /// 302 Found
279    /// [[RFC7231, Section 6.4.3](https://tools.ietf.org/html/rfc7231#section-6.4.3)]
280    (302, FOUND, "Found");
281    /// 303 See Other
282    /// [[RFC7231, Section 6.4.4](https://tools.ietf.org/html/rfc7231#section-6.4.4)]
283    (303, SEE_OTHER, "See Other");
284    /// 304 Not Modified
285    /// [[RFC7232, Section 4.1](https://tools.ietf.org/html/rfc7232#section-4.1)]
286    (304, NOT_MODIFIED, "Not Modified");
287    /// 305 Use Proxy
288    /// [[RFC7231, Section 6.4.5](https://tools.ietf.org/html/rfc7231#section-6.4.5)]
289    (305, USE_PROXY, "Use Proxy");
290    /// 307 Temporary Redirect
291    /// [[RFC7231, Section 6.4.7](https://tools.ietf.org/html/rfc7231#section-6.4.7)]
292    (307, TEMPORARY_REDIRECT, "Temporary Redirect");
293    /// 308 Permanent Redirect
294    /// [[RFC7238](https://tools.ietf.org/html/rfc7238)]
295    (308, PERMANENT_REDIRECT, "Permanent Redirect");
296
297    /// 400 Bad Request
298    /// [[RFC7231, Section 6.5.1](https://tools.ietf.org/html/rfc7231#section-6.5.1)]
299    (400, BAD_REQUEST, "Bad Request");
300    /// 401 Unauthorized
301    /// [[RFC7235, Section 3.1](https://tools.ietf.org/html/rfc7235#section-3.1)]
302    (401, UNAUTHORIZED, "Unauthorized");
303    /// 402 Payment Required
304    /// [[RFC7231, Section 6.5.2](https://tools.ietf.org/html/rfc7231#section-6.5.2)]
305    (402, PAYMENT_REQUIRED, "Payment Required");
306    /// 403 Forbidden
307    /// [[RFC7231, Section 6.5.3](https://tools.ietf.org/html/rfc7231#section-6.5.3)]
308    (403, FORBIDDEN, "Forbidden");
309    /// 404 Not Found
310    /// [[RFC7231, Section 6.5.4](https://tools.ietf.org/html/rfc7231#section-6.5.4)]
311    (404, NOT_FOUND, "Not Found");
312    /// 405 Method Not Allowed
313    /// [[RFC7231, Section 6.5.5](https://tools.ietf.org/html/rfc7231#section-6.5.5)]
314    (405, METHOD_NOT_ALLOWED, "Method Not Allowed");
315    /// 406 Not Acceptable
316    /// [[RFC7231, Section 6.5.6](https://tools.ietf.org/html/rfc7231#section-6.5.6)]
317    (406, NOT_ACCEPTABLE, "Not Acceptable");
318    /// 407 Proxy Authentication Required
319    /// [[RFC7235, Section 3.2](https://tools.ietf.org/html/rfc7235#section-3.2)]
320    (407, PROXY_AUTHENTICATION_REQUIRED, "Proxy Authentication Required");
321    /// 408 Request Timeout
322    /// [[RFC7231, Section 6.5.7](https://tools.ietf.org/html/rfc7231#section-6.5.7)]
323    (408, REQUEST_TIMEOUT, "Request Timeout");
324    /// 409 Conflict
325    /// [[RFC7231, Section 6.5.8](https://tools.ietf.org/html/rfc7231#section-6.5.8)]
326    (409, CONFLICT, "Conflict");
327    /// 410 Gone
328    /// [[RFC7231, Section 6.5.9](https://tools.ietf.org/html/rfc7231#section-6.5.9)]
329    (410, GONE, "Gone");
330    /// 411 Length Required
331    /// [[RFC7231, Section 6.5.10](https://tools.ietf.org/html/rfc7231#section-6.5.10)]
332    (411, LENGTH_REQUIRED, "Length Required");
333    /// 412 Precondition Failed
334    /// [[RFC7232, Section 4.2](https://tools.ietf.org/html/rfc7232#section-4.2)]
335    (412, PRECONDITION_FAILED, "Precondition Failed");
336    /// 413 Payload Too Large
337    /// [[RFC7231, Section 6.5.11](https://tools.ietf.org/html/rfc7231#section-6.5.11)]
338    (413, PAYLOAD_TOO_LARGE, "Payload Too Large");
339    /// 414 URI Too Long
340    /// [[RFC7231, Section 6.5.12](https://tools.ietf.org/html/rfc7231#section-6.5.12)]
341    (414, URI_TOO_LONG, "URI Too Long");
342    /// 415 Unsupported Media Type
343    /// [[RFC7231, Section 6.5.13](https://tools.ietf.org/html/rfc7231#section-6.5.13)]
344    (415, UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type");
345    /// 416 Range Not Satisfiable
346    /// [[RFC7233, Section 4.4](https://tools.ietf.org/html/rfc7233#section-4.4)]
347    (416, RANGE_NOT_SATISFIABLE, "Range Not Satisfiable");
348    /// 417 Expectation Failed
349    /// [[RFC7231, Section 6.5.14](https://tools.ietf.org/html/rfc7231#section-6.5.14)]
350    (417, EXPECTATION_FAILED, "Expectation Failed");
351    /// 418 I'm a teapot
352    /// [curiously not registered by IANA but [RFC2324](https://tools.ietf.org/html/rfc2324)]
353    (418, IM_A_TEAPOT, "I'm a teapot");
354
355    /// 421 Misdirected Request
356    /// [RFC7540, Section 9.1.2](http://tools.ietf.org/html/rfc7540#section-9.1.2)
357    (421, MISDIRECTED_REQUEST, "Misdirected Request");
358    /// 422 Unprocessable Entity
359    /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
360    (422, UNPROCESSABLE_ENTITY, "Unprocessable Entity");
361    /// 423 Locked
362    /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
363    (423, LOCKED, "Locked");
364    /// 424 Failed Dependency
365    /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
366    (424, FAILED_DEPENDENCY, "Failed Dependency");
367
368    /// 426 Upgrade Required
369    /// [[RFC7231, Section 6.5.15](https://tools.ietf.org/html/rfc7231#section-6.5.15)]
370    (426, UPGRADE_REQUIRED, "Upgrade Required");
371
372    /// 428 Precondition Required
373    /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
374    (428, PRECONDITION_REQUIRED, "Precondition Required");
375    /// 429 Too Many Requests
376    /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
377    (429, TOO_MANY_REQUESTS, "Too Many Requests");
378
379    /// 431 Request Header Fields Too Large
380    /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
381    (431, REQUEST_HEADER_FIELDS_TOO_LARGE, "Request Header Fields Too Large");
382
383    /// 451 Unavailable For Legal Reasons
384    /// [[RFC7725](http://tools.ietf.org/html/rfc7725)]
385    (451, UNAVAILABLE_FOR_LEGAL_REASONS, "Unavailable For Legal Reasons");
386
387    /// 500 Internal Server Error
388    /// [[RFC7231, Section 6.6.1](https://tools.ietf.org/html/rfc7231#section-6.6.1)]
389    (500, INTERNAL_SERVER_ERROR, "Internal Server Error");
390    /// 501 Not Implemented
391    /// [[RFC7231, Section 6.6.2](https://tools.ietf.org/html/rfc7231#section-6.6.2)]
392    (501, NOT_IMPLEMENTED, "Not Implemented");
393    /// 502 Bad Gateway
394    /// [[RFC7231, Section 6.6.3](https://tools.ietf.org/html/rfc7231#section-6.6.3)]
395    (502, BAD_GATEWAY, "Bad Gateway");
396    /// 503 Service Unavailable
397    /// [[RFC7231, Section 6.6.4](https://tools.ietf.org/html/rfc7231#section-6.6.4)]
398    (503, SERVICE_UNAVAILABLE, "Service Unavailable");
399    /// 504 Gateway Timeout
400    /// [[RFC7231, Section 6.6.5](https://tools.ietf.org/html/rfc7231#section-6.6.5)]
401    (504, GATEWAY_TIMEOUT, "Gateway Timeout");
402    /// 505 HTTP Version Not Supported
403    /// [[RFC7231, Section 6.6.6](https://tools.ietf.org/html/rfc7231#section-6.6.6)]
404    (505, HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported");
405    /// 506 Variant Also Negotiates
406    /// [[RFC2295](https://tools.ietf.org/html/rfc2295)]
407    (506, VARIANT_ALSO_NEGOTIATES, "Variant Also Negotiates");
408    /// 507 Insufficient Storage
409    /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
410    (507, INSUFFICIENT_STORAGE, "Insufficient Storage");
411    /// 508 Loop Detected
412    /// [[RFC5842](https://tools.ietf.org/html/rfc5842)]
413    (508, LOOP_DETECTED, "Loop Detected");
414
415    /// 510 Not Extended
416    /// [[RFC2774](https://tools.ietf.org/html/rfc2774)]
417    (510, NOT_EXTENDED, "Not Extended");
418    /// 511 Network Authentication Required
419    /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
420    (511, NETWORK_AUTHENTICATION_REQUIRED, "Network Authentication Required");
421}