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
mod bindings;

use std::fmt;

pub use bindings::{
    collection, delete_doc, doc, get_firestore, on_snapshot_doc, on_snapshot_query, query, set_doc,
    set_doc_with_options, CollectionReference, DocumentReference, DocumentSnapshot, Firestore,
    Query, QueryConstraint, QuerySnapshot, SetDocOptions,
};
use bindings::{get_doc as b_get_doc, get_docs as b_get_docs, where_ as b_where};
use wasm_bindgen::{JsCast, JsValue};

pub fn where_<V: Into<JsValue>>(
    field_path: &str,
    op: QueryConstraintOp,
    value: V,
) -> QueryConstraint {
    let value = value.into();

    b_where(field_path, &op.to_string(), value)
}

pub enum QueryConstraintOp {
    /// `<`o
    ///
    LessThan,
    /// `<=`
    LessThanEq,
    /// `>`
    GreaterThan,
    /// `>=`
    GreaterThanEq,
    /// `==`
    Eq,
    /// `!=`
    NotEq,
    /// `array-contains`
    ArrayContains,
    /// `in`
    In,
    /// `array-contains-any`
    ArrayContainsAny,
    /// `not-in`
    NotIn,
}

impl fmt::Display for QueryConstraintOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let str = match self {
            Self::LessThan => "<",
            Self::LessThanEq => "<=",
            Self::GreaterThan => ">",
            Self::GreaterThanEq => ">=",
            Self::Eq => "==",
            Self::NotEq => "!=",
            Self::ArrayContains => "array-contains",
            Self::In => "in",
            Self::ArrayContainsAny => "array-contains-any",
            Self::NotIn => "not-in",
        };

        f.write_str(str)
    }
}

pub async fn get_doc(doc: DocumentReference) -> Result<DocumentSnapshot, JsValue> {
    let snapshot = b_get_doc(doc).await?;

    Ok(snapshot.unchecked_into())
}

pub async fn get_docs(query: Query) -> Result<QuerySnapshot, JsValue> {
    let snapshot = b_get_docs(query).await?;

    Ok(snapshot.unchecked_into())
}