serde_firestore_value/with/option_lat_lng.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
//! (De)serialize `Option<LatLng>` as `geoPointValue` or `nullValue`.
use crate::google::r#type::LatLng as GoogleApiProtoLatLng;
/// Deserialize `Option<LatLng>` from `geoPointValue` or `nullValue`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
/// # use std::collections::BTreeMap;
/// use serde_firestore_value::google::{
/// firestore::v1::{value::ValueType, MapValue, Value},
/// r#type::LatLng,
/// };
/// use serde_firestore_value::{from_value, with::option_lat_lng};
///
/// #[derive(Debug, PartialEq, serde::Deserialize)]
/// struct S(#[serde(deserialize_with = "option_lat_lng::deserialize")] Option<LatLng>);
///
/// // some
/// {
/// let o = S(Some(LatLng {
/// latitude: 1_f64,
/// longitude: 2_f64,
/// }));
/// let v = Value {
/// value_type: Some(ValueType::GeoPointValue(LatLng {
/// latitude: 1_f64,
/// longitude: 2_f64,
/// })),
/// };
/// let d = from_value::<'_, S>(&v)?;
/// assert_eq!(d, o);
/// }
///
/// // none
/// {
/// let o = S(None);
/// let v = Value {
/// value_type: Some(ValueType::NullValue(0_i32)),
/// };
/// let d = from_value::<'_, S>(&v)?;
/// assert_eq!(d, o);
/// }
/// # Ok(())
/// # }
/// ```
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<GoogleApiProtoLatLng>, D::Error>
where
D: serde::Deserializer<'de>,
{
crate::de::with::lat_lng_as_geo_point::deserialize_option_lat_lng(deserializer)
}
/// Serialize `Option<LatLng>` as `geoPointValue` or `nullValue`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
/// # use std::collections::BTreeMap;
/// use serde_firestore_value::google::{
/// firestore::v1::{value::ValueType, MapValue, Value},
/// r#type::LatLng,
/// };
/// use serde_firestore_value::{to_value, with::option_lat_lng};
/// #[derive(Debug, PartialEq, serde::Serialize)]
/// struct S(#[serde(serialize_with = "option_lat_lng::serialize")] Option<LatLng>);
///
/// // some
/// {
/// let o = S(Some(LatLng {
/// latitude: 1_f64,
/// longitude: 2_f64,
/// }));
/// let v = Value {
/// value_type: Some(ValueType::GeoPointValue(LatLng {
/// latitude: 1_f64,
/// longitude: 2_f64,
/// })),
/// };
/// let s = to_value(&o)?;
/// assert_eq!(s, v);
/// }
///
/// // none
/// {
/// let o = S(None);
/// let v = Value {
/// value_type: Some(ValueType::NullValue(0)),
/// };
/// let s = to_value(&o)?;
/// assert_eq!(s, v);
/// }
/// # Ok(())
/// # }
/// ```
pub fn serialize<S>(
lat_lng: &Option<GoogleApiProtoLatLng>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
crate::ser::with::lat_lng_as_geo_point::serialize_option_lat_lng(lat_lng, serializer)
}