serde_firestore_value/with/string_as_reference.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
//! (De)serialize `String` as `referenceValue`.
/// Deserialize `String` from `referenceValue`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
///
/// use serde_firestore_value::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 = std::collections::HashMap::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<String, D::Error>
where
D: serde::Deserializer<'de>,
{
crate::de::with::string_as_reference::deserialize_string_as_reference(deserializer)
}
/// Serialize `String` as `referenceValue`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
/// use serde_firestore_value::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 = std::collections::HashMap::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>(s: &str, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
crate::ser::with::string_as_reference::serialize_string_as_reference(s, serializer)
}