implement result handler

This commit is contained in:
yggverse 2024-11-14 12:00:53 +02:00
parent ffeaad91f3
commit f338b93217
4 changed files with 51 additions and 34 deletions

View File

@ -1,7 +1,9 @@
mod database;
mod error;
mod memory;
use database::Database;
use error::Error;
use memory::Memory;
use gtk::glib::DateTime;
@ -24,7 +26,9 @@ impl Bookmark {
// Build initial index
for record in database.records(None) {
memory.set(record.request, record.time)
if memory.add(record.request, record.time).is_err() {
todo!()
}
}
// Return new `Self`
@ -44,21 +48,30 @@ impl Bookmark {
}
}
/// Toggle record in bookmarks database, update emory index
pub fn toggle(&self, request: &str) {
/// Toggle record in `database` and `memory` index
pub fn toggle(&self, request: &str) -> Result<(), Error> {
// Get current timestamp for new record
let time = DateTime::now_local().unwrap();
// Delete record if exists
if self.has_request(request, false) {
match self.database.delete(request) {
Ok(_) => self.memory.delete(request),
Err(_) => todo!(),
Ok(_) => match self.memory.delete(request) {
Ok(_) => Ok(()),
Err(_) => Err(Error::MemoryDelete),
},
Err(_) => Err(Error::DatabaseDelete),
}
// Otherwise, create new record
} else {
match self.database.add(time.clone(), request.into()) {
Ok(_) => self.memory.set(request.into(), time),
Err(_) => todo!(),
};
}
Ok(_) => match self.memory.add(request.into(), time) {
Ok(_) => Ok(()),
Err(_) => Err(Error::MemoryAdd),
},
Err(_) => Err(Error::DatabaseAdd),
}
} // @TODO return affected rows on success?
}
}

View File

@ -0,0 +1,6 @@
pub enum Error {
DatabaseAdd,
DatabaseDelete,
MemoryAdd,
MemoryDelete,
}

View File

@ -1,7 +1,10 @@
mod error;
use error::Error;
use gtk::glib::DateTime;
use std::{cell::RefCell, collections::HashMap};
/// Reduce disk usage by cache results in memory
/// Reduce disk usage by cache Bookmarks index in memory
pub struct Memory {
index: RefCell<HashMap<String, DateTime>>,
}
@ -18,33 +21,24 @@ impl Memory {
// Actions
/// Set new record
/// * replace existing record with new value
pub fn set(&self, request: String, time: DateTime) {
// Borrow index to update
let mut index = self.index.borrow_mut();
// Cleanup previous record
if index.get(&request).is_some() {
index.remove(&request);
}
// Insert new record with actual data
index.insert(request, time);
}
/// Check request exist in memory index
pub fn delete(&self, request: &str) {
// Borrow index to update
let mut index = self.index.borrow_mut();
// Delete record (if exist)
if index.get(request).is_some() {
index.remove(request);
/// Add new record for given `request`
/// * validates record with same key does not exist yet
pub fn add(&self, request: String, time: DateTime) -> Result<(), Error> {
match self.index.borrow_mut().insert(request, time) {
Some(_) => Err(Error::Overwrite),
None => Ok(()),
}
}
/// Check request exist in memory index
/// Delete record from index by `request`
pub fn delete(&self, request: &str) -> Result<(), Error> {
match self.index.borrow_mut().remove(request) {
Some(_) => Ok(()),
None => Err(Error::NotFound),
}
}
/// Check `request` exist in memory index
pub fn is_exist(&self, request: &str) -> bool {
self.index.borrow().get(request).is_some()
}

View File

@ -0,0 +1,4 @@
pub enum Error {
NotFound,
Overwrite,
}