handle construction results, separate index methods

This commit is contained in:
yggverse 2024-11-16 20:27:33 +02:00
parent e60a00c500
commit 611addda42
9 changed files with 105 additions and 40 deletions

View File

@ -96,7 +96,7 @@ impl Profile {
// Result // Result
Self { Self {
bookmark: Rc::new(Bookmark::new(connection.clone(), profile_id.clone())), bookmark: Rc::new(Bookmark::new(connection.clone(), profile_id.clone())),
identity: Rc::new(Identity::new(connection, profile_id)), identity: Rc::new(Identity::new(connection, profile_id).unwrap()), // @TODO handle
database, database,
config_path, config_path,
} }

View File

@ -1,7 +1,9 @@
mod database; mod database;
mod error;
mod gemini; mod gemini;
use database::Database; use database::Database;
use error::Error;
use gemini::Gemini; use gemini::Gemini;
use gtk::glib::DateTime; use gtk::glib::DateTime;
@ -18,7 +20,7 @@ impl Identity {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self { pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Result<Self, Error> {
// Init identity database // Init identity database
let database = Rc::new(Database::new(connection.clone())); let database = Rc::new(Database::new(connection.clone()));
@ -27,14 +29,21 @@ impl Identity {
Some(identity) => identity.id, Some(identity) => identity.id,
None => match database.add(profile_id, true, DateTime::now_local().unwrap(), None) { None => match database.add(profile_id, true, DateTime::now_local().unwrap(), None) {
Ok(id) => id, Ok(id) => id,
Err(_) => todo!(), Err(_) => return Err(Error::Database),
}, },
}); });
Self { // Init gemini component
let gemini = Rc::new(match Gemini::new(connection, profile_identity_id) {
Ok(result) => result,
Err(_) => return Err(Error::Gemini),
});
// Done
Ok(Self {
// database, // database,
gemini: Rc::new(Gemini::new(connection, profile_identity_id)), gemini,
} })
} }
/// Get `pem` record match `request` /// Get `pem` record match `request`

View File

@ -0,0 +1,5 @@
#[derive(Debug)]
pub enum Error {
Database,
Gemini,
}

View File

@ -1,9 +1,11 @@
mod auth; mod auth;
mod database; mod database;
mod error;
mod memory; mod memory;
use auth::Auth; use auth::Auth;
use database::Database; use database::Database;
use error::Error;
use memory::Memory; use memory::Memory;
use sqlite::{Connection, Transaction}; use sqlite::{Connection, Transaction};
@ -14,7 +16,7 @@ use std::{rc::Rc, sync::RwLock};
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates /// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
pub struct Gemini { pub struct Gemini {
pub auth: Rc<Auth>, pub auth: Rc<Auth>,
// pub database: Rc<Database>, pub database: Rc<Database>,
pub memory: Rc<Memory>, pub memory: Rc<Memory>,
} }
@ -22,29 +24,50 @@ impl Gemini {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_identity_id: Rc<i64>) -> Self { pub fn new(
// Init children components connection: Rc<RwLock<Connection>>,
let auth = Rc::new(Auth::new(connection.clone())); profile_identity_id: Rc<i64>,
) -> Result<Self, Error> {
// Init components
let auth = match Auth::new(connection.clone()) {
Ok(auth) => Rc::new(auth),
Err(_) => return Err(Error::AuthInit), // @TODO
};
let database = Rc::new(Database::new(connection, profile_identity_id)); let database = Rc::new(Database::new(connection, profile_identity_id));
let memory = Rc::new(Memory::new()); let memory = Rc::new(Memory::new());
// Init `Self`
let this = Self {
auth,
database,
memory,
};
// Build initial index // Build initial index
match database.records() { Self::index(&this)?;
Ok(this)
}
// Actions
/// Create new `Memory` index from `Database` for `Self`
pub fn index(&self) -> Result<(), Error> {
// Cleanup previous records
self.memory.clear();
// Build new index
match self.database.records() {
Ok(records) => { Ok(records) => {
for record in records { for record in records {
if memory.add(record.id, record.pem).is_err() { if self.memory.add(record.id, record.pem).is_err() {
todo!() return Err(Error::MemoryIndex); // @TODO
} }
} }
} }
Err(reason) => todo!("{reason}"), Err(_) => return Err(Error::DatabaseIndex), // @TODO
} };
Ok(()) // @TODO
Self {
auth,
// database,
memory,
}
} }
// @TODO create new identity API // @TODO create new identity API

View File

@ -1,7 +1,9 @@
mod database; mod database;
mod error;
mod memory; mod memory;
use database::Database; use database::Database;
use error::Error;
use memory::Memory; use memory::Memory;
use sqlite::{Connection, Transaction}; use sqlite::{Connection, Transaction};
@ -9,7 +11,7 @@ use std::{rc::Rc, sync::RwLock};
/// API for `profile_identity_gemini_id` + `url` auth pairs operations /// API for `profile_identity_gemini_id` + `url` auth pairs operations
pub struct Auth { pub struct Auth {
// pub database: Rc<Database>, pub database: Rc<Database>,
pub memory: Rc<Memory>, pub memory: Rc<Memory>,
} }
@ -17,33 +19,45 @@ impl Auth {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>) -> Self { pub fn new(connection: Rc<RwLock<Connection>>) -> Result<Self, Error> {
// Init children components // Init `Self`
let database = Rc::new(Database::new(connection)); let this = Self {
let memory = Rc::new(Memory::new()); database: Rc::new(Database::new(connection)),
memory: Rc::new(Memory::new()),
};
// Build initial index // Build initial index
match database.records(None) { Self::index(&this)?;
// Done
Ok(this)
}
// Actions
/// Create new `Memory` index from `Database` for `Self`
pub fn index(&self) -> Result<(), Error> {
// Clear previous records
self.memory.clear();
// Build new index
match self.database.records(None) {
Ok(records) => { Ok(records) => {
for record in records { for record in records {
if record.is_active { if record.is_active {
if memory if self
.memory
.add(record.url, record.profile_identity_gemini_id) .add(record.url, record.profile_identity_gemini_id)
.is_err() .is_err()
{ {
todo!() return Err(Error::MemoryIndex);
} }
} }
} }
} }
Err(reason) => todo!("{reason}"), Err(_) => return Err(Error::DatabaseIndex),
}
// Return new `Self`
Self {
// database,
memory,
} }
Ok(())
} }
} }
@ -58,6 +72,5 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Delegate migration to childs // Delegate migration to childs
// nothing yet.. // nothing yet..
// Success
Ok(()) Ok(())
} }

View File

@ -0,0 +1,5 @@
#[derive(Debug)]
pub enum Error {
DatabaseIndex,
MemoryIndex,
}

View File

@ -33,11 +33,10 @@ impl Memory {
} }
} }
/* @TODO update feature
/// Cleanup index /// Cleanup index
pub fn clear(&self, url: &str) { pub fn clear(&self) {
self.index.borrow_mut().clear() self.index.borrow_mut().clear()
} */ }
/// Get `profile_identity_gemini_id` vector match given `request` /// Get `profile_identity_gemini_id` vector match given `request`
/// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates)

View File

@ -0,0 +1,6 @@
#[derive(Debug)]
pub enum Error {
AuthInit,
MemoryIndex,
DatabaseIndex,
}

View File

@ -36,4 +36,9 @@ impl Memory {
None => Err(Error::NotFound), None => Err(Error::NotFound),
} }
} }
/// Cleanup index
pub fn clear(&self) {
self.index.borrow_mut().clear()
}
} }