diff --git a/Cargo.toml b/Cargo.toml index 22553ff..9bb764e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,3 +26,9 @@ serde_json = "1.0.108" default = [] chrono = ["dep:chrono"] time = ["dep:time"] + +[lints.rust] +missing_docs = "deny" + +[lints.rustdoc] +all = "deny" diff --git a/src/de.rs b/src/de.rs index e986e40..84780aa 100644 --- a/src/de.rs +++ b/src/de.rs @@ -16,7 +16,7 @@ use google_api_proto::google::firestore::v1::Value; /// Deserialize an instance of type `T` from a Firestore Value. /// -/// # Example +/// # Examples /// /// ```rust /// # fn main() -> anyhow::Result<()> { diff --git a/src/ser.rs b/src/ser.rs index 664c73e..9ebf099 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -15,7 +15,7 @@ use crate::{ser::firestore_value_serializer::FirestoreValueSerializer, Error}; /// Serialize an instance of type `T` to a Firestore Value. /// -/// # Example +/// # Examples /// /// ```rust /// # fn main() -> anyhow::Result<()> { diff --git a/src/with/lat_lng.rs b/src/with/lat_lng.rs index abe6404..60ca4b3 100644 --- a/src/with/lat_lng.rs +++ b/src/with/lat_lng.rs @@ -2,6 +2,35 @@ use google_api_proto::google::r#type::LatLng; +/// Deserialize `LatLng` from `geoPointValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// use google_api_proto::google::{ +/// firestore::v1::{value::ValueType, Value}, +/// r#type::LatLng, +/// }; +/// use serde_firestore_value::{from_value, with::lat_lng}; +/// +/// #[derive(Debug, PartialEq, serde::Deserialize)] +/// struct S(#[serde(deserialize_with = "lat_lng::deserialize")] LatLng); +/// let o = S(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); +/// # Ok(()) +/// # } +/// ``` pub fn deserialize<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -9,6 +38,35 @@ where crate::de::with::lat_lng_as_geo_point::deserialize_lat_lng(deserializer) } +/// Serialize `LatLng` as `geoPointValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// use google_api_proto::google::{ +/// firestore::v1::{value::ValueType, Value}, +/// r#type::LatLng, +/// }; +/// use serde_firestore_value::{to_value, with::lat_lng}; +/// +/// #[derive(Debug, PartialEq, serde::Serialize)] +/// struct S(#[serde(serialize_with = "lat_lng::serialize")] LatLng); +/// let o = S(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); +/// # Ok(()) +/// # } +/// ``` pub fn serialize(lat_lng: &LatLng, serializer: S) -> Result where S: serde::Serializer, diff --git a/src/with/option_chrono_date_time_as_timestamp.rs b/src/with/option_chrono_date_time_as_timestamp.rs index fd3c250..436f894 100644 --- a/src/with/option_chrono_date_time_as_timestamp.rs +++ b/src/with/option_chrono_date_time_as_timestamp.rs @@ -4,7 +4,7 @@ use prost_types::Timestamp; /// Deserialize `Option>` from `timestampValue` or `nullValue /// -/// # Example +/// # Examples /// /// ```rust /// # fn main() -> anyhow::Result<()> { @@ -62,7 +62,7 @@ where /// Serialize `Option` as `referenceValue` or `nullValue`. /// -/// # Example +/// # Examples /// /// ```rust /// # fn main() -> anyhow::Result<()> { diff --git a/src/with/option_lat_lng.rs b/src/with/option_lat_lng.rs index 816b6c1..d6cea1f 100644 --- a/src/with/option_lat_lng.rs +++ b/src/with/option_lat_lng.rs @@ -2,6 +2,50 @@ use google_api_proto::google::r#type::LatLng; +/// Deserialize `Option` from `geoPointValue` or `nullValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::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); +/// +/// // 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, D::Error> where D: serde::Deserializer<'de>, @@ -9,6 +53,49 @@ where crate::de::with::lat_lng_as_geo_point::deserialize_option_lat_lng(deserializer) } +/// Serialize `Option` as `geoPointValue` or `nullValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::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); +/// +/// // 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(lat_lng: &Option, serializer: S) -> Result where S: serde::Serializer, diff --git a/src/with/option_string_as_reference.rs b/src/with/option_string_as_reference.rs index 3e328da..9cdea3f 100644 --- a/src/with/option_string_as_reference.rs +++ b/src/with/option_string_as_reference.rs @@ -1,5 +1,53 @@ //! (De)serialize `Option` as `referenceValue` or `nullValue`. +/// Deserialize `Option` from `referenceValue` or `nullValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::google::firestore::v1::{value::ValueType, MapValue, Value}; +/// use serde_firestore_value::{from_value, with::string_as_reference}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Deserialize)] +/// struct S { +/// #[serde(deserialize_with = "string_as_reference::deserialize")] +/// r: String, +/// s: String, +/// } +/// +/// let o = S { +/// r: "projects/p/databases/d/documents/c/1".to_string(), +/// s: "s1".to_string(), +/// }; +/// let v = Value { +/// value_type: Some(ValueType::MapValue(MapValue { +/// fields: { +/// let mut fields = BTreeMap::new(); +/// fields.insert( +/// "r".to_string(), +/// Value { +/// value_type: Some(ValueType::ReferenceValue( +/// "projects/p/databases/d/documents/c/1".to_string(), +/// )), +/// }, +/// ); +/// fields.insert( +/// "s".to_string(), +/// Value { +/// value_type: Some(ValueType::StringValue("s1".to_string())), +/// }, +/// ); +/// fields +/// }, +/// })), +/// }; +/// let d = from_value::<'_, S>(&v)?; +/// assert_eq!(d, o); +/// # Ok(()) +/// # } +/// ``` pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::Deserializer<'de>, @@ -7,6 +55,54 @@ where crate::de::with::string_as_reference::deserialize_option_string_as_reference(deserializer) } +/// Serialize `Option` as `referenceValue` or `nullValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::google::firestore::v1::{value::ValueType, MapValue, Value}; +/// use serde_firestore_value::{to_value, with::string_as_reference}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Serialize)] +/// struct S { +/// #[serde(serialize_with = "string_as_reference::serialize")] +/// r: String, +/// s: String, +/// } +/// +/// let o = S { +/// r: "projects/p/databases/d/documents/c/1".to_string(), +/// s: "s1".to_string(), +/// }; +/// let v = Value { +/// value_type: Some(ValueType::MapValue(MapValue { +/// fields: { +/// let mut fields = BTreeMap::new(); +/// fields.insert( +/// "r".to_string(), +/// Value { +/// value_type: Some(ValueType::ReferenceValue( +/// "projects/p/databases/d/documents/c/1".to_string(), +/// )), +/// }, +/// ); +/// fields.insert( +/// "s".to_string(), +/// Value { +/// value_type: Some(ValueType::StringValue("s1".to_string())), +/// }, +/// ); +/// fields +/// }, +/// })), +/// }; +/// let s = to_value(&o)?; +/// assert_eq!(s, v); +/// # Ok(()) +/// # } +/// ``` pub fn serialize(option_string: &Option, serializer: S) -> Result where S: serde::Serializer, diff --git a/src/with/option_time_offset_date_time_as_timestamp.rs b/src/with/option_time_offset_date_time_as_timestamp.rs index 5176c87..6c5ad9a 100644 --- a/src/with/option_time_offset_date_time_as_timestamp.rs +++ b/src/with/option_time_offset_date_time_as_timestamp.rs @@ -4,7 +4,7 @@ use prost_types::Timestamp; /// Deserialize `Option` from `timestampValue` or `nullValue /// -/// # Example +/// # Examples /// /// ```rust /// # fn main() -> anyhow::Result<()> { @@ -57,7 +57,7 @@ where /// Serialize `Option` as `timestampValue` or `nullValue`. /// -/// # Example +/// # Examples /// /// ```rust /// # fn main() -> anyhow::Result<()> { diff --git a/src/with/option_timestamp.rs b/src/with/option_timestamp.rs index e605fd4..3bcfaf1 100644 --- a/src/with/option_timestamp.rs +++ b/src/with/option_timestamp.rs @@ -2,6 +2,49 @@ use prost_types::Timestamp; +/// Deserialize `Option` from `timestampValue` or `nullValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::google::firestore::v1::{value::ValueType, MapValue, Value}; +/// use prost_types::Timestamp; +/// use serde_firestore_value::{from_value, with::option_timestamp}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Deserialize)] +/// struct S(#[serde(deserialize_with = "option_timestamp::deserialize")] Option); +/// +/// // some +/// { +/// let o = S(Some(Timestamp { +/// seconds: 1_i64, +/// nanos: 2_i32, +/// })); +/// let v = Value { +/// value_type: Some(ValueType::TimestampValue(Timestamp { +/// seconds: 1_i64, +/// nanos: 2_i32, +/// })), +/// }; +/// 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, D::Error> where D: serde::Deserializer<'de>, @@ -9,6 +52,48 @@ where crate::de::with::timestamp_as_timestamp::deserialize_option_timestamp(deserializer) } +/// Serialize `Option` as `timestampValue` or `nullValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::google::firestore::v1::{value::ValueType, MapValue, Value}; +/// use prost_types::Timestamp; +/// use serde_firestore_value::{from_value, to_value, with::option_timestamp}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Serialize)] +/// struct S(#[serde(serialize_with = "option_timestamp::serialize")] Option); +/// +/// // some +/// { +/// let o = S(Some(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); +/// } +/// +/// // 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(option_timestamp: &Option, serializer: S) -> Result where S: serde::Serializer, diff --git a/src/with/string_as_reference.rs b/src/with/string_as_reference.rs index bb08993..c131d03 100644 --- a/src/with/string_as_reference.rs +++ b/src/with/string_as_reference.rs @@ -1,5 +1,54 @@ //! (De)serialize `String` as `referenceValue`. +/// Deserialize `String` from `referenceValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// +/// use google_api_proto::google::firestore::v1::{value::ValueType, MapValue, Value}; +/// use serde_firestore_value::{from_value, with::string_as_reference}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Deserialize)] +/// struct S { +/// #[serde(deserialize_with = "string_as_reference::deserialize")] +/// r: String, +/// s: String, +/// } +/// +/// let o = S { +/// r: "projects/p/databases/d/documents/c/1".to_string(), +/// s: "s1".to_string(), +/// }; +/// let v = Value { +/// value_type: Some(ValueType::MapValue(MapValue { +/// fields: { +/// let mut fields = BTreeMap::new(); +/// fields.insert( +/// "r".to_string(), +/// Value { +/// value_type: Some(ValueType::ReferenceValue( +/// "projects/p/databases/d/documents/c/1".to_string(), +/// )), +/// }, +/// ); +/// fields.insert( +/// "s".to_string(), +/// Value { +/// value_type: Some(ValueType::StringValue("s1".to_string())), +/// }, +/// ); +/// fields +/// }, +/// })), +/// }; +/// let d = from_value::<'_, S>(&v)?; +/// assert_eq!(d, o); +/// # Ok(()) +/// # } +/// ``` pub fn deserialize<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -7,6 +56,54 @@ where crate::de::with::string_as_reference::deserialize_string_as_reference(deserializer) } +/// Serialize `String` as `referenceValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::google::firestore::v1::{value::ValueType, MapValue, Value}; +/// use serde_firestore_value::{to_value, with::string_as_reference}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Serialize)] +/// struct S { +/// #[serde(serialize_with = "string_as_reference::serialize")] +/// r: String, +/// s: String, +/// } +/// +/// let o = S { +/// r: "projects/p/databases/d/documents/c/1".to_string(), +/// s: "s1".to_string(), +/// }; +/// let v = Value { +/// value_type: Some(ValueType::MapValue(MapValue { +/// fields: { +/// let mut fields = BTreeMap::new(); +/// fields.insert( +/// "r".to_string(), +/// Value { +/// value_type: Some(ValueType::ReferenceValue( +/// "projects/p/databases/d/documents/c/1".to_string(), +/// )), +/// }, +/// ); +/// fields.insert( +/// "s".to_string(), +/// Value { +/// value_type: Some(ValueType::StringValue("s1".to_string())), +/// }, +/// ); +/// fields +/// }, +/// })), +/// }; +/// let s = to_value(&o)?; +/// assert_eq!(s, v); +/// # Ok(()) +/// # } +/// ``` pub fn serialize(s: &str, serializer: S) -> Result where S: serde::Serializer, diff --git a/src/with/timestamp.rs b/src/with/timestamp.rs index fd21ba1..5602a4c 100644 --- a/src/with/timestamp.rs +++ b/src/with/timestamp.rs @@ -2,6 +2,35 @@ use prost_types::Timestamp; +/// Deserialize `Timestamp` from `timestampValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::google::firestore::v1::{value::ValueType, ArrayValue, MapValue, Value}; +/// use prost_types::Timestamp; +/// use serde_firestore_value::{from_value, with::timestamp}; +/// +/// #[derive(Debug, Eq, PartialEq, serde::Deserialize)] +/// struct S(#[serde(deserialize_with = "timestamp::deserialize")] 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 d = from_value::<'_, S>(&v)?; +/// assert_eq!(d, o); +/// # Ok(()) +/// # } +/// ``` pub fn deserialize<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -9,6 +38,35 @@ where crate::de::with::timestamp_as_timestamp::deserialize_timestamp(deserializer) } +/// Serialize `Timestamp` as `timestampValue`. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # use std::collections::BTreeMap; +/// use google_api_proto::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(timestamp: &Timestamp, serializer: S) -> Result where S: serde::Serializer,