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 database;
mod error;
mod memory; mod memory;
use database::Database; use database::Database;
use error::Error;
use memory::Memory; use memory::Memory;
use gtk::glib::DateTime; use gtk::glib::DateTime;
@ -24,7 +26,9 @@ impl Bookmark {
// Build initial index // Build initial index
for record in database.records(None) { 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` // Return new `Self`
@ -44,21 +48,30 @@ impl Bookmark {
} }
} }
/// Toggle record in bookmarks database, update emory index /// Toggle record in `database` and `memory` index
pub fn toggle(&self, request: &str) { pub fn toggle(&self, request: &str) -> Result<(), Error> {
// Get current timestamp for new record
let time = DateTime::now_local().unwrap(); let time = DateTime::now_local().unwrap();
// Delete record if exists
if self.has_request(request, false) { if self.has_request(request, false) {
match self.database.delete(request) { match self.database.delete(request) {
Ok(_) => self.memory.delete(request), Ok(_) => match self.memory.delete(request) {
Err(_) => todo!(), Ok(_) => Ok(()),
Err(_) => Err(Error::MemoryDelete),
},
Err(_) => Err(Error::DatabaseDelete),
} }
// Otherwise, create new record
} else { } else {
match self.database.add(time.clone(), request.into()) { match self.database.add(time.clone(), request.into()) {
Ok(_) => self.memory.set(request.into(), time), Ok(_) => match self.memory.add(request.into(), time) {
Err(_) => todo!(), 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 gtk::glib::DateTime;
use std::{cell::RefCell, collections::HashMap}; 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 { pub struct Memory {
index: RefCell<HashMap<String, DateTime>>, index: RefCell<HashMap<String, DateTime>>,
} }
@ -18,33 +21,24 @@ impl Memory {
// Actions // Actions
/// Set new record /// Add new record for given `request`
/// * replace existing record with new value /// * validates record with same key does not exist yet
pub fn set(&self, request: String, time: DateTime) { pub fn add(&self, request: String, time: DateTime) -> Result<(), Error> {
// Borrow index to update match self.index.borrow_mut().insert(request, time) {
let mut index = self.index.borrow_mut(); Some(_) => Err(Error::Overwrite),
None => Ok(()),
// 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);
} }
} }
/// 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 { pub fn is_exist(&self, request: &str) -> bool {
self.index.borrow().get(request).is_some() self.index.borrow().get(request).is_some()
} }

View File

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