firebase_rs_sdk/firestore/api/
converter.rs

1use std::collections::BTreeMap;
2
3use crate::firestore::error::FirestoreResult;
4use crate::firestore::value::{FirestoreValue, MapValue};
5
6/// Trait describing how to convert between user models and Firestore maps.
7///
8/// This mirrors the modular JS `FirestoreDataConverter` contract: writes use
9/// `to_map`, reads use `from_map`, and callers choose the `Model` type they
10/// want to surface.
11pub trait FirestoreDataConverter: Send + Sync + Clone + 'static {
12    /// The strongly typed model associated with this converter.
13    type Model: Clone;
14
15    /// Encodes the user model into a Firestore map for writes.
16    fn to_map(&self, value: &Self::Model) -> FirestoreResult<BTreeMap<String, FirestoreValue>>;
17
18    /// Decodes a Firestore map into the user model for reads.
19    fn from_map(&self, value: &MapValue) -> FirestoreResult<Self::Model>;
20}
21
22/// Default converter that leaves Firestore maps unchanged (raw JSON-style data).
23#[derive(Clone, Default)]
24pub struct PassthroughConverter;
25
26impl FirestoreDataConverter for PassthroughConverter {
27    type Model = BTreeMap<String, FirestoreValue>;
28
29    fn to_map(&self, value: &Self::Model) -> FirestoreResult<BTreeMap<String, FirestoreValue>> {
30        Ok(value.clone())
31    }
32
33    fn from_map(&self, value: &MapValue) -> FirestoreResult<Self::Model> {
34        Ok(value.fields().clone())
35    }
36}