From f338b932174f0b423a421f6a0493753e2aed6943 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 14 Nov 2024 12:00:53 +0200 Subject: [PATCH] implement result handler --- src/profile/bookmark.rs | 31 ++++++++++++++------ src/profile/bookmark/error.rs | 6 ++++ src/profile/bookmark/memory.rs | 44 ++++++++++++---------------- src/profile/bookmark/memory/error.rs | 4 +++ 4 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 src/profile/bookmark/error.rs create mode 100644 src/profile/bookmark/memory/error.rs diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index a7559540..40be357a 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -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? } } diff --git a/src/profile/bookmark/error.rs b/src/profile/bookmark/error.rs new file mode 100644 index 00000000..1ce8b543 --- /dev/null +++ b/src/profile/bookmark/error.rs @@ -0,0 +1,6 @@ +pub enum Error { + DatabaseAdd, + DatabaseDelete, + MemoryAdd, + MemoryDelete, +} diff --git a/src/profile/bookmark/memory.rs b/src/profile/bookmark/memory.rs index a8f4f3c7..c2ad9c28 100644 --- a/src/profile/bookmark/memory.rs +++ b/src/profile/bookmark/memory.rs @@ -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>, } @@ -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() } diff --git a/src/profile/bookmark/memory/error.rs b/src/profile/bookmark/memory/error.rs new file mode 100644 index 00000000..37e2945c --- /dev/null +++ b/src/profile/bookmark/memory/error.rs @@ -0,0 +1,4 @@ +pub enum Error { + NotFound, + Overwrite, +}