serde_firestore_value

Function to_value

Source
pub fn to_value<T>(value: &T) -> Result<Value, Error>
where T: Serialize,
Expand description

Serialize an instance of type T to a Firestore Value.

§Examples

#[derive(serde::Serialize)]
struct T {
    b: bool,
    n: i64,
}
assert_eq!(
    to_value(&T { b: true, n: 1 })?,
    Value {
        value_type: Some(ValueType::MapValue(MapValue {
            fields: std::collections::HashMap::from([
                (
                    "b".to_string(),
                    Value {
                        value_type: Some(ValueType::BooleanValue(true))
                    }
                ),
                (
                    "n".to_string(),
                    Value {
                        value_type: Some(ValueType::IntegerValue(1))
                    }
                )
            ])
        }))
    }
);

§Serialize GeoPoint, Reference, and Timestamp

See: with.

§Mapping table

serde data modelFirestore Value
boolbooleanValue
i8integerValue
i16integerValue
i32integerValue
i64integerValue
i128(not supported)
u8integerValue
u16integerValue
u32integerValue
u64(not supported)
u128(not supported)
f32doubleValue
f64doubleValue
charstringValue
stringstringValue
byte arraybytesValue
optionnullValue or (value)
unitnullValue
unit_structnullValue
unit_variantstringValue
newtype_struct(value)
newtype_struct (reference)referenceValue
newtype_variantmapValue ({ (name): (value) })
seqarrayValue
tuplearrayValue
tuple_structarrayValue
tuple_variantmapValue ({ (name): arrayValue })
mapmapValue ({ (key): (value) })
structmapValue ({ (field): (value) })
struct (lat_lng)geoPointValue
struct (timestamp)timestampValue
struct_variantmapValue ({ (name): mapValue })