firebase_app_sdk/
lib.rs

1//! # Firebase App SDK
2//! The Firebase App SDK for Rust provides a Rust API for integrating Firebase services into your Rust applications. This SDK allows you to easily access Firebase features such as authentication, real-time database, storage, and more.
3//! ## Usage
4//! To interact with Firebase services, you need to create an `App` instance with the required options. Here's an example:
5//! ```rust
6//! use firebase_app::{App,DEFAULT,Options};
7//! 
8//! fn main() {
9//!     let options = Options::new("YOUR_APPLICATION_ID", "YOUR_API_KEY")
10//!         .with_database_url("YOUR_DATABASE_URL")
11//!         .with_storage_bucket("YOUR_STORAGE_BUCKET");
12//!     
13//!     let app = App::new(DEFAULT, options);
14//! }
15//! ```
16//! Once you have created an App instance, you can pass it to other Firebase services for further interactions. Here's an example of using the app instance with the Firebase Authentication service:
17//!
18//! ```rust
19//! use firebase_app::App;
20//! use firebase_app::Options;
21//! use firebase_auth::auth::Authentication;
22//! 
23//! const DEFAULT: &str = "DEFAULT";
24//! 
25//! fn main() {
26//!     let options = Options::new("YOUR_APPLICATION_ID", "YOUR_API_KEY")
27//!         .with_auth_domain("YOUR_AUTH_DOMAIN");
28//!     
29//!     let app = App::new(DEFAULT, options);
30//!     
31//!     // Use the app instance with the Firebase Authentication service
32//!     let auth = Authentication::new(&app);
33//!     
34//!     // Perform authentication operations using the `auth` instance
35//!    // ...
36//! }
37//! ```
38//! Please note that the above examples are simplified and serve as a starting point. Refer to the documentation of each Firebase service for detailed usage instructions.
39
40///Represents a default value or identifier in the context of the Firebase application.
41pub const DEFAULT : &str = "DEFAULT";
42
43/// Represents an application with its associated options.
44pub struct App<'a> {
45    /// The name of the application.
46    pub name: &'a str,
47    /// The options for the application.
48    pub options: Options<'a>,
49}
50
51/// Represents the configurable options for an application.
52pub struct Options<'a> {
53    /// The application ID.
54    pub application_id: &'a str,
55    /// The API key for accessing the application.
56    pub api_key: &'a str,
57    /// The URL of the database (optional).
58    pub database_url: Option<&'a str>,
59    /// The Google Analytics tracking ID (optional).
60    pub ga_tracking_id: Option<&'a str>,
61    /// The storage bucket (optional).
62    pub storage_bucket: Option<&'a str>,
63    /// The project ID (optional).
64    pub project_id: Option<&'a str>,
65    /// The Google Cloud Messaging (GCM) sender ID (optional).
66    pub gcm_sender_id: Option<&'a str>,
67    /// The authentication domain (optional).
68    pub auth_domain: Option<&'a str>,
69}
70
71impl<'a> App<'a> {
72    /// Creates a new instance of the `App` struct.
73    ///
74    /// # Arguments
75    ///
76    /// * `name` - The name of the application.
77    /// * `options` - The options for the application.
78    ///
79    /// # Returns
80    ///
81    /// A new `App` instance.
82    pub fn new(name: &'a str, options: Options<'a>) -> Self {
83        /*let db : Database = open_database(DATABASE_NAME).unwrap();
84
85        if name != DEFAULT && db.contains_key(DEFAULT).unwrap_or(false) {
86            return Err("Error: Database does not contain default value".to_string());
87        };
88
89        if db.contains_key(name).unwrap_or(false) {
90            return Err(format!("Error: Database already contains app named [{}] ",name));
91        };
92
93        let app = App {
94            name,
95            options,
96        };
97
98        let serialized = serde_json::to_string(&app).expect("Error serializing app");
99        let bytes = serialized.as_bytes();
100
101        db.insert(name,SledVector::from(bytes)).expect("Error writing app instance to database");*/
102        App {
103            name,
104            options,
105        }
106    }
107}
108
109impl<'a> Options<'a> {
110    /// Creates a new instance of the `Options` struct.
111    ///
112    /// # Arguments
113    ///
114    /// * `application_id` - The application ID.
115    /// * `api_key` - The API key for accessing the application.
116    ///
117    /// # Returns
118    ///
119    /// A new `Options` instance.
120    pub const fn new(application_id: &'a str, api_key: &'a str) -> Self {
121        Options {
122            application_id,
123            api_key,
124            database_url: None,
125            ga_tracking_id: None,
126            storage_bucket: None,
127            project_id: None,
128            gcm_sender_id: None,
129            auth_domain: None,
130        }
131    }
132
133    /// Sets the database URL for the options.
134    ///
135    /// # Arguments
136    ///
137    /// * `database_url` - The URL of the database.
138    ///
139    /// # Returns
140    ///
141    /// The modified `Options` instance.
142    pub const fn with_database_url(mut self, database_url: &'a str) -> Self {
143        self.database_url = Some(database_url);
144        self
145    }
146
147    /// Sets the Google Analytics tracking ID for the options.
148    ///
149    /// # Arguments
150    ///
151    /// * `ga_tracking_id` - The Google Analytics tracking ID.
152    ///
153    /// # Returns
154    ///
155    /// The modified `Options` instance.
156    pub const fn with_ga_tracking_id(mut self, ga_tracking_id: &'a str) -> Self {
157        self.ga_tracking_id = Some(ga_tracking_id);
158        self
159    }
160
161    /// Sets the storage bucket for the options.
162    ///
163    /// # Arguments
164    ///
165    /// * `storage_bucket` - The storage bucket.
166    ///
167    /// # Returns
168    ///
169    /// The modified `Options` instance.
170    pub const fn with_storage_bucket(mut self, storage_bucket: &'a str) -> Self {
171        self.storage_bucket = Some(storage_bucket);
172        self
173    }
174
175    /// Sets the project ID for the options.
176    ///
177    /// # Arguments
178    ///
179    /// * `project_id` - The project ID.
180    ///
181    /// # Returns
182    ///
183    /// The modified `Options` instance.
184    pub const fn with_project_id(mut self, project_id: &'a str) -> Self {
185        self.project_id = Some(project_id);
186        self
187    }
188
189    /// Sets the Google Cloud Messaging (GCM) sender ID for the options.
190    ///
191    /// # Arguments
192    ///
193    /// * `gcm_sender_id` - The GCM sender ID.
194    ///
195    /// # Returns
196    ///
197    /// The modified `Options` instance.
198    pub const fn with_gcm_sender_id(mut self, gcm_sender_id: &'a str) -> Self {
199        self.gcm_sender_id = Some(gcm_sender_id);
200        self
201    }
202
203    /// Sets the authentication domain for the options.
204    ///
205    /// # Arguments
206    ///
207    /// * `auth_domain` - The authentication domain.
208    ///
209    /// # Returns
210    ///
211    /// The modified `Options` instance.
212    pub const fn with_auth_domain(mut self, auth_domain: &'a str) -> Self {
213        self.auth_domain = Some(auth_domain);
214        self
215    }
216}
217
218#[cfg(test)]
219mod tests {
220    use super::*;
221
222    #[test]
223    fn create_app_instance(){
224        App::new(DEFAULT,Options::new("..","..."));
225    }
226}