#firestore #serde #serialization #google #derive-debug #gcloud #string

serde-firestore-value

A serde (de)serializer using Firestore Value as its data format

46 releases (13 breaking)

0.14.1 Nov 28, 2024
0.13.0 Nov 9, 2024
0.8.8 Jul 31, 2024
0.7.3 Mar 27, 2024
0.2.0 Nov 2, 2023

#320 in Encoding

Download history 54/week @ 2024-08-16 194/week @ 2024-08-23 195/week @ 2024-08-30 210/week @ 2024-09-06 321/week @ 2024-09-13 97/week @ 2024-09-20 430/week @ 2024-09-27 331/week @ 2024-10-04 78/week @ 2024-10-11 131/week @ 2024-10-18 184/week @ 2024-10-25 136/week @ 2024-11-01 371/week @ 2024-11-08 134/week @ 2024-11-15 121/week @ 2024-11-22 161/week @ 2024-11-29

866 downloads per month
Used in firestore-structured-quer…

MIT/Apache

155KB
3K SLoC

serde-firestore-value

A serde (de)serializer using Firestore Value as its data format.

ci crates.io docs.rs license

use googleapis_tonic_google_firestore_v1::google::firestore::v1::{value::ValueType, ArrayValue, MapValue, Value};
use serde_firestore_value::{LatLng, Reference, Timestamp};

#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
struct T {
    b: bool,
    i: i64,
    d: f64,
    t: Timestamp,
    s: String,
    r: Reference,
    g: LatLng,
    a: Vec<Option<i64>>,
    // You can use `btree-map` feature instead of `hash-map` feature.
    m: std::collections::HashMap<String, bool>,
}

let t = T {
    b: true,
    i: 1_i64,
    d: 2_f64,
    t: Timestamp {
        seconds: 3_i64,
        nanos: 4_i32,
    },
    s: "s".to_string(),
    r: Reference("projects/p/databases/d/documents/n".to_string()),
    g: LatLng {
        latitude: 5_f64,
        longitude: 6_f64,
    },
    a: vec![Some(1), Some(2), None],
    m: {
        let mut m = std::collection::Map::new();
        m.insert("a".to_string(), false);
        m.insert("b".to_string(), true);
        m
    },
};
let value = Value {
    value_type: Some(ValueType::MapValue(MapValue {
        fields: {
            let mut fields = std::collections::HashMap::new();
            fields.insert(
                "b".to_string(),
                Value {
                    value_type: Some(ValueType::BooleanValue(true)),
                },
            );
            fields.insert(
                "i".to_string(),
                Value {
                    value_type: Some(ValueType::IntegerValue(1)),
                },
            );
            fields.insert(
                "d".to_string(),
                Value {
                    value_type: Some(ValueType::DoubleValue(2_f64)),
                },
            );
            fields.insert(
                "t".to_string(),
                Value {
                    value_type: Some(ValueType::TimestampValue(prost_types::Timestamp {
                        seconds: 3_i64,
                        nanos: 4_i32,
                    })),
                },
            );
            fields.insert(
                "s".to_string(),
                Value {
                    value_type: Some(ValueType::StringValue("s".to_string())),
                },
            );
            fields.insert(
                "r".to_string(),
                Value {
                    value_type: Some(ValueType::ReferenceValue(
                        "projects/p/databases/d/documents/n".to_string(),
                    )),
                },
            );
            fields.insert(
                "g".to_string(),
                Value {
                    value_type: Some(ValueType::GeoPointValue(
                        googleapis_tonic_google_firestore_v1::google::r#type::LatLng {
                            latitude: 5_f64,
                            longitude: 6_f64,
                        },
                    )),
                },
            );
            fields.insert(
                "a".to_string(),
                Value {
                    value_type: Some(ValueType::ArrayValue(ArrayValue {
                        values: vec![
                            Value {
                                value_type: Some(ValueType::IntegerValue(1)),
                            },
                            Value {
                                value_type: Some(ValueType::IntegerValue(2)),
                            },
                            Value {
                                value_type: Some(ValueType::NullValue(0)),
                            },
                        ],
                    })),
                },
            );
            fields.insert(
                "m".to_string(),
                Value {
                    value_type: Some(ValueType::MapValue(MapValue {
                        fields: {
                            let mut fields = std::collections::HashMap::new();
                            fields.insert(
                                "a".to_string(),
                                Value {
                                    value_type: Some(ValueType::BooleanValue(false)),
                                },
                            );
                            fields.insert(
                                "b".to_string(),
                                Value {
                                    value_type: Some(ValueType::BooleanValue(true)),
                                },
                            );
                            fields
                        },
                    })),
                },
            );
            fields
        },
    })),
};

let serialized = serde_firestore_value::to_value(&t)?;
assert_eq!(serialized, value);

let deserialized = serde_firestore_value::from_value::<T>(&serialized)?;
assert_eq!(deserialized, t);

Dependencies

~7–13MB
~143K SLoC