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
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::time::Duration;
pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if let Some(milis) = s.strip_suffix("ms") {
return milis
.replace('_', "")
.parse::<u64>()
.map(Duration::from_millis)
.map_err(|e| serde::de::Error::custom(e.to_string()));
} else if let Some(seconds) = s.strip_suffix('s') {
return seconds
.replace('_', "")
.parse::<u64>()
.map(Duration::from_secs)
.map_err(|e| serde::de::Error::custom(e.to_string()));
}
Err(serde::de::Error::custom(format!(
"Wrong format detected: {s}. It should be number in miliseconds, e.x 10ms"
)))
}
pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
format!("{}ms", duration.as_millis()).serialize(serializer)
}
#[cfg(test)]
mod tests {
use crate::duration_format;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
struct MockProperties {
#[serde(with = "duration_format")]
property_1: Duration,
#[serde(with = "duration_format")]
property_2: Duration,
#[serde(with = "duration_format")]
property_3: Duration,
#[serde(with = "duration_format")]
property_4: Duration,
}
#[test]
fn parse_miliseconds_and_seconds() {
let input = r#"{
"property_1": "1_000ms",
"property_2": "2ms",
"property_3": "8s",
"property_4": "5_000s"
}"#;
let result: MockProperties =
serde_json::from_str(input).expect("Couldn't deserialize string");
assert_eq!(result.property_1.as_millis(), 1_000);
assert_eq!(result.property_2.as_millis(), 2);
assert_eq!(result.property_3.as_secs(), 8);
assert_eq!(result.property_4.as_secs(), 5_000);
}
#[test]
fn roundtrip() {
let input = r#"{
"property_1": "1_000ms",
"property_2": "2ms",
"property_3": "8s",
"property_4": "5_000s"
}"#;
let result: MockProperties =
serde_json::from_str(input).expect("Couldn't deserialize string");
let serialized = serde_json::to_string(&result).unwrap();
let deserialized = serde_json::from_str(&serialized).unwrap();
assert_eq!(result, deserialized);
}
#[test]
fn parse_error() {
let input = r#"{
"property_1": "1000 ms",
"property_2": "8seconds"
}"#;
let result = serde_json::from_str::<MockProperties>(input);
assert!(result.is_err());
}
}