apache_avro/
duration.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17/// A struct representing duration that hides the details of endianness and conversion between
18/// platform-native u32 and byte arrays.
19#[derive(Debug, Copy, Clone, Eq, PartialEq)]
20pub struct Duration {
21    months: Months,
22    days: Days,
23    millis: Millis,
24}
25
26#[derive(Debug, Copy, Clone, Eq, PartialEq)]
27pub struct Months(u32);
28
29impl Months {
30    pub fn new(months: u32) -> Self {
31        Self(months)
32    }
33
34    fn as_bytes(&self) -> [u8; 4] {
35        self.0.to_le_bytes()
36    }
37}
38
39impl From<Months> for u32 {
40    fn from(days: Months) -> Self {
41        days.0
42    }
43}
44
45impl From<[u8; 4]> for Months {
46    fn from(bytes: [u8; 4]) -> Self {
47        Self(u32::from_le_bytes(bytes))
48    }
49}
50
51#[derive(Debug, Copy, Clone, Eq, PartialEq)]
52pub struct Days(u32);
53
54impl Days {
55    pub fn new(days: u32) -> Self {
56        Self(days)
57    }
58
59    fn as_bytes(&self) -> [u8; 4] {
60        self.0.to_le_bytes()
61    }
62}
63
64impl From<Days> for u32 {
65    fn from(days: Days) -> Self {
66        days.0
67    }
68}
69
70impl From<[u8; 4]> for Days {
71    fn from(bytes: [u8; 4]) -> Self {
72        Self(u32::from_le_bytes(bytes))
73    }
74}
75
76#[derive(Debug, Copy, Clone, Eq, PartialEq)]
77pub struct Millis(u32);
78
79impl Millis {
80    pub fn new(millis: u32) -> Self {
81        Self(millis)
82    }
83
84    fn as_bytes(&self) -> [u8; 4] {
85        self.0.to_le_bytes()
86    }
87}
88
89impl From<Millis> for u32 {
90    fn from(days: Millis) -> Self {
91        days.0
92    }
93}
94
95impl From<[u8; 4]> for Millis {
96    fn from(bytes: [u8; 4]) -> Self {
97        Self(u32::from_le_bytes(bytes))
98    }
99}
100
101impl Duration {
102    /// Construct a new `Duration`.
103    pub fn new(months: Months, days: Days, millis: Millis) -> Self {
104        Self {
105            months,
106            days,
107            millis,
108        }
109    }
110
111    /// Return the number of months in this duration.
112    pub fn months(&self) -> Months {
113        self.months
114    }
115
116    /// Return the number of days in this duration.
117    pub fn days(&self) -> Days {
118        self.days
119    }
120
121    /// Return the number of milliseconds in this duration.
122    pub fn millis(&self) -> Millis {
123        self.millis
124    }
125}
126
127impl From<Duration> for [u8; 12] {
128    fn from(duration: Duration) -> Self {
129        let mut bytes = [0u8; 12];
130        bytes[0..4].copy_from_slice(&duration.months.as_bytes());
131        bytes[4..8].copy_from_slice(&duration.days.as_bytes());
132        bytes[8..12].copy_from_slice(&duration.millis.as_bytes());
133        bytes
134    }
135}
136
137impl From<[u8; 12]> for Duration {
138    fn from(bytes: [u8; 12]) -> Self {
139        Self {
140            months: Months::from([bytes[0], bytes[1], bytes[2], bytes[3]]),
141            days: Days::from([bytes[4], bytes[5], bytes[6], bytes[7]]),
142            millis: Millis::from([bytes[8], bytes[9], bytes[10], bytes[11]]),
143        }
144    }
145}