From 4002c94a4a208ff97c14c8cebfa898b8650c5616 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 13 Mar 2025 17:25:07 +0200 Subject: [PATCH] remove extra rc wrapper --- src/profile.rs | 24 ++++++++++++------------ src/profile/bookmark.rs | 2 +- src/profile/bookmark/database.rs | 10 +++++----- src/profile/history.rs | 2 +- src/profile/history/database.rs | 12 ++++++------ src/profile/identity.rs | 19 ++++++++----------- src/profile/identity/database.rs | 12 ++++++------ src/profile/search.rs | 2 +- src/profile/search/database.rs | 26 +++++++++++++------------- 9 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/profile.rs b/src/profile.rs index 5bcc7835..dca5076a 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -22,11 +22,11 @@ const BRANCH: &str = "master"; const DB_NAME: &str = "database.sqlite3"; pub struct Profile { - pub bookmark: Rc, - pub database: Rc, - pub history: Rc, - pub identity: Rc, - pub search: Rc, + pub bookmark: Bookmark, + pub database: Database, + pub history: History, + pub identity: Identity, + pub search: Search, pub config_path: PathBuf, } @@ -69,19 +69,19 @@ impl Profile { } // unlock database // Init model - let database = Rc::new(Database::build(&connection)); + let database = Database::build(&connection); // Get active profile or create new one - let profile_id = Rc::new(match database.active()? { + let profile_id = match database.active()? { Some(profile) => profile.id, None => database.add(true, DateTime::now_local()?, None)?, - }); + }; // Init components - let bookmark = Rc::new(Bookmark::build(&connection, &profile_id)?); - let history = Rc::new(History::build(&connection, &profile_id)?); - let search = Rc::new(Search::build(&connection, &profile_id)?); - let identity = Rc::new(Identity::build(&connection, &profile_id)?); + let bookmark = Bookmark::build(&connection, profile_id)?; + let history = History::build(&connection, profile_id)?; + let search = Search::build(&connection, profile_id)?; + let identity = Identity::build(&connection, profile_id)?; // Result Ok(Self { diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index a2df1785..928a83f9 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -19,7 +19,7 @@ impl Bookmark { // Constructors /// Create new `Self` - pub fn build(connection: &Rc>, profile_id: &Rc) -> Result { + pub fn build(connection: &Rc>, profile_id: i64) -> Result { // Init children components let database = Database::new(connection, profile_id); let memory = RefCell::new(Memory::new()); diff --git a/src/profile/bookmark/database.rs b/src/profile/bookmark/database.rs index feb35adb..84159ea5 100644 --- a/src/profile/bookmark/database.rs +++ b/src/profile/bookmark/database.rs @@ -6,17 +6,17 @@ use std::{rc::Rc, sync::RwLock}; pub struct Database { connection: Rc>, - profile_id: Rc, // multi-profile relationship + profile_id: i64, } impl Database { // Constructors /// Create new `Self` - pub fn new(connection: &Rc>, profile_id: &Rc) -> Self { + pub fn new(connection: &Rc>, profile_id: i64) -> Self { Self { connection: connection.clone(), - profile_id: profile_id.clone(), + profile_id, } } @@ -26,7 +26,7 @@ impl Database { pub fn records(&self, request: Option<&str>, title: Option<&str>) -> Result> { let readable = self.connection.read().unwrap(); // @TODO let tx = readable.unchecked_transaction()?; - select(&tx, *self.profile_id, request, title) + select(&tx, self.profile_id, request, title) } // Setters @@ -36,7 +36,7 @@ impl Database { pub fn add(&self, time: DateTime, request: &str, title: Option<&str>) -> Result { let mut writable = self.connection.write().unwrap(); // @TODO let tx = writable.transaction()?; - let id = insert(&tx, *self.profile_id, time, request, title)?; + let id = insert(&tx, self.profile_id, time, request, title)?; tx.commit()?; Ok(id) } diff --git a/src/profile/history.rs b/src/profile/history.rs index 31e06ed5..54a2795a 100644 --- a/src/profile/history.rs +++ b/src/profile/history.rs @@ -19,7 +19,7 @@ impl History { // Constructors /// Create new `Self` - pub fn build(connection: &Rc>, profile_id: &Rc) -> Result { + pub fn build(connection: &Rc>, profile_id: i64) -> Result { // Init children components let database = Database::build(connection, profile_id); let memory = RefCell::new(Memory::new()); diff --git a/src/profile/history/database.rs b/src/profile/history/database.rs index 2adf7c8a..9e3f442e 100644 --- a/src/profile/history/database.rs +++ b/src/profile/history/database.rs @@ -6,17 +6,17 @@ use std::{rc::Rc, sync::RwLock}; pub struct Database { connection: Rc>, - profile_id: Rc, // multi-profile relationship + profile_id: i64, } impl Database { // Constructors /// Create new `Self` - pub fn build(connection: &Rc>, profile_id: &Rc) -> Self { + pub fn build(connection: &Rc>, profile_id: i64) -> Self { Self { connection: connection.clone(), - profile_id: profile_id.clone(), + profile_id, } } @@ -26,7 +26,7 @@ impl Database { pub fn records(&self, request: Option<&str>, title: Option<&str>) -> Result> { let readable = self.connection.read().unwrap(); // @TODO let tx = readable.unchecked_transaction()?; - select(&tx, *self.profile_id, request, title) + select(&tx, self.profile_id, request, title) } // Actions @@ -36,7 +36,7 @@ impl Database { pub fn add(&self, item: &Item) -> Result { let mut writable = self.connection.write().unwrap(); // @TODO let tx = writable.transaction()?; - let id = insert(&tx, *self.profile_id, item)?; + let id = insert(&tx, self.profile_id, item)?; tx.commit()?; Ok(id) } @@ -44,7 +44,7 @@ impl Database { pub fn update(&self, item: &Item) -> Result { let mut writable = self.connection.write().unwrap(); // @TODO let tx = writable.transaction()?; - let affected = update(&tx, *self.profile_id, item)?; + let affected = update(&tx, self.profile_id, item)?; tx.commit()?; Ok(affected) } diff --git a/src/profile/identity.rs b/src/profile/identity.rs index d54ad2c9..9d44545a 100644 --- a/src/profile/identity.rs +++ b/src/profile/identity.rs @@ -17,23 +17,20 @@ use std::{rc::Rc, sync::RwLock}; /// /// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates pub struct Identity { - pub auth: Rc, - pub database: Rc, - pub memory: Rc, -} + pub auth: Auth, + pub database: Database, + pub memory: Memory, +} // @TODO remove pub access impl Identity { // Constructors /// Create new `Self` - pub fn build( - connection: &Rc>, - profile_identity_id: &Rc, - ) -> Result { + pub fn build(connection: &Rc>, profile_identity_id: i64) -> Result { // Init components - let auth = Rc::new(Auth::build(connection)?); - let database = Rc::new(Database::build(connection, profile_identity_id)); - let memory = Rc::new(Memory::new()); + let auth = Auth::build(connection)?; + let database = Database::build(connection, profile_identity_id); + let memory = Memory::new(); // Init `Self` let this = Self { diff --git a/src/profile/identity/database.rs b/src/profile/identity/database.rs index 9833d95c..90324a57 100644 --- a/src/profile/identity/database.rs +++ b/src/profile/identity/database.rs @@ -10,17 +10,17 @@ pub struct Table { /// Storage for Gemini auth certificates pub struct Database { connection: Rc>, - profile_id: Rc, // multi-profile relationship + profile_id: i64, } impl Database { // Constructors /// Create new `Self` - pub fn build(connection: &Rc>, profile_id: &Rc) -> Self { + pub fn build(connection: &Rc>, profile_id: i64) -> Self { Self { connection: connection.clone(), - profile_id: profile_id.clone(), + profile_id, } } @@ -33,7 +33,7 @@ impl Database { let tx = writable.transaction()?; // Create new record - insert(&tx, *self.profile_id, pem)?; + insert(&tx, self.profile_id, pem)?; // Hold insert ID for result let id = last_insert_id(&tx); @@ -65,7 +65,7 @@ impl Database { pub fn record(&self, id: i64) -> Result, Error> { let readable = self.connection.read().unwrap(); let tx = readable.unchecked_transaction()?; - let records = select(&tx, *self.profile_id)?; // @TODO single record query + let records = select(&tx, self.profile_id)?; // @TODO single record query for record in records { if record.id == id { @@ -80,7 +80,7 @@ impl Database { pub fn records(&self) -> Result, Error> { let readable = self.connection.read().unwrap(); // @TODO let tx = readable.unchecked_transaction()?; - select(&tx, *self.profile_id) + select(&tx, self.profile_id) } } diff --git a/src/profile/search.rs b/src/profile/search.rs index ac3651ee..f60ef71a 100644 --- a/src/profile/search.rs +++ b/src/profile/search.rs @@ -17,7 +17,7 @@ impl Search { // Constructors /// Create new `Self` - pub fn build(connection: &Rc>, profile_id: &Rc) -> Result { + pub fn build(connection: &Rc>, profile_id: i64) -> Result { let database = Database::init(connection, profile_id)?; // Init fast search index let memory = Memory::init(); diff --git a/src/profile/search/database.rs b/src/profile/search/database.rs index fae11431..c15adc54 100644 --- a/src/profile/search/database.rs +++ b/src/profile/search/database.rs @@ -12,27 +12,27 @@ pub struct Row { pub struct Database { connection: Rc>, - profile_id: Rc, // multi-profile relationship + profile_id: i64, } impl Database { // Constructors /// Create new `Self` - pub fn init(connection: &Rc>, profile_id: &Rc) -> Result { + pub fn init(connection: &Rc>, profile_id: i64) -> Result { let mut writable = connection.write().unwrap(); // @TODO handle let tx = writable.transaction()?; - let records = select(&tx, **profile_id)?; + let records = select(&tx, profile_id)?; if records.is_empty() { - add_defaults(&tx, **profile_id)?; + add_defaults(&tx, profile_id)?; tx.commit()?; } Ok(Self { connection: connection.clone(), - profile_id: profile_id.clone(), + profile_id, }) } @@ -42,7 +42,7 @@ impl Database { pub fn records(&self) -> Result> { let readable = self.connection.read().unwrap(); // @TODO handle let tx = readable.unchecked_transaction()?; - select(&tx, *self.profile_id) + select(&tx, self.profile_id) } // Setters @@ -53,9 +53,9 @@ impl Database { let mut writable = self.connection.write().unwrap(); // @TODO handle let tx = writable.transaction()?; if is_default { - reset(&tx, *self.profile_id, !is_default)?; + reset(&tx, self.profile_id, !is_default)?; } - let id = insert(&tx, *self.profile_id, query, is_default)?; + let id = insert(&tx, self.profile_id, query, is_default)?; tx.commit()?; Ok(id) } @@ -69,11 +69,11 @@ impl Database { // Delete record by ID delete(&tx, id)?; - let records = select(&tx, *self.profile_id)?; + let records = select(&tx, self.profile_id)?; // Restore defaults if DB becomes empty if records.is_empty() { - add_defaults(&tx, *self.profile_id)?; + add_defaults(&tx, self.profile_id)?; } else { // At least one provider should be selected as default let mut has_default = false; @@ -85,7 +85,7 @@ impl Database { } // Select first if !has_default { - set_default(&tx, *self.profile_id, records[0].id, true)?; + set_default(&tx, self.profile_id, records[0].id, true)?; } } @@ -101,10 +101,10 @@ impl Database { let tx = writable.transaction()?; // Make sure only one default provider in set - reset(&tx, *self.profile_id, false)?; + reset(&tx, self.profile_id, false)?; // Delete record by ID - set_default(&tx, *self.profile_id, id, true)?; + set_default(&tx, self.profile_id, id, true)?; tx.commit()?; Ok(()) }