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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Protobuf encoding for types from the [`chrono`] crate.

include!(concat!(env!("OUT_DIR"), "/chrono.rs"));

use crate::proto::{ProtoRepr, TryFromProtoError};
use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, Timelike, Utc};
use chrono_tz::Tz;
use std::str::FromStr;

impl ProtoRepr for NaiveDate {
    type Repr = ProtoNaiveDate;

    fn into_proto(self: Self) -> Self::Repr {
        ProtoNaiveDate {
            year: self.year(),
            ordinal: self.ordinal(),
        }
    }

    fn from_proto(repr: Self::Repr) -> Result<Self, TryFromProtoError> {
        NaiveDate::from_yo_opt(repr.year, repr.ordinal).ok_or_else(|| {
            TryFromProtoError::DateConversionError(format!(
                "NaiveDate::from_yo_opt({},{}) failed",
                repr.year, repr.ordinal
            ))
        })
    }
}

impl ProtoRepr for NaiveTime {
    type Repr = ProtoNaiveTime;

    fn into_proto(self: Self) -> Self::Repr {
        ProtoNaiveTime {
            secs: self.num_seconds_from_midnight(),
            frac: self.nanosecond(),
        }
    }

    fn from_proto(repr: Self::Repr) -> Result<Self, TryFromProtoError> {
        NaiveTime::from_num_seconds_from_midnight_opt(repr.secs, repr.frac).ok_or_else(|| {
            TryFromProtoError::DateConversionError(format!(
                "NaiveTime::from_num_seconds_from_midnight_opt({},{}) failed",
                repr.secs, repr.frac
            ))
        })
    }
}

impl ProtoRepr for NaiveDateTime {
    type Repr = ProtoNaiveDateTime;

    fn into_proto(self: Self) -> Self::Repr {
        ProtoNaiveDateTime {
            date: Some(self.date().into_proto()),
            time: Some(self.time().into_proto()),
        }
    }

    fn from_proto(repr: Self::Repr) -> Result<Self, TryFromProtoError> {
        let date =
            NaiveDate::from_proto(repr.date.ok_or_else(|| {
                TryFromProtoError::MissingField("ProtoNaiveDateTime::date".into())
            })?)?;

        let time =
            NaiveTime::from_proto(repr.time.ok_or_else(|| {
                TryFromProtoError::MissingField("ProtoNaiveDateTime::time".into())
            })?)?;

        Ok(NaiveDateTime::new(date, time))
    }
}

impl ProtoRepr for DateTime<Utc> {
    type Repr = ProtoNaiveDateTime;

    fn into_proto(self: Self) -> Self::Repr {
        self.naive_utc().into_proto()
    }

    fn from_proto(repr: Self::Repr) -> Result<Self, TryFromProtoError> {
        Ok(DateTime::from_utc(NaiveDateTime::from_proto(repr)?, Utc))
    }
}

impl ProtoRepr for FixedOffset {
    type Repr = ProtoFixedOffset;

    fn into_proto(self: Self) -> Self::Repr {
        ProtoFixedOffset {
            local_minus_utc: self.local_minus_utc(),
        }
    }

    fn from_proto(repr: Self::Repr) -> Result<Self, TryFromProtoError> {
        FixedOffset::east_opt(repr.local_minus_utc).ok_or_else(|| {
            TryFromProtoError::DateConversionError(format!(
                "FixedOffset::east_opt({}) failed.",
                repr.local_minus_utc
            ))
        })
    }
}

/// Encode a Tz as string representation. This is not the most space efficient solution, but
/// it is immune to changes in the chrono_tz (and is fully compatible with its public API).
impl ProtoRepr for chrono_tz::Tz {
    type Repr = ProtoTz;

    fn into_proto(self: Self) -> Self::Repr {
        ProtoTz {
            name: self.name().into(),
        }
    }

    fn from_proto(repr: Self::Repr) -> Result<Self, TryFromProtoError> {
        Tz::from_str(&repr.name).map_err(TryFromProtoError::DateConversionError)
    }
}

#[cfg(test)]
mod tests {
    use super::super::protobuf_repr_roundtrip;
    use crate::chrono::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(4096))]

        #[test]
        fn naive_date_time_protobuf_roundtrip(expect in any_naive_datetime() ) {
            let actual =  protobuf_repr_roundtrip(&expect);
            assert!(actual.is_ok());
            assert_eq!(actual.unwrap(), expect);
        }

        #[test]
        fn date_time_protobuf_roundtrip(expect in any_datetime() ) {
            let actual =  protobuf_repr_roundtrip(&expect);
            assert!(actual.is_ok());
            assert_eq!(actual.unwrap(), expect);
        }

        #[test]
        fn fixed_offset_protobuf_roundtrip(expect in any_fixed_offset() ) {
            let actual =  protobuf_repr_roundtrip(&expect);
            assert!(actual.is_ok());
            assert_eq!(actual.unwrap(), expect);
        }
    }
}