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
153
154
155
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use byteorder::LittleEndian;
use zerocopy::U32;

/// A struct representing duration that hides the details of endianness and conversion between
/// platform-native u32 and byte arrays.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Duration {
    months: Months,
    days: Days,
    millis: Millis,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Months(U32<LittleEndian>);

impl Months {
    pub fn new(months: u32) -> Self {
        Self(U32::new(months))
    }
}

impl From<Months> for u32 {
    fn from(days: Months) -> Self {
        days.0.get()
    }
}

impl From<[u8; 4]> for Months {
    fn from(bytes: [u8; 4]) -> Self {
        Self(U32::from(bytes))
    }
}

impl AsRef<[u8; 4]> for Months {
    fn as_ref(&self) -> &[u8; 4] {
        self.0.as_ref()
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Days(U32<LittleEndian>);

impl Days {
    pub fn new(days: u32) -> Self {
        Self(U32::new(days))
    }
}

impl From<Days> for u32 {
    fn from(days: Days) -> Self {
        days.0.get()
    }
}

impl From<[u8; 4]> for Days {
    fn from(bytes: [u8; 4]) -> Self {
        Self(U32::from(bytes))
    }
}

impl AsRef<[u8; 4]> for Days {
    fn as_ref(&self) -> &[u8; 4] {
        self.0.as_ref()
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Millis(U32<LittleEndian>);

impl Millis {
    pub fn new(millis: u32) -> Self {
        Self(U32::new(millis))
    }
}

impl From<Millis> for u32 {
    fn from(days: Millis) -> Self {
        days.0.get()
    }
}

impl From<[u8; 4]> for Millis {
    fn from(bytes: [u8; 4]) -> Self {
        Self(U32::from(bytes))
    }
}

impl AsRef<[u8; 4]> for Millis {
    fn as_ref(&self) -> &[u8; 4] {
        self.0.as_ref()
    }
}

impl Duration {
    /// Construct a new `Duration`.
    pub fn new(months: Months, days: Days, millis: Millis) -> Self {
        Self {
            months,
            days,
            millis,
        }
    }

    /// Return the number of months in this duration.
    pub fn months(&self) -> Months {
        self.months
    }

    /// Return the number of days in this duration.
    pub fn days(&self) -> Days {
        self.days
    }

    /// Return the number of milliseconds in this duration.
    pub fn millis(&self) -> Millis {
        self.millis
    }
}

impl From<Duration> for [u8; 12] {
    fn from(duration: Duration) -> Self {
        let mut bytes = [0u8; 12];
        bytes[0..4].copy_from_slice(duration.months.as_ref());
        bytes[4..8].copy_from_slice(duration.days.as_ref());
        bytes[8..12].copy_from_slice(duration.millis.as_ref());
        bytes
    }
}

impl From<[u8; 12]> for Duration {
    fn from(bytes: [u8; 12]) -> Self {
        Self {
            months: Months::from([bytes[0], bytes[1], bytes[2], bytes[3]]),
            days: Days::from([bytes[4], bytes[5], bytes[6], bytes[7]]),
            millis: Millis::from([bytes[8], bytes[9], bytes[10], bytes[11]]),
        }
    }
}