Skip to content

Commit

Permalink
Add doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 31, 2023
1 parent d2d6854 commit a1f9c10
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion src/de.rs
Expand Up @@ -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<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/ser.rs
Expand Up @@ -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<()> {
Expand Down
58 changes: 58 additions & 0 deletions src/with/lat_lng.rs
Expand Up @@ -2,13 +2,71 @@

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<LatLng, D::Error>
where
D: serde::Deserializer<'de>,
{
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<S>(lat_lng: &LatLng, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down
4 changes: 2 additions & 2 deletions src/with/option_chrono_date_time_as_timestamp.rs
Expand Up @@ -4,7 +4,7 @@ use prost_types::Timestamp;

/// Deserialize `Option<chrono::DateTime<chrono::Utc>>` from `timestampValue` or `nullValue
///
/// # Example
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -62,7 +62,7 @@ where

/// Serialize `Option<chrono::DateTime<chrono::Utc>` as `referenceValue` or `nullValue`.
///
/// # Example
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
Expand Down
87 changes: 87 additions & 0 deletions src/with/option_lat_lng.rs
Expand Up @@ -2,13 +2,100 @@

use google_api_proto::google::r#type::LatLng;

/// Deserialize `Option<LatLng>` 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<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<LatLng>, 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 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<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<LatLng>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down
96 changes: 96 additions & 0 deletions src/with/option_string_as_reference.rs
@@ -1,12 +1,108 @@
//! (De)serialize `Option<String>` as `referenceValue` or `nullValue`.

/// Deserialize `Option<String>` 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<Option<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
crate::de::with::string_as_reference::deserialize_option_string_as_reference(deserializer)
}

/// Serialize `Option<String>` 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<S>(option_string: &Option<String>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down
4 changes: 2 additions & 2 deletions src/with/option_time_offset_date_time_as_timestamp.rs
Expand Up @@ -4,7 +4,7 @@ use prost_types::Timestamp;

/// Deserialize `Option<time::OffsetDateTime>` from `timestampValue` or `nullValue
///
/// # Example
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -57,7 +57,7 @@ where

/// Serialize `Option<time::OffsetDateTime>` as `timestampValue` or `nullValue`.
///
/// # Example
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
Expand Down

0 comments on commit a1f9c10

Please sign in to comment.