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
38
39
40pub mod auth {
41
42    use crate::auth::{ApplicationVerifier, AuthResult};
43
44    pub struct MockVerifier {
45        pub token: &'static str,
46        pub kind: &'static str,
47    }
48
49    impl ApplicationVerifier for MockVerifier {
50        fn verify(&self) -> AuthResult<String> {
51            Ok(self.token.to_string())
52        }
53
54        fn verifier_type(&self) -> &str {
55            self.kind
56        }
57    }
58
59
60}
61
62
63pub mod firestore {
64    use crate::firestore::{get_firestore, register_firestore_component, Firestore, FirestoreClient};
65    use std::sync::Arc;
66
67    pub async fn get_mock_firestore(app: Option<super::FirebaseApp>) -> Arc<Firestore> {
68        let app =  match app {
69            Some(a) => a,
70            None => super::get_mock_app().await
71        };
72
73        register_firestore_component();
74        get_firestore(Some(app)).await.expect("failed to resolve Firestore component")
75
76    }
77
78    pub async fn get_mock_client(app: Option<super::FirebaseApp>) -> FirestoreClient {
79        let firestore = get_mock_firestore(app).await;
80        FirestoreClient::with_http_datastore(Firestore::from_arc(firestore))
81            .expect("failed to create FirestoreClient")
82    }
83}
84
85
86