1use serde::{Deserialize, Serialize, de};
19
20#[derive(Debug, Copy, Clone, Eq, PartialEq)]
22pub struct Duration {
23 months: Months,
24 days: Days,
25 millis: Millis,
26}
27
28#[derive(Debug, Copy, Clone, Eq, PartialEq)]
30pub struct Months(u32);
31
32impl Months {
33 pub fn new(months: u32) -> Self {
34 Self(months)
35 }
36
37 fn as_bytes(&self) -> [u8; 4] {
38 self.0.to_le_bytes()
39 }
40}
41
42impl From<Months> for u32 {
43 fn from(days: Months) -> Self {
44 days.0
45 }
46}
47
48impl From<[u8; 4]> for Months {
49 fn from(bytes: [u8; 4]) -> Self {
50 Self(u32::from_le_bytes(bytes))
51 }
52}
53
54#[derive(Debug, Copy, Clone, Eq, PartialEq)]
56pub struct Days(u32);
57
58impl Days {
59 pub fn new(days: u32) -> Self {
60 Self(days)
61 }
62
63 fn as_bytes(&self) -> [u8; 4] {
64 self.0.to_le_bytes()
65 }
66}
67
68impl From<Days> for u32 {
69 fn from(days: Days) -> Self {
70 days.0
71 }
72}
73
74impl From<[u8; 4]> for Days {
75 fn from(bytes: [u8; 4]) -> Self {
76 Self(u32::from_le_bytes(bytes))
77 }
78}
79
80#[derive(Debug, Copy, Clone, Eq, PartialEq)]
82pub struct Millis(u32);
83
84impl Millis {
85 pub fn new(millis: u32) -> Self {
86 Self(millis)
87 }
88
89 fn as_bytes(&self) -> [u8; 4] {
90 self.0.to_le_bytes()
91 }
92}
93
94impl From<Millis> for u32 {
95 fn from(days: Millis) -> Self {
96 days.0
97 }
98}
99
100impl From<[u8; 4]> for Millis {
101 fn from(bytes: [u8; 4]) -> Self {
102 Self(u32::from_le_bytes(bytes))
103 }
104}
105
106impl Duration {
107 pub fn new(months: Months, days: Days, millis: Millis) -> Self {
109 Self {
110 months,
111 days,
112 millis,
113 }
114 }
115
116 pub fn months(&self) -> Months {
118 self.months
119 }
120
121 pub fn days(&self) -> Days {
123 self.days
124 }
125
126 pub fn millis(&self) -> Millis {
128 self.millis
129 }
130}
131
132impl From<&Duration> for [u8; 12] {
133 fn from(duration: &Duration) -> Self {
134 let mut bytes = [0u8; 12];
135 bytes[0..4].copy_from_slice(&duration.months.as_bytes());
136 bytes[4..8].copy_from_slice(&duration.days.as_bytes());
137 bytes[8..12].copy_from_slice(&duration.millis.as_bytes());
138 bytes
139 }
140}
141
142impl From<Duration> for [u8; 12] {
143 fn from(duration: Duration) -> Self {
144 (&duration).into()
145 }
146}
147
148impl From<&[u8; 12]> for Duration {
149 fn from(bytes: &[u8; 12]) -> Self {
150 Self {
151 months: Months::from([bytes[0], bytes[1], bytes[2], bytes[3]]),
152 days: Days::from([bytes[4], bytes[5], bytes[6], bytes[7]]),
153 millis: Millis::from([bytes[8], bytes[9], bytes[10], bytes[11]]),
154 }
155 }
156}
157
158impl From<[u8; 12]> for Duration {
159 fn from(bytes: [u8; 12]) -> Duration {
160 (&bytes).into()
161 }
162}
163
164impl Serialize for Duration {
165 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
166 where
167 S: serde::Serializer,
168 {
169 let value_bytes: [u8; 12] = self.into();
170 serializer.serialize_bytes(&value_bytes)
171 }
172}
173
174impl<'de> Deserialize<'de> for Duration {
175 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
176 where
177 D: serde::Deserializer<'de>,
178 {
179 struct DurationVisitor;
180
181 impl de::Visitor<'_> for DurationVisitor {
182 type Value = Duration;
183
184 fn expecting(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
185 write!(f, "a byte array with size 12")
186 }
187
188 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
189 where
190 E: de::Error,
191 {
192 if v.len() != 12 {
193 Err(E::custom(format!(
194 "Expected byte array of length 12, but length is {}",
195 v.len()
196 )))
197 } else {
198 let v_slice: [u8; 12] = v[..12].try_into().unwrap();
199 Ok(Duration::from(v_slice))
200 }
201 }
202 }
203
204 deserializer.deserialize_bytes(DurationVisitor)
205 }
206}
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211 use crate::types::Value;
212 use apache_avro_test_helper::TestResult;
213
214 #[test]
215 fn avro_rs_382_duration_from_value() -> TestResult {
216 let val = Value::Duration(Duration::new(Months::new(7), Days::new(4), Millis::new(45)));
217 let de_val: Duration = crate::from_value(&val)?;
218 assert_eq!(de_val.months(), Months::new(7));
219 assert_eq!(de_val.days(), Days::new(4));
220 assert_eq!(de_val.millis(), Millis::new(45));
221 Ok(())
222 }
223
224 #[test]
225 fn avro_rs_382_duration_to_value() -> TestResult {
226 let duration = Duration::new(Months::new(7), Days::new(4), Millis::new(45));
227 let ser_val = crate::to_value(duration)?;
228 match ser_val {
229 Value::Bytes(b) => {
231 assert_eq!(b, vec![7, 0, 0, 0, 4, 0, 0, 0, 45, 0, 0, 0]);
232 }
233 _ => {
234 Err(format!("Expected a Bytes value but got {ser_val:?}"))?;
235 }
236 }
237 Ok(())
238 }
239}