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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use crate::serde::helpers as serde_helpers;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "chrono")]
use chrono::Duration as ChronoDuration;
use std::{
    fmt::{self, Display, Formatter},
    str::FromStr,
    time::Duration as StdDuration,
};
use thiserror::Error;
use time::Duration as TimeDuration;

use crate::google::protobuf::Duration;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum DurationError {
    #[error("duration is out of range")]
    OutOfRange,
    #[error("duration is negative")]
    NegativeDuration,
    #[error("invalid duration string format `{0}`")]
    InvalidFormat(String),
}

impl Duration {
    #[must_use]
    pub const fn new(seconds: i64, nanos: i32) -> Self {
        Self { seconds, nanos }
    }

    /// Get Duration normalized to a canonical format.
    /// Based on [1] and [2].
    ///
    /// [1]: https://github.com/tokio-rs/prost/blob/v0.12.1/prost-types/src/lib.rs#L107
    /// [2]: https://github.com/protocolbuffers/protobuf/blob/v3.3.2/src/google/protobuf/util/time_util.cc#L79-L100
    #[must_use]
    pub fn normalized(self) -> Self {
        const NANOS_PER_SECOND: i64 = 1_000_000_000;
        const NANOS_MAX: i64 = NANOS_PER_SECOND - 1;
        // const DURATION_MIN_SECONDS: i64 = -315_576_000_000;
        // const DURATION_MAX_SECONDS: i64 = 315_576_000_000;

        let mut seconds = self.seconds;
        let mut nanos = i64::from(self.nanos);

        // Make sure nanos is in the range.
        if nanos <= -NANOS_PER_SECOND || nanos >= NANOS_PER_SECOND {
            if let Some(new_seconds) = seconds.checked_add(nanos / NANOS_PER_SECOND) {
                seconds = new_seconds;
                nanos %= NANOS_PER_SECOND;
            } else if nanos < 0 {
                seconds = i64::MIN;
                nanos = -NANOS_MAX;
            } else {
                seconds = i64::MAX;
                nanos = NANOS_MAX;
            }
        }

        // Nanos should have the same sign as seconds.
        if seconds < 0 && nanos > 0 {
            if let Some(new_seconds) = seconds.checked_add(1) {
                seconds = new_seconds;
                nanos -= NANOS_PER_SECOND;
            } else {
                nanos = NANOS_MAX;
            }
        } else if seconds > 0 && nanos < 0 {
            if let Some(new_seconds) = seconds.checked_sub(1) {
                seconds = new_seconds;
                nanos += NANOS_PER_SECOND;
            } else {
                nanos = -NANOS_MAX;
            }
        }

        // debug_assert!(
        //     seconds >= DURATION_MIN_SECONDS && seconds <= DURATION_MAX_SECONDS,
        //     "seconds out of range: {}",
        //     seconds
        // );

        Self {
            seconds,
            nanos: nanos as i32,
        }
    }
}

impl TryFrom<StdDuration> for Duration {
    type Error = DurationError;

    fn try_from(duration: StdDuration) -> Result<Self, DurationError> {
        let seconds = i64::try_from(duration.as_secs()).map_err(|_| DurationError::OutOfRange)?;
        let nanos = duration.subsec_nanos() as i32;
        Ok(Self { seconds, nanos }.normalized())
    }
}

impl TryFrom<Duration> for StdDuration {
    type Error = DurationError;

    fn try_from(duration: Duration) -> Result<Self, DurationError> {
        let d = duration.normalized();
        if d.seconds >= 0 && d.nanos >= 0 {
            Ok(Self::new(d.seconds as u64, d.nanos as u32))
        } else {
            Err(DurationError::NegativeDuration)
        }
    }
}

impl TryFrom<TimeDuration> for Duration {
    type Error = DurationError;

    fn try_from(duration: TimeDuration) -> Result<Self, DurationError> {
        StdDuration::try_from(duration)
            .map_err(|_| DurationError::OutOfRange)?
            .try_into()
            .map_err(|_| DurationError::OutOfRange)
    }
}

impl TryFrom<Duration> for TimeDuration {
    type Error = DurationError;

    fn try_from(duration: Duration) -> Result<Self, DurationError> {
        Self::try_from(StdDuration::try_from(duration)?).map_err(|_| DurationError::OutOfRange)
    }
}

#[cfg(feature = "chrono")]
impl TryFrom<ChronoDuration> for Duration {
    type Error = DurationError;

    fn try_from(duration: ChronoDuration) -> Result<Self, DurationError> {
        duration
            .to_std()
            .map_err(|_| DurationError::OutOfRange)?
            .try_into()
    }
}

#[cfg(feature = "chrono")]
impl TryFrom<Duration> for ChronoDuration {
    type Error = DurationError;

    fn try_from(duration: Duration) -> Result<Self, DurationError> {
        Self::from_std(StdDuration::try_from(duration)?).map_err(|_| DurationError::OutOfRange)
    }
}

impl Display for Duration {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if self.nanos == 0 {
            write!(f, "{}s", self.seconds)
        } else {
            let fractional_seconds = (f64::from(self.nanos) / 1_000_000_000.0).to_string();
            write!(
                f,
                "{}.{}s",
                self.seconds,
                fractional_seconds.trim_start_matches("0.")
            )
        }
    }
}

impl FromStr for Duration {
    type Err = DurationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if !s.ends_with('s') {
            return Err(DurationError::InvalidFormat(s.into()));
        }

        let v = &s[..s.len() - 1]; // Remove 's'
        let parts: Vec<&str> = v.split('.').collect();

        if parts.len() > 2 {
            return Err(DurationError::InvalidFormat(s.into()));
        }

        let seconds = parts[0]
            .parse::<i64>()
            .map_err(|_| DurationError::InvalidFormat(s.into()))?;
        let nanos = if parts.len() > 1 {
            (format!("0.{}", parts[1])
                .parse::<f64>()
                .map_err(|_| DurationError::InvalidFormat(s.into()))?
                * 1_000_000_000f64) as i32
        } else {
            0
        };

        Ok(Self::new(seconds, nanos))
    }
}

impl Serialize for Duration {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        serde_helpers::duration::serialize(self, serializer)
    }
}

impl<'de> Deserialize<'de> for Duration {
    fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
    where
        D: Deserializer<'de>,
    {
        serde_helpers::duration::deserialize(deserializer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn str_convert() {
        let d = Duration::new(10, 2);
        let s = d.to_string();
        assert_eq!(s, "10.000000002s");
        let d2 = s.parse::<Duration>().unwrap();
        assert_eq!(d, d2);
    }

    #[test]
    fn normalize() {
        #[rustfmt::skip]
        let cases = [
            // --- Table of test cases ---
            //        test seconds      test nanos  expected seconds  expected nanos
            (line!(),            0,              0,                0,              0),
            (line!(),            1,              1,                1,              1),
            (line!(),           -1,             -1,               -1,             -1),
            (line!(),            0,    999_999_999,                0,    999_999_999),
            (line!(),            0,   -999_999_999,                0,   -999_999_999),
            (line!(),            0,  1_000_000_000,                1,              0),
            (line!(),            0, -1_000_000_000,               -1,              0),
            (line!(),            0,  1_000_000_001,                1,              1),
            (line!(),            0, -1_000_000_001,               -1,             -1),
            (line!(),           -1,              1,                0,   -999_999_999),
            (line!(),            1,             -1,                0,    999_999_999),
            (line!(),           -1,  1_000_000_000,                0,              0),
            (line!(),            1, -1_000_000_000,                0,              0),
            (line!(), i64::MIN    ,              0,     i64::MIN    ,              0),
            (line!(), i64::MIN + 1,              0,     i64::MIN + 1,              0),
            (line!(), i64::MIN    ,              1,     i64::MIN + 1,   -999_999_999),
            (line!(), i64::MIN    ,  1_000_000_000,     i64::MIN + 1,              0),
            (line!(), i64::MIN    , -1_000_000_000,     i64::MIN    ,   -999_999_999),
            (line!(), i64::MIN + 1, -1_000_000_000,     i64::MIN    ,              0),
            (line!(), i64::MIN + 2, -1_000_000_000,     i64::MIN + 1,              0),
            (line!(), i64::MIN    , -1_999_999_998,     i64::MIN    ,   -999_999_999),
            (line!(), i64::MIN + 1, -1_999_999_998,     i64::MIN    ,   -999_999_998),
            (line!(), i64::MIN + 2, -1_999_999_998,     i64::MIN + 1,   -999_999_998),
            (line!(), i64::MIN    , -1_999_999_999,     i64::MIN    ,   -999_999_999),
            (line!(), i64::MIN + 1, -1_999_999_999,     i64::MIN    ,   -999_999_999),
            (line!(), i64::MIN + 2, -1_999_999_999,     i64::MIN + 1,   -999_999_999),
            (line!(), i64::MIN    , -2_000_000_000,     i64::MIN    ,   -999_999_999),
            (line!(), i64::MIN + 1, -2_000_000_000,     i64::MIN    ,   -999_999_999),
            (line!(), i64::MIN + 2, -2_000_000_000,     i64::MIN    ,              0),
            (line!(), i64::MIN    ,   -999_999_998,     i64::MIN    ,   -999_999_998),
            (line!(), i64::MIN + 1,   -999_999_998,     i64::MIN + 1,   -999_999_998),
            (line!(), i64::MAX    ,              0,     i64::MAX    ,              0),
            (line!(), i64::MAX - 1,              0,     i64::MAX - 1,              0),
            (line!(), i64::MAX    ,             -1,     i64::MAX - 1,    999_999_999),
            (line!(), i64::MAX    ,  1_000_000_000,     i64::MAX    ,    999_999_999),
            (line!(), i64::MAX - 1,  1_000_000_000,     i64::MAX    ,              0),
            (line!(), i64::MAX - 2,  1_000_000_000,     i64::MAX - 1,              0),
            (line!(), i64::MAX    ,  1_999_999_998,     i64::MAX    ,    999_999_999),
            (line!(), i64::MAX - 1,  1_999_999_998,     i64::MAX    ,    999_999_998),
            (line!(), i64::MAX - 2,  1_999_999_998,     i64::MAX - 1,    999_999_998),
            (line!(), i64::MAX    ,  1_999_999_999,     i64::MAX    ,    999_999_999),
            (line!(), i64::MAX - 1,  1_999_999_999,     i64::MAX    ,    999_999_999),
            (line!(), i64::MAX - 2,  1_999_999_999,     i64::MAX - 1,    999_999_999),
            (line!(), i64::MAX    ,  2_000_000_000,     i64::MAX    ,    999_999_999),
            (line!(), i64::MAX - 1,  2_000_000_000,     i64::MAX    ,    999_999_999),
            (line!(), i64::MAX - 2,  2_000_000_000,     i64::MAX    ,              0),
            (line!(), i64::MAX    ,    999_999_998,     i64::MAX    ,    999_999_998),
            (line!(), i64::MAX - 1,    999_999_998,     i64::MAX - 1,    999_999_998),
        ];

        for case in &cases {
            assert_eq!(
                Duration {
                    seconds: case.1,
                    nanos: case.2,
                }
                .normalized(),
                Duration {
                    seconds: case.3,
                    nanos: case.4,
                },
                "test case on line {} doesn't match",
                case.0,
            );
        }

        dbg!(StdDuration::new(0, u32::MAX));
    }
}