mod database;
mod error;
mod gemini;
use database::Database;
pub use error::Error;
use gemini::Gemini;
use sqlite::{Connection, Transaction};
use std::{rc::Rc, sync::RwLock};
pub struct Identity {
pub gemini: Rc<Gemini>,
}
impl Identity {
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Result<Self, Error> {
let database = Rc::new(Database::new(connection.clone()));
let profile_identity_id = Rc::new(match database.active() {
Ok(result) => match result {
Some(identity) => identity.id,
None => match database.add(profile_id, true) {
Ok(id) => id,
Err(reason) => return Err(Error::Database(reason)),
},
},
Err(reason) => return Err(Error::Database(reason)),
});
let gemini = Rc::new(match Gemini::new(connection, profile_identity_id) {
Ok(result) => result,
Err(reason) => return Err(Error::Gemini(reason)),
});
Ok(Self {
gemini,
})
}
}
pub fn migrate(tx: &Transaction) -> Result<(), String> {
if let Err(reason) = database::init(tx) {
return Err(reason.to_string());
}
gemini::migrate(tx)?;
Ok(())
}