remove extra Rc wrappers, disable internal components access

This commit is contained in:
yggverse 2025-03-08 17:16:08 +02:00
parent a874bd8106
commit 6c7cfec5d2
3 changed files with 23 additions and 29 deletions

View File

@ -207,7 +207,7 @@ impl Menu for MenuButton {
move |_| { move |_| {
// Bookmarks // Bookmarks
main_bookmarks.remove_all(); main_bookmarks.remove_all();
for request in profile.bookmark.memory.recent() { for request in profile.bookmark.recent() {
let menu_item = gio::MenuItem::new(Some(&ellipsize(&request, LABEL_MAX_LENGTH)), None); let menu_item = gio::MenuItem::new(Some(&ellipsize(&request, LABEL_MAX_LENGTH)), None);
menu_item.set_action_and_target_value(Some(&format!( menu_item.set_action_and_target_value(Some(&format!(
"{}.{}", "{}.{}",

View File

@ -9,8 +9,8 @@ use sqlite::{Connection, Transaction};
use std::{rc::Rc, sync::RwLock}; use std::{rc::Rc, sync::RwLock};
pub struct Bookmark { pub struct Bookmark {
pub database: Rc<Database>, // permanent storage database: Database, // permanent storage
pub memory: Rc<Memory>, // fast search index memory: Memory, // fast search index
} }
impl Bookmark { impl Bookmark {
@ -19,8 +19,8 @@ impl Bookmark {
/// Create new `Self` /// Create new `Self`
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self> { pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self> {
// Init children components // Init children components
let database = Rc::new(Database::new(connection, profile_id)); let database = Database::new(connection, profile_id);
let memory = Rc::new(Memory::new()); let memory = Memory::new();
// Build initial index // Build initial index
for record in database.records(None)? { for record in database.records(None)? {
@ -38,28 +38,27 @@ impl Bookmark {
self.memory.get(request) self.memory.get(request)
} }
/// Toggle record in `database` and `memory` index /// Toggle bookmark in `database` and `memory` index
/// * return `true` on bookmark created, `false` on deleted /// * return `true` on bookmark create, `false` on delete
pub fn toggle(&self, request: &str) -> Result<bool> { pub fn toggle(&self, request: &str) -> Result<bool> {
// Delete record if exists
if let Some(id) = self.get(request) { if let Some(id) = self.get(request) {
match self.database.delete(id) { self.database.delete(id)?;
Ok(_) => match self.memory.delete(request) { self.memory.delete(request)?;
Ok(_) => Ok(false), Ok(false)
Err(_) => panic!(), // unexpected
},
Err(_) => panic!(), // unexpected
}
// Otherwise, create new record
} else { } else {
match self.database.add(DateTime::now_local()?, request.into()) { self.memory.add(
Ok(id) => match self.memory.add(request.into(), id) { request.into(),
Ok(_) => Ok(true), self.database.add(DateTime::now_local()?, request.into())?,
Err(_) => panic!(), // unexpected )?;
}, Ok(true)
Err(_) => panic!(), // unexpected }
} }
} // @TODO return affected rows on success?
// Getters
/// Get recent requests vector from `memory`, sorted by `ID` DESC
pub fn recent(&self) -> Vec<String> {
self.memory.recent()
} }
} }

View File

@ -70,9 +70,4 @@ impl Memory {
} }
recent recent
} }
/// Get records total
pub fn total(&self) -> usize {
self.index.borrow().len()
}
} }