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

@ -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(())
}