firestore_db_and_auth/documents/
mod.rs

1//! # Firestore Document Access
2//!
3//! Interact with Firestore documents.
4//! Please check the root page of this documentation for examples.
5#![allow(unused_imports, dead_code)]
6use super::dto;
7use super::errors::{extract_google_api_error, extract_google_api_error_async, FirebaseError, Result};
8use super::firebase_rest_to_rust::{document_to_pod, pod_to_document};
9use super::FirebaseAuthBearer;
10
11use serde::{Deserialize, Serialize};
12use std::path::Path;
13
14mod delete;
15mod list;
16mod query;
17mod read;
18mod write;
19
20pub use delete::*;
21pub use list::*;
22pub use query::*;
23pub use read::*;
24pub use write::*;
25
26/// An [`Iterator`] implementation that provides a join method
27///
28/// [`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
29pub trait JoinableIterator: Iterator {
30    fn join(&mut self, sep: &str) -> String
31    where
32        Self::Item: std::fmt::Display,
33    {
34        use std::fmt::Write;
35        match self.next() {
36            None => String::new(),
37            Some(first_elt) => {
38                // estimate lower bound of capacity needed
39                let (lower, _) = self.size_hint();
40                let mut result = String::with_capacity(sep.len() * lower);
41                write!(&mut result, "{}", first_elt).unwrap();
42                for elt in self {
43                    result.push_str(sep);
44                    write!(&mut result, "{}", elt).unwrap();
45                }
46                result
47            }
48        }
49    }
50}
51
52impl<'a, VALUE> JoinableIterator for std::collections::hash_map::Keys<'a, String, VALUE> {}
53
54#[inline]
55fn firebase_url_query(v1: &str) -> String {
56    format!(
57        "https://firestore.googleapis.com/v1/projects/{}/databases/(default)/documents:runQuery",
58        v1
59    )
60}
61
62#[inline]
63fn firebase_url_base(v1: &str) -> String {
64    format!("https://firestore.googleapis.com/v1/{}", v1)
65}
66
67#[inline]
68fn firebase_url_extended(v1: &str, v2: &str, v3: &str) -> String {
69    format!(
70        "https://firestore.googleapis.com/v1/projects/{}/databases/(default)/documents/{}/{}",
71        v1, v2, v3
72    )
73}
74
75#[inline]
76fn firebase_url(v1: &str, v2: &str) -> String {
77    format!(
78        "https://firestore.googleapis.com/v1/projects/{}/databases/(default)/documents/{}?",
79        v1, v2
80    )
81}
82
83/// Converts an absolute path like "projects/{PROJECT_ID}/databases/(default)/documents/my_collection/document_id"
84/// into a relative document path like "my_collection/document_id"
85///
86/// This is usually used to get a suitable path for [`delete`].
87pub fn abs_to_rel(path: &str) -> &str {
88    &path[path.find("(default)").unwrap() + 20..]
89}
90
91#[test]
92fn abs_to_rel_test() {
93    assert_eq!(
94        abs_to_rel("projects/{PROJECT_ID}/databases/(default)/documents/my_collection/document_id"),
95        "my_collection/document_id"
96    );
97}