From 0b4430302344dea23732b347adb9be62a3c84d1e Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 14 Nov 2024 12:48:58 +0200 Subject: [PATCH] handle bookmark toggle action results --- src/app/browser/window.rs | 8 ++++++-- src/app/browser/window/tab.rs | 12 ++++++++++-- src/app/browser/window/tab/error.rs | 5 +++++ src/app/browser/window/tab/item/page.rs | 17 +++++++++++++---- src/app/browser/window/tab/item/page/error.rs | 4 ++++ src/profile/bookmark.rs | 7 ++++--- 6 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 src/app/browser/window/tab/error.rs create mode 100644 src/app/browser/window/tab/item/page/error.rs diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index 4016b2ee..657b2acc 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -49,8 +49,12 @@ impl Window { action.bookmark().connect_activate({ let tab = tab.clone(); - move |position| tab.bookmark(position) - }); // @TODO + move |position| { + if tab.bookmark(position).is_err() { + todo!() + } + } + }); action.pin().connect_activate({ let tab = tab.clone(); diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index b4ed630d..76af33ac 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -1,8 +1,10 @@ mod database; +mod error; mod item; mod menu; mod widget; +use error::Error; use item::Item; use menu::Menu; use widget::Widget; @@ -157,14 +159,20 @@ impl Tab { self.widget.close_all(); } - pub fn bookmark(&self, page_position: Option) { + /// Toggle bookmark for page at position or current page on `None` + /// * return `true` on bookmark created, `false` on deleted + pub fn bookmark(&self, page_position: Option) -> Result { if let Some(page) = self.widget.page(page_position) { if let Some(id) = page.keyword() { if let Some(item) = self.index.borrow().get(&id) { - item.page().bookmark(); + return match item.page().bookmark() { + Ok(result) => Ok(result), + Err(_) => Err(Error::Bookmark), + }; } } } + Err(Error::PageNotFound) } // Toggle pin status for active tab diff --git a/src/app/browser/window/tab/error.rs b/src/app/browser/window/tab/error.rs new file mode 100644 index 00000000..cd27d010 --- /dev/null +++ b/src/app/browser/window/tab/error.rs @@ -0,0 +1,5 @@ +#[derive(Debug)] +pub enum Error { + Bookmark, + PageNotFound, +} diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index d60a3846..abcb56e4 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -1,11 +1,13 @@ mod content; mod database; +mod error; mod input; mod meta; mod navigation; mod widget; use content::Content; +use error::Error; use input::Input; use meta::{Meta, Status}; use navigation::Navigation; @@ -89,12 +91,19 @@ impl Page { // Actions - /// Toggle bookmark for navigation request in profile database - pub fn bookmark(&self) { - self.profile + /// Toggle bookmark for current `profile` by navigation request value + /// * return `true` on bookmark created, `false` on deleted + pub fn bookmark(&self) -> Result { + let result = match self + .profile .bookmark - .toggle(self.navigation.request().widget().gobject().text().as_str()); + .toggle(self.navigation.request().widget().gobject().text().as_str()) + { + Ok(result) => Ok(result), + Err(_) => Err(Error::Bookmark), + }; self.update(); + result } /// Navigate home URL (parsed from current navigation entry) diff --git a/src/app/browser/window/tab/item/page/error.rs b/src/app/browser/window/tab/item/page/error.rs new file mode 100644 index 00000000..0b803432 --- /dev/null +++ b/src/app/browser/window/tab/item/page/error.rs @@ -0,0 +1,4 @@ +#[derive(Debug)] +pub enum Error { + Bookmark, +} diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index 9ef2bf83..7fc54735 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -46,12 +46,13 @@ impl Bookmark { } /// Toggle record in `database` and `memory` index - pub fn toggle(&self, request: &str) -> Result<(), Error> { + /// * return `true` on bookmark created, `false` on deleted + pub fn toggle(&self, request: &str) -> Result { // Delete record if exists if let Ok(id) = self.get(request) { match self.database.delete(id) { Ok(_) => match self.memory.delete(request) { - Ok(_) => Ok(()), + Ok(_) => Ok(false), Err(_) => Err(Error::MemoryDelete), }, Err(_) => Err(Error::DatabaseDelete), @@ -63,7 +64,7 @@ impl Bookmark { .add(DateTime::now_local().unwrap(), request.into()) { Ok(id) => match self.memory.add(request.into(), id) { - Ok(_) => Ok(()), + Ok(_) => Ok(true), Err(_) => Err(Error::MemoryAdd), }, Err(_) => Err(Error::DatabaseAdd),