handle memory clear errors

This commit is contained in:
yggverse 2024-11-23 11:48:02 +02:00
parent efa3bc48c3
commit 8a4e979d48
8 changed files with 33 additions and 11 deletions

View File

@ -87,8 +87,10 @@ impl Gemini {
/// Create new `Memory` index from `Database` for `Self`
pub fn index(&self) -> Result<(), Error> {
// Cleanup previous records
self.memory.clear();
// Clear previous records
if let Err(reason) = self.memory.clear() {
return Err(Error::MemoryClear(reason));
}
// Build new index
match self.database.records() {
@ -101,7 +103,8 @@ impl Gemini {
}
Err(reason) => return Err(Error::DatabaseIndex(reason)),
};
Ok(()) // @TODO
Ok(())
}
}

View File

@ -84,7 +84,9 @@ impl Auth {
/// Create new `Memory` index from `Database` for `Self`
pub fn index(&self) -> Result<(), Error> {
// Clear previous records
self.memory.clear();
if let Err(reason) = self.memory.clear() {
return Err(Error::MemoryClear(reason));
}
// Build new index
match self.database.records(None) {
@ -100,6 +102,7 @@ impl Auth {
}
Err(reason) => return Err(Error::DatabaseIndex(reason)),
}
Ok(())
}
}

View File

@ -4,5 +4,6 @@ pub enum Error {
DatabaseRecordCreate(i64, String, sqlite::Error),
DatabaseRecordDelete(i64, sqlite::Error),
DatabaseRecordsRead(String, sqlite::Error),
MemoryClear(super::memory::Error),
MemoryIndex(super::memory::Error),
}

View File

@ -1,4 +1,4 @@
mod error;
pub mod error;
pub use error::Error;
use std::{cell::RefCell, collections::HashMap};
@ -34,8 +34,14 @@ impl Memory {
}
/// Cleanup index
pub fn clear(&self) {
self.index.borrow_mut().clear()
pub fn clear(&self) -> Result<(), Error> {
let mut index = self.index.borrow_mut();
index.clear();
if index.is_empty() {
Ok(())
} else {
Err(Error::Clear)
}
}
/// Get `profile_identity_gemini_id` vector match given `request`

View File

@ -1,4 +1,5 @@
#[derive(Debug)]
pub enum Error {
Clear,
Overwrite(i64),
}

View File

@ -3,6 +3,7 @@ pub enum Error {
AuthInit(super::auth::Error),
DatabaseIndex(sqlite::Error),
DatabaseRecordCreate(sqlite::Error),
MemoryClear(super::memory::Error),
MemoryIndex(i64),
Certificate(Box<dyn std::error::Error>),
}

View File

@ -1,5 +1,5 @@
mod error;
use error::Error;
pub mod error;
pub use error::Error;
use std::{cell::RefCell, collections::HashMap};
@ -38,7 +38,13 @@ impl Memory {
}
/// Cleanup index
pub fn clear(&self) {
self.index.borrow_mut().clear()
pub fn clear(&self) -> Result<(), Error> {
let mut index = self.index.borrow_mut();
index.clear();
if index.is_empty() {
Ok(())
} else {
Err(Error::Clear)
}
}
}

View File

@ -1,5 +1,6 @@
#[derive(Debug)]
pub enum Error {
Clear,
NotFound(i64),
Overwrite(String),
}