serde_firestore_value/typ/
timestamp.rs

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
/// Timestamp
///
/// `timestampValue` inner type.
///
/// <https://protobuf.dev/reference/protobuf/google.protobuf/#timestamp>
/// <https://firebase.google.com/docs/firestore/reference/rest/Shared.Types/ArrayValue#Value>
///
/// # Examples
///
/// ```rust
/// # fn test_timestamp() -> anyhow::Result<()> {
/// #     use serde_firestore_value::google::firestore::v1::{value::ValueType, Value};
/// #     use serde_firestore_value::{from_value, to_value, Timestamp};
/// let o = Timestamp {
///     seconds: 1_i64,
///     nanos: 2_i32,
/// };
/// let v = Value {
///     value_type: Some(ValueType::TimestampValue(prost_types::Timestamp {
///         seconds: 1_i64,
///         nanos: 2_i32,
///     })),
/// };
/// let s = to_value(&o)?;
/// let d = from_value::<'_, Timestamp>(&s)?;
/// assert_eq!(s, v);
/// assert_eq!(d, o);
/// #     Ok(())
/// # }
/// ```
#[derive(
    Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
)]
#[serde(rename = "$__serde-firestore-value_private_timestamp")]
pub struct Timestamp {
    /// seconds
    pub seconds: i64,
    /// nanoseconds
    pub nanos: i32,
}

impl Timestamp {
    pub(crate) const NAME: &'static str = "$__serde-firestore-value_private_timestamp";
}

impl From<Timestamp> for prost_types::Timestamp {
    fn from(Timestamp { seconds, nanos }: Timestamp) -> Self {
        Self { seconds, nanos }
    }
}

impl From<prost_types::Timestamp> for Timestamp {
    fn from(prost_types::Timestamp { seconds, nanos }: prost_types::Timestamp) -> Self {
        Self { seconds, nanos }
    }
}

#[cfg(feature = "chrono")]
impl std::convert::TryFrom<Timestamp> for chrono::DateTime<chrono::Utc> {
    type Error = crate::Error;

    fn try_from(Timestamp { seconds, nanos }: Timestamp) -> Result<Self, Self::Error> {
        let nanos = u32::try_from(nanos).map_err(|_| {
            crate::Error::from(crate::error::ErrorCode::Custom(format!(
                "chrono::DateTime::<chrono::Utc>::try_from(Timestamp) / u32::try_from({})",
                nanos
            )))
        })?;
        Self::from_timestamp(seconds, nanos).ok_or_else(|| {
            crate::Error::from(crate::error::ErrorCode::Custom(format!(
                "chrono::DateTime::<chrono::Utc>::try_from(Timestamp) / chrono::DateTime::<chrono::Utc>::from_timestamp({}, {})",
                seconds, nanos
            )))
        })
    }
}

#[cfg(feature = "time")]
impl std::convert::TryFrom<Timestamp> for time::OffsetDateTime {
    type Error = crate::Error;

    fn try_from(Timestamp { seconds, nanos }: Timestamp) -> Result<Self, Self::Error> {
        let timestamp_nanos = i128::from(seconds) * 1_000_000_000_i128 + i128::from(nanos);
        Self::from_unix_timestamp_nanos(timestamp_nanos).map_err(|e| {
            crate::Error::from(crate::error::ErrorCode::Custom(format!(
                "time::OffsetDateTime::try_from(Tiemstamp) / time::OffsetDateTime::from_unix_timestamp_nanos({}) : {}",
                timestamp_nanos,
                e
            )))
        })
    }
}

#[cfg(feature = "chrono")]
impl std::convert::TryFrom<chrono::DateTime<chrono::Utc>> for Timestamp {
    type Error = crate::Error;

    fn try_from(date_time: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
        let seconds = date_time.timestamp();
        let nanos = date_time.timestamp_subsec_nanos();
        let nanos = i32::try_from(nanos).map_err(|_| {
            crate::Error::from(crate::error::ErrorCode::Custom(format!(
                "Timestamp::try_from(chrono::DateTime::<chrono::Utc>) / i32::try_from({})",
                nanos
            )))
        })?;
        Ok(Self { seconds, nanos })
    }
}

#[cfg(feature = "time")]
impl std::convert::TryFrom<time::OffsetDateTime> for Timestamp {
    type Error = crate::Error;

    fn try_from(offset_date_time: time::OffsetDateTime) -> Result<Self, Self::Error> {
        let seconds = offset_date_time.unix_timestamp();
        let nanos = offset_date_time.unix_timestamp_nanos() % 1_000_000_000_i128;
        let nanos = i32::try_from(nanos).map_err(|_| {
            crate::Error::from(crate::error::ErrorCode::Custom(format!(
                "Timestamp::try_from(chrono::DateTime::<chrono::Utc>) / i32::try_from({})",
                nanos
            )))
        })?;
        Ok(Self { seconds, nanos })
    }
}