firebase_rs_sdk/
doctest_support.rs

1//! Helpers used by documentation examples to compile in isolation.
2
3use std::sync::atomic::{AtomicUsize, Ordering};
4use std::sync::Arc;
5
6use crate::app::{initialize_app, FirebaseApp, FirebaseAppSettings, FirebaseOptions};
7use crate::auth::{auth_for_app, register_auth_component, Auth};
8
9static COUNTER: AtomicUsize = AtomicUsize::new(0);
10
11pub async fn get_mock_app() -> FirebaseApp {
12    let app_name = format!("doc-auth-{}", COUNTER.fetch_add(1, Ordering::SeqCst));
13    let options = FirebaseOptions {
14        api_key: Some("DOCTEST_API_KEY".into()),
15        project_id: Some("doctest-project".into()),
16        auth_domain: Some("doctest.firebaseapp.com".into()),
17        ..Default::default()
18    };
19    let settings = FirebaseAppSettings {
20        name: Some(app_name),
21        ..Default::default()
22    };
23
24    initialize_app(options, Some(settings))
25        .await
26        .expect("failed to initialize doctest Firebase app")
27}
28
29pub async fn get_mock_auth(app: Option<FirebaseApp>) -> Arc<Auth> {
30    let app = match app {
31        Some(a) => a,
32        None => get_mock_app().await,
33    };
34    register_auth_component();
35    auth_for_app(app).expect("failed to resolve Auth component")
36}
37
38pub mod auth {
39
40    use crate::auth::{ApplicationVerifier, AuthResult};
41
42    pub struct MockVerifier {
43        pub token: &'static str,
44        pub kind: &'static str,
45    }
46
47    impl ApplicationVerifier for MockVerifier {
48        fn verify(&self) -> AuthResult<String> {
49            Ok(self.token.to_string())
50        }
51
52        fn verifier_type(&self) -> &str {
53            self.kind
54        }
55    }
56}
57
58pub mod firestore {
59    use crate::firestore::{
60        get_firestore, register_firestore_component, Firestore, FirestoreClient,
61    };
62    use std::sync::Arc;
63
64    pub async fn get_mock_firestore(app: Option<super::FirebaseApp>) -> Arc<Firestore> {
65        let app = match app {
66            Some(a) => a,
67            None => super::get_mock_app().await,
68        };
69
70        register_firestore_component();
71        get_firestore(Some(app))
72            .await
73            .expect("failed to resolve Firestore component")
74    }
75
76    pub async fn get_mock_client(app: Option<super::FirebaseApp>) -> FirestoreClient {
77        let firestore = get_mock_firestore(app).await;
78        FirestoreClient::with_http_datastore(Firestore::from_arc(firestore))
79            .expect("failed to create FirestoreClient")
80    }
81}