serde_firestore_value/with/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
//! (De)serialize `Timestamp` as `timestampValue`.
/// Deserialize `Timestamp` from `timestampValue`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
/// use serde_firestore_value::google::firestore::v1::{value::ValueType, ArrayValue, MapValue, Value};
/// use serde_firestore_value::{from_value, with::timestamp};
///
/// #[derive(Debug, Eq, PartialEq, serde::Deserialize)]
/// struct S(#[serde(deserialize_with = "timestamp::deserialize")] prost_types::Timestamp);
///
/// let o = S(prost_types::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 d = from_value::<'_, S>(&v)?;
/// assert_eq!(d, o);
/// # Ok(())
/// # }
/// ```
pub fn deserialize<'de, D>(deserializer: D) -> Result<prost_types::Timestamp, D::Error>
where
D: serde::Deserializer<'de>,
{
crate::de::with::timestamp_as_timestamp::deserialize_timestamp(deserializer)
}
/// Serialize `Timestamp` as `timestampValue`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
/// use serde_firestore_value::google::firestore::v1::{value::ValueType, ArrayValue, MapValue, Value};
/// use prost_types::Timestamp;
/// use serde_firestore_value::{to_value, with::timestamp};
///
/// #[derive(Debug, Eq, PartialEq, serde::Serialize)]
/// struct S(#[serde(serialize_with = "timestamp::serialize")] Timestamp);
///
/// let o = S(Timestamp {
/// seconds: 1_i64,
/// nanos: 2_i32,
/// });
/// let v = Value {
/// value_type: Some(ValueType::TimestampValue(Timestamp {
/// seconds: 1_i64,
/// nanos: 2_i32,
/// })),
/// };
/// let s = to_value(&o)?;
/// assert_eq!(s, v);
/// # Ok(())
/// # }
/// ```
pub fn serialize<S>(timestamp: &prost_types::Timestamp, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
crate::ser::with::timestamp_as_timestamp::serialize_timestamp(timestamp, serializer)
}