1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::Serializable;
/// A serializer for building JSON-like string representations.
///
/// This struct provides methods to construct a serialized string
/// with key-value pairs, following a JSON-like format.
pub struct Serializer {
/// The output string being constructed.
pub(crate) out: String,
}
impl Serializer {
/// Creates a new `Serializer` instance with an initial opening brace `{`.
///
/// # Returns
///
/// A new `Serializer` instance.
pub fn builder() -> Self {
Self {
out: String::from("{"),
}
}
/// Adds a key-value pair to the serialized output.
///
/// # Arguments
///
/// * `key` - The key as a string slice.
/// * `value` - A reference to a type implementing the `Serializable` trait.
///
/// # Returns
///
/// A mutable reference to the `Serializer` instance.
pub fn set(&mut self, key: &str, value: &impl Serializable) -> &mut Self {
if let Some(value) = value.serialize() {
self.out.push_str(&format!("\"{}\":{},", key, value));
}
self
}
/// Finalizes the serialized output by closing the JSON-like structure.
///
/// If the output ends with a trailing comma, it is removed before appending
/// the closing brace `}`.
///
/// # Returns
///
/// A `String` containing the complete serialized output.
pub fn build(&mut self) -> String {
if self.out.ends_with(',') {
self.out.pop();
}
self.out.clone() + "}"
}
}
impl From<Serializer> for String {
fn from(mut serializer: Serializer) -> Self {
serializer.build()
}
}
#[cfg(test)]
mod tests {
use super::Serializer;
use crate::Serializable;
struct MockSerializable<T> {
value1: T,
value2: T,
}
impl<T: Serializable> Serializable for MockSerializable<T> {
fn serialize(&self) -> Option<String> {
Serializer::builder()
.set("value1", &self.value1)
.set("value2", &self.value2)
.build()
.into()
}
}
#[test]
fn test_serializer_string() {
let mock = MockSerializable {
value1: "test_value1",
value2: "test_value2",
};
let serialized: serde_json::Value =
serde_json::from_str(&mock.serialize().unwrap()).unwrap();
assert_eq!(
serialized,
serde_json::json!({
"value1": "test_value1",
"value2": "test_value2"
})
);
}
#[test]
fn test_serializer_empty() {
assert_eq!(Serializer::builder().build(), "{}");
}
#[test]
fn test_serializer_option() {
let mock = MockSerializable {
value1: Some("test_value1"),
value2: Some("test_value2"),
};
let serialized: serde_json::Value =
serde_json::from_str(&mock.serialize().unwrap()).unwrap();
assert_eq!(
serialized,
serde_json::json!({
"value1": "test_value1",
"value2": "test_value2"
})
);
let mock_none = MockSerializable::<Option<String>> {
value1: None,
value2: None,
};
let serialized: serde_json::Value =
serde_json::from_str(&mock_none.serialize().unwrap()).unwrap();
assert_eq!(serialized, serde_json::json!({}));
let mock_mixed = MockSerializable {
value1: Some("test_value1"),
value2: None,
};
let serialized: serde_json::Value =
serde_json::from_str(&mock_mixed.serialize().unwrap()).unwrap();
assert_eq!(
serialized,
serde_json::json!({
"value1": "test_value1",
})
);
}
}