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
Self {
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,
config_path,
}

View File

@ -1,7 +1,9 @@
mod database;
mod error;
mod gemini;
use database::Database;
use error::Error;
use gemini::Gemini;
use gtk::glib::DateTime;
@ -18,7 +20,7 @@ impl Identity {
// Constructors
/// 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
let database = Rc::new(Database::new(connection.clone()));
@ -27,14 +29,21 @@ impl Identity {
Some(identity) => identity.id,
None => match database.add(profile_id, true, DateTime::now_local().unwrap(), None) {
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,
gemini: Rc::new(Gemini::new(connection, profile_identity_id)),
}
gemini,
})
}
/// 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 database;
mod error;
mod memory;
use auth::Auth;
use database::Database;
use error::Error;
use memory::Memory;
use sqlite::{Connection, Transaction};
@ -14,7 +16,7 @@ use std::{rc::Rc, sync::RwLock};
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
pub struct Gemini {
pub auth: Rc<Auth>,
// pub database: Rc<Database>,
pub database: Rc<Database>,
pub memory: Rc<Memory>,
}
@ -22,29 +24,50 @@ impl Gemini {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_identity_id: Rc<i64>) -> Self {
// Init children components
let auth = Rc::new(Auth::new(connection.clone()));
pub fn new(
connection: Rc<RwLock<Connection>>,
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 memory = Rc::new(Memory::new());
// Init `Self`
let this = Self {
auth,
database,
memory,
};
// 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) => {
for record in records {
if memory.add(record.id, record.pem).is_err() {
todo!()
if self.memory.add(record.id, record.pem).is_err() {
return Err(Error::MemoryIndex); // @TODO
}
}
}
Err(reason) => todo!("{reason}"),
}
Self {
auth,
// database,
memory,
}
Err(_) => return Err(Error::DatabaseIndex), // @TODO
};
Ok(()) // @TODO
}
// @TODO create new identity API

View File

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