simbld_http/helpers/unified_tuple_helper.rs
1//! # Unified HTTP Response Representation
2//!
3//! This module provides the `UnifiedTuple` structure which offers a standardized
4//! way to represent HTTP responses across the application.
5//!
6//! The `UnifiedTuple` combines standard HTTP status information with optional
7//! application-specific internal codes and names, enabling consistent handling
8//! of both standard HTTP responses and custom application responses.
9//!
10//! ## Example
11//!
12//! ```rust
13//! use simbld_http::helpers::unified_tuple_helper::UnifiedTuple;
14//!
15//! let tuple = UnifiedTuple {
16//! standard_code: 200,
17//! standard_name: "OK",
18//! unified_description: "Successful request",
19//! internal_code: None,
20//! internal_name: None,
21//! };
22//!
23//! // Convert to JSON for serialization
24//! let json = tuple.as_json();
25//! assert_eq!(json["description"], "Successful request");
26//! ```
27
28use crate::responses::ResponsesTypes;
29use serde::Serialize;
30
31/// A standardized representation of HTTP response codes and metadata.
32///
33/// This structure provides a unified way to represent both standard HTTP
34/// response codes and application-specific internal codes, making it easier
35/// to handle responses consistently throughout the application.
36///
37#[derive(Debug, PartialEq, Serialize)]
38pub struct UnifiedTuple {
39 /// Standard HTTP code.
40 pub standard_code: u16,
41 /// Standard HTTP name.
42 pub standard_name: &'static str,
43 /// Description of the response.
44 pub unified_description: &'static str,
45 /// Internal HTTP code (None if equal to standard code).
46 pub internal_code: Option<u16>,
47 /// Internal HTTP name (None if equal to standard name).
48 pub internal_name: Option<&'static str>,
49}
50
51impl UnifiedTuple {
52 /// Converts the UnifiedTuple to a structured JSON representation.
53 ///
54 /// The resulting JSON structure includes:
55 /// - Standard HTTP code information (code and name)
56 /// - Description text
57 /// - Internal HTTP code information if available (code and name)
58 ///
59 /// # Returns
60 ///
61 /// A `serde_json::Value` containing the structured JSON representation
62 ///
63 pub fn as_json(&self) -> serde_json::Value {
64 serde_json::json!({
65 "standard http code": {
66 "code": self.standard_code,
67 "name": self.standard_name,
68 },
69 "description": self.unified_description,
70 "internal http code": {
71 "code": self.internal_code,
72 "name": self.internal_name,
73 },
74 })
75 }
76
77 pub fn new(
78 standard_code: u16,
79 standard_name: &'static str,
80 unified_description: &'static str,
81 internal_code: u16,
82 internal_name: &'static str,
83 ) -> Self {
84 Self {
85 standard_code,
86 standard_name,
87 unified_description,
88 internal_code: Some(internal_code),
89 internal_name: Some(internal_name),
90 }
91 }
92}
93
94/// Implements automatic conversion from ResponsesTypes to UnifiedTuple.
95///
96/// This allows for seamless integration between the enum-based response types
97/// and the structured tuple representation.
98///
99impl From<ResponsesTypes> for UnifiedTuple {
100 fn from(response: ResponsesTypes) -> Self {
101 let standard_code = response.get_code();
102 let standard_name = response.get_description();
103 let unified_description = response.get_description();
104 UnifiedTuple {
105 standard_code,
106 standard_name,
107 unified_description,
108 internal_code: None,
109 internal_name: None,
110 }
111 }
112}