simbld_http/responses/
success.rs

1/// The above Rust code defines an enum representing HTTP response success codes with associated descriptions and provides helper functions to retrieve code-description pairs.
2use crate::helpers::{from_u16_helper::FromU16, to_u16_helper::ToU16};
3use num_enum::{IntoPrimitive, TryFromPrimitive};
4use strum::EnumProperty;
5use strum_macros::{Display, EnumIter, EnumProperty};
6
7#[derive(
8  Display, IntoPrimitive, TryFromPrimitive, EnumProperty, EnumIter, Debug, Copy, Clone, PartialEq,
9)]
10#[repr(u16)]
11
12pub enum ResponsesSuccessCodes {
13  #[strum(props(
14    Description = "Request processed successfully. Response will depend on the request method used, and the result will be either a representation of the requested resource or an empty response"
15  ))]
16  Ok = 200,
17  #[strum(props(
18    Description = "Request processed successfully and document created, with a new resource created, and the URI of the new resource returned, if available"
19  ))]
20  Created = 201,
21  #[strum(props(
22    Description = "Request processed, but with no guarantee of results, and no indication of the final status of the request, which will be processed asynchronously, such as a request to create a new resource"
23  ))]
24  Accepted = 202,
25  #[strum(props(
26    Description = "Information returned, but generated by an uncertified source, such as a proxy server, rather than the origin server, and may be incorrect, outdated, or otherwise unreliable"
27  ))]
28  NonAuthoritativeInformation = 203,
29  #[strum(props(
30    Description = "Request processed successfully but no information to return, and the response body is empty, useful as a header for a DELETE request, indicating that the resource has been deleted"
31  ))]
32  NoContent = 204,
33  #[strum(props(
34    Description = "Request processed successfully, current page can be deleted, and the client should reset the document view, useful as a header for a form submission, indicating that the form has been processed successfully"
35  ))]
36  ResetContent = 205,
37  #[strum(props(
38    Description = "Only part of the resource was transmitted, as the request used the Range header to retrieve a specific portion of the resource, and the response contains the requested range, or the server is unable to return the entire resource"
39  ))]
40  PartialContent = 206,
41  #[strum(props(
42    Description = "Multiple status responses, with a separate response code for each part of the request, and the response body contains XML that describes the status of each part of the request, useful for WebDAV RFC 4918"
43  ))]
44  MultiStatus = 207,
45  #[strum(props(
46    Description = "A WebDAV binding has been enumerated in a previous http code 207 and are not included here again, useful for WebDAV RFC 5842"
47  ))]
48  AlreadyReported = 208,
49  #[strum(props(
50    Description = "The client-side copy of the resource differs from the server-side copy (content or properties), the content of the response has a different meaning depending on the media type that is returned, and the response body may contain a representation of the requested resource, or some instructions on how to process the request"
51  ))]
52  ContentDifferent = 210,
53  #[strum(props(
54    Description = "The response provides a URL for accessing a resource that is the result of the requested action"
55  ))]
56  ContentLocation = 211,
57  #[strum(props(
58    Description = "The response contains the representation of an object’s data, and the response body contains the data of the object, such as a JSON object or XML document, and the response body may contain the requested resource"
59  ))]
60  ObjectData = 212,
61  #[strum(props(
62    Description = "The response indicates multiple instances of the requested resource exist, each with its own set of properties, and the response body contains an array of resources, each with its own set of properties"
63  ))]
64  MultipleResourceInstances = 213,
65  #[strum(props(
66    Description = "The response represents the result of a transformation or conversion applied to the resource, and the response body contains the transformed resource, such as a transcoded media file, or a formatted document"
67  ))]
68  TransformApplied = 214,
69  #[strum(props(
70    Description = "The requested resource has been deleted, and the response body contains the status of the deletion, and the response body may contain the requested resource"
71  ))]
72  ContentDeleted = 215,
73  #[strum(props(
74    Description = "The server has completed the resource request, responded to a POST request, and the response is a representation of the result of one or more instance manipulations applied to the current instance"
75  ))]
76  IMUsedPostRequest = 216,
77  #[strum(props(
78    Description = "The response contains the result of a partial modification to the resource, and the response body contains the modified resource, such as a JSON patch document or a binary diff, the response is a delta encoding of the requested resource, containing only the changes between the current and previous versions"
79  ))]
80  DeltaEncodingApplied = 217,
81  #[strum(props(
82    Description = "Everything is fine, and the response body contains a humorous or playful message, indicating that the server is aware of the situation and is not concerned, The server is returning this response to indicate that everything is working as expected, even though the situation may be unusual or unexpected, apache, unofficial"
83  ))]
84  ThisIsFine = 218,
85  #[strum(props(
86    Description = "The response contains the transferred content, and the response body contains the content that was transferred, such as a file or document, and the response body may contain the requested resource, the response indicates that the content has been transferred successfully to another instance, thus ending the current instance"
87  ))]
88  ContentTransferred = 219,
89  #[strum(props(
90    Description = "The server has started a load balancer, and the response body contains the status of the load balancer, indicating that the server has initiated a load balancer to distribute incoming requests across multiple servers, the server response is sent by a load balancer to notify the client that a new server load balancing process has started"
91  ))]
92  LoadBalancerStarted = 220,
93  #[strum(props(
94    Description = "The server has stopped a load balancer, and the response body contains the status of the load balancer, indicating that the server has terminated a load balancer process, the server response is sent by a load balancer to notify the client that the server load balancing process has ended, the server response is sent by a load balancer to notify the client that the server load balancing process has ended"
95  ))]
96  LoadBalancerEnded = 221,
97  #[strum(props(
98    Description = "The client authentication was successful, and the response body contains the authentication token or session information, indicating that the client has been successfully authenticated by the server, and the response body may contain the authentication token or session information"
99  ))]
100  AuthenticationSuccessful = 222,
101  #[strum(props(
102    Description = "The server has completed the resource request, responded to a GET request, and the response is a representation of the current instance, indicating that the server has completed the resource request and responded to a GET request, and the response body contains the current instance of the resource"
103  ))]
104  IMUsedGetRequest = 226,
105  #[strum(props(
106    Description = "The server is running low on storage space, and the response body contains the status of the storage space, indicating that the server is running low on storage space, and the response body may contain the status of the storage space, the server is temporarily unable to store the representation needed to complete the request."
107  ))]
108  LowOnStorageSpace = 250,
109  #[strum(props(
110    Description = "The server has recognized the request but cannot process it, and the response body contains the status of the request, indicating that the server has recognized the request but cannot process it, and the response body may contain the status of the request, the server is unable to process the request due to constraints or limitations, the server cannot produce a response that satisfies the range specified in the request’s Range header field"
111  ))]
112  EntityRecognizedNotProcessable = 252,
113  #[strum(props(
114    Description = "The resource is locked and cannot be accessed or modified, and the response body contains the status of the resource, indicating that the resource is locked and cannot be accessed or modified, and the response body may contain the status of the resource, the server has locked the resource to prevent access or modification"
115  ))]
116  ResourceAccessedLocked = 253,
117  #[strum(props(
118    Description = "The server does not recognize the request method or lacks the capability to fulfill it, and the response body contains the status of the request, indicating that the server does not recognize the request method or lacks the capability to fulfill it, and the response body may contain the status of the request, the server is unable to process the request due to an unsupported method"
119  ))]
120  MethodNotFound = 254,
121  #[strum(props(
122    Description = "The server has returned an extended status code, and the response body contains the extended status code, indicating that the server has returned an extended status code, and the response body may contain the extended status code, the server has provided additional information or context in the response"
123  ))]
124  ExtendedCode = 255,
125  #[strum(props(
126    Description = "The server has returned a miscellaneous persistent warning, and the response body contains the warning message, indicating that the server has returned a miscellaneous persistent warning, and the response body may contain the warning message, the server has encountered a warning condition that is not covered by other status codes"
127  ))]
128  MiscellaneousPersistentWarningStart = 299,
129}
130
131impl ToU16 for ResponsesSuccessCodes {
132  fn to_u16(self) -> u16 {
133    self.into() // Conversion`Into<u16>`
134  }
135}
136
137impl FromU16 for ResponsesSuccessCodes {
138  fn from_u16(code: u16) -> Option<Self> {
139    Self::try_from(code).ok() // Conversion`TryFrom<u16>`
140  }
141}
142
143impl Into<(u16, &'static str)> for ResponsesSuccessCodes {
144  fn into(self) -> (u16, &'static str) {
145    let code: u16 = self.to_u16();
146    let description = self.get_str("Description").unwrap_or("No description");
147    (code, description) // Tuple
148  }
149}
150
151pub fn ok() -> (u16, &'static str) {
152  (200, "Ok")
153}
154
155pub fn created() -> (u16, &'static str) {
156  (201, "Created")
157}
158
159pub fn accepted() -> (u16, &'static str) {
160  (202, "Accepted")
161}
162
163pub fn non_authoritative_information() -> (u16, &'static str) {
164  (203, "Non-authoritative information")
165}
166
167pub fn no_content() -> (u16, &'static str) {
168  (204, "No content")
169}
170
171pub fn reset_content() -> (u16, &'static str) {
172  (205, "Reset content")
173}
174
175pub fn partial_content() -> (u16, &'static str) {
176  (206, "Partial content")
177}
178
179pub fn multi_status() -> (u16, &'static str) {
180  (207, "Multi-status")
181}
182
183pub fn already_reported() -> (u16, &'static str) {
184  (208, "Already reported")
185}
186
187pub fn content_different() -> (u16, &'static str) {
188  (210, "Content different")
189}
190
191pub fn content_location() -> (u16, &'static str) {
192  (211, "Content location")
193}
194
195pub fn object_data() -> (u16, &'static str) {
196  (212, "Object data")
197}
198
199pub fn multiple_resource_instances() -> (u16, &'static str) {
200  (213, "Multiple resource instances")
201}
202
203pub fn transform_applied() -> (u16, &'static str) {
204  (214, "Transform applied")
205}
206
207pub fn content_deleted() -> (u16, &'static str) {
208  (215, "Content deleted")
209}
210
211pub fn im_used_post_request() -> (u16, &'static str) {
212  (216, "IM used post request")
213}
214
215pub fn delta_encoding_applied() -> (u16, &'static str) {
216  (217, "Delta encoding applied")
217}
218
219pub fn this_is_fine() -> (u16, &'static str) {
220  (218, "This is fine")
221}
222
223pub fn content_transferred() -> (u16, &'static str) {
224  (219, "Content transferred")
225}
226
227pub fn load_balancer_started() -> (u16, &'static str) {
228  (220, "Load balancer started")
229}
230
231pub fn load_balancer_ended() -> (u16, &'static str) {
232  (221, "Load balancer ended")
233}
234
235pub fn authentication_successful() -> (u16, &'static str) {
236  (222, "Authentication successful")
237}
238
239pub fn im_used_get_request() -> (u16, &'static str) {
240  (226, "IM used get request")
241}
242
243pub fn low_on_storage_space() -> (u16, &'static str) {
244  (250, "Low on storage space")
245}
246
247pub fn entity_recognized_not_processable() -> (u16, &'static str) {
248  (252, "Entity recognized not processable")
249}
250
251pub fn resource_accessed_locked() -> (u16, &'static str) {
252  (253, "Resource accessed locked")
253}
254
255pub fn method_not_found() -> (u16, &'static str) {
256  (254, "Method not found")
257}
258
259pub fn extended_code() -> (u16, &'static str) {
260  (255, "Extended code")
261}
262
263pub fn miscellaneous_persistent_warning_start() -> (u16, &'static str) {
264  (299, "Miscellaneous persistent warning start")
265}
266
267#[cfg(test)]
268mod tests {
269  use super::*;
270
271  #[test]
272  fn test_generated_functions_success() {
273    let response = ResponsesSuccessCodes::Ok;
274    let (code, description): (u16, &str) = response.into();
275    assert_eq!(code, 200);
276    assert_eq!(
277      description,
278      "Request processed successfully. Response will depend on the request method used, and the result will be either a representation of the requested resource or an empty response"
279    );
280  }
281
282  #[test]
283  fn test_to_u16_success() {
284    let response = ResponsesSuccessCodes::Created;
285    let code = response.to_u16();
286    assert_eq!(code, 201);
287  }
288
289  #[test]
290  fn test_ok() {
291    let response = accepted();
292    assert_eq!(response, (202, "Accepted"));
293  }
294
295  #[test]
296  fn test_from_u16_no_content() {
297    let response = ResponsesSuccessCodes::from_u16(204);
298    assert_eq!(response, Some(ResponsesSuccessCodes::NoContent));
299  }
300}