remove extra rc wrapper

This commit is contained in:
yggverse 2025-03-13 17:25:07 +02:00
parent b71b2cd7fa
commit 4002c94a4a
9 changed files with 53 additions and 56 deletions

View file

@ -22,11 +22,11 @@ const BRANCH: &str = "master";
const DB_NAME: &str = "database.sqlite3"; const DB_NAME: &str = "database.sqlite3";
pub struct Profile { pub struct Profile {
pub bookmark: Rc<Bookmark>, pub bookmark: Bookmark,
pub database: Rc<Database>, pub database: Database,
pub history: Rc<History>, pub history: History,
pub identity: Rc<Identity>, pub identity: Identity,
pub search: Rc<Search>, pub search: Search,
pub config_path: PathBuf, pub config_path: PathBuf,
} }
@ -69,19 +69,19 @@ impl Profile {
} // unlock database } // unlock database
// Init model // Init model
let database = Rc::new(Database::build(&connection)); let database = Database::build(&connection);
// Get active profile or create new one // 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, Some(profile) => profile.id,
None => database.add(true, DateTime::now_local()?, None)?, None => database.add(true, DateTime::now_local()?, None)?,
}); };
// Init components // Init components
let bookmark = Rc::new(Bookmark::build(&connection, &profile_id)?); let bookmark = Bookmark::build(&connection, profile_id)?;
let history = Rc::new(History::build(&connection, &profile_id)?); let history = History::build(&connection, profile_id)?;
let search = Rc::new(Search::build(&connection, &profile_id)?); let search = Search::build(&connection, profile_id)?;
let identity = Rc::new(Identity::build(&connection, &profile_id)?); let identity = Identity::build(&connection, profile_id)?;
// Result // Result
Ok(Self { Ok(Self {

View file

@ -19,7 +19,7 @@ impl Bookmark {
// Constructors // Constructors
/// 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: i64) -> Result<Self> {
// Init children components // Init children components
let database = Database::new(connection, profile_id); let database = Database::new(connection, profile_id);
let memory = RefCell::new(Memory::new()); let memory = RefCell::new(Memory::new());

View file

@ -6,17 +6,17 @@ use std::{rc::Rc, sync::RwLock};
pub struct Database { pub struct Database {
connection: Rc<RwLock<Connection>>, connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship profile_id: i64,
} }
impl Database { impl Database {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self { pub fn new(connection: &Rc<RwLock<Connection>>, profile_id: i64) -> Self {
Self { Self {
connection: connection.clone(), 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<Vec<Item>> { pub fn records(&self, request: Option<&str>, title: Option<&str>) -> Result<Vec<Item>> {
let readable = self.connection.read().unwrap(); // @TODO let readable = self.connection.read().unwrap(); // @TODO
let tx = readable.unchecked_transaction()?; let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id, request, title) select(&tx, self.profile_id, request, title)
} }
// Setters // Setters
@ -36,7 +36,7 @@ impl Database {
pub fn add(&self, time: DateTime, request: &str, title: Option<&str>) -> Result<i64> { pub fn add(&self, time: DateTime, request: &str, title: Option<&str>) -> Result<i64> {
let mut writable = self.connection.write().unwrap(); // @TODO let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?; 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()?; tx.commit()?;
Ok(id) Ok(id)
} }

View file

@ -19,7 +19,7 @@ impl History {
// Constructors // Constructors
/// 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: i64) -> Result<Self> {
// Init children components // Init children components
let database = Database::build(connection, profile_id); let database = Database::build(connection, profile_id);
let memory = RefCell::new(Memory::new()); let memory = RefCell::new(Memory::new());

View file

@ -6,17 +6,17 @@ use std::{rc::Rc, sync::RwLock};
pub struct Database { pub struct Database {
connection: Rc<RwLock<Connection>>, connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship profile_id: i64,
} }
impl Database { impl Database {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self { pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: i64) -> Self {
Self { Self {
connection: connection.clone(), 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<Vec<Item>> { pub fn records(&self, request: Option<&str>, title: Option<&str>) -> Result<Vec<Item>> {
let readable = self.connection.read().unwrap(); // @TODO let readable = self.connection.read().unwrap(); // @TODO
let tx = readable.unchecked_transaction()?; let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id, request, title) select(&tx, self.profile_id, request, title)
} }
// Actions // Actions
@ -36,7 +36,7 @@ impl Database {
pub fn add(&self, item: &Item) -> Result<i64> { pub fn add(&self, item: &Item) -> Result<i64> {
let mut writable = self.connection.write().unwrap(); // @TODO let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?; let tx = writable.transaction()?;
let id = insert(&tx, *self.profile_id, item)?; let id = insert(&tx, self.profile_id, item)?;
tx.commit()?; tx.commit()?;
Ok(id) Ok(id)
} }
@ -44,7 +44,7 @@ impl Database {
pub fn update(&self, item: &Item) -> Result<usize> { pub fn update(&self, item: &Item) -> Result<usize> {
let mut writable = self.connection.write().unwrap(); // @TODO let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?; let tx = writable.transaction()?;
let affected = update(&tx, *self.profile_id, item)?; let affected = update(&tx, self.profile_id, item)?;
tx.commit()?; tx.commit()?;
Ok(affected) Ok(affected)
} }

View file

@ -17,23 +17,20 @@ use std::{rc::Rc, sync::RwLock};
/// ///
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates /// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
pub struct Identity { pub struct Identity {
pub auth: Rc<Auth>, pub auth: Auth,
pub database: Rc<Database>, pub database: Database,
pub memory: Rc<Memory>, pub memory: Memory,
} } // @TODO remove pub access
impl Identity { impl Identity {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn build( pub fn build(connection: &Rc<RwLock<Connection>>, profile_identity_id: i64) -> Result<Self> {
connection: &Rc<RwLock<Connection>>,
profile_identity_id: &Rc<i64>,
) -> Result<Self> {
// Init components // Init components
let auth = Rc::new(Auth::build(connection)?); let auth = Auth::build(connection)?;
let database = Rc::new(Database::build(connection, profile_identity_id)); let database = Database::build(connection, profile_identity_id);
let memory = Rc::new(Memory::new()); let memory = Memory::new();
// Init `Self` // Init `Self`
let this = Self { let this = Self {

View file

@ -10,17 +10,17 @@ pub struct Table {
/// Storage for Gemini auth certificates /// Storage for Gemini auth certificates
pub struct Database { pub struct Database {
connection: Rc<RwLock<Connection>>, connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship profile_id: i64,
} }
impl Database { impl Database {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self { pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: i64) -> Self {
Self { Self {
connection: connection.clone(), connection: connection.clone(),
profile_id: profile_id.clone(), profile_id,
} }
} }
@ -33,7 +33,7 @@ impl Database {
let tx = writable.transaction()?; let tx = writable.transaction()?;
// Create new record // Create new record
insert(&tx, *self.profile_id, pem)?; insert(&tx, self.profile_id, pem)?;
// Hold insert ID for result // Hold insert ID for result
let id = last_insert_id(&tx); let id = last_insert_id(&tx);
@ -65,7 +65,7 @@ impl Database {
pub fn record(&self, id: i64) -> Result<Option<Table>, Error> { pub fn record(&self, id: i64) -> Result<Option<Table>, Error> {
let readable = self.connection.read().unwrap(); let readable = self.connection.read().unwrap();
let tx = readable.unchecked_transaction()?; 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 { for record in records {
if record.id == id { if record.id == id {
@ -80,7 +80,7 @@ impl Database {
pub fn records(&self) -> Result<Vec<Table>, Error> { pub fn records(&self) -> Result<Vec<Table>, Error> {
let readable = self.connection.read().unwrap(); // @TODO let readable = self.connection.read().unwrap(); // @TODO
let tx = readable.unchecked_transaction()?; let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id) select(&tx, self.profile_id)
} }
} }

View file

@ -17,7 +17,7 @@ impl Search {
// Constructors // Constructors
/// 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: i64) -> Result<Self> {
let database = Database::init(connection, profile_id)?; let database = Database::init(connection, profile_id)?;
// Init fast search index // Init fast search index
let memory = Memory::init(); let memory = Memory::init();

View file

@ -12,27 +12,27 @@ pub struct Row {
pub struct Database { pub struct Database {
connection: Rc<RwLock<Connection>>, connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship profile_id: i64,
} }
impl Database { impl Database {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self> { pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: i64) -> Result<Self> {
let mut writable = connection.write().unwrap(); // @TODO handle let mut writable = connection.write().unwrap(); // @TODO handle
let tx = writable.transaction()?; let tx = writable.transaction()?;
let records = select(&tx, **profile_id)?; let records = select(&tx, profile_id)?;
if records.is_empty() { if records.is_empty() {
add_defaults(&tx, **profile_id)?; add_defaults(&tx, profile_id)?;
tx.commit()?; tx.commit()?;
} }
Ok(Self { Ok(Self {
connection: connection.clone(), connection: connection.clone(),
profile_id: profile_id.clone(), profile_id,
}) })
} }
@ -42,7 +42,7 @@ impl Database {
pub fn records(&self) -> Result<Vec<Row>> { pub fn records(&self) -> Result<Vec<Row>> {
let readable = self.connection.read().unwrap(); // @TODO handle let readable = self.connection.read().unwrap(); // @TODO handle
let tx = readable.unchecked_transaction()?; let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id) select(&tx, self.profile_id)
} }
// Setters // Setters
@ -53,9 +53,9 @@ impl Database {
let mut writable = self.connection.write().unwrap(); // @TODO handle let mut writable = self.connection.write().unwrap(); // @TODO handle
let tx = writable.transaction()?; let tx = writable.transaction()?;
if is_default { 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()?; tx.commit()?;
Ok(id) Ok(id)
} }
@ -69,11 +69,11 @@ impl Database {
// Delete record by ID // Delete record by ID
delete(&tx, id)?; delete(&tx, id)?;
let records = select(&tx, *self.profile_id)?; let records = select(&tx, self.profile_id)?;
// Restore defaults if DB becomes empty // Restore defaults if DB becomes empty
if records.is_empty() { if records.is_empty() {
add_defaults(&tx, *self.profile_id)?; add_defaults(&tx, self.profile_id)?;
} else { } else {
// At least one provider should be selected as default // At least one provider should be selected as default
let mut has_default = false; let mut has_default = false;
@ -85,7 +85,7 @@ impl Database {
} }
// Select first // Select first
if !has_default { 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()?; let tx = writable.transaction()?;
// Make sure only one default provider in set // Make sure only one default provider in set
reset(&tx, *self.profile_id, false)?; reset(&tx, self.profile_id, false)?;
// Delete record by ID // Delete record by ID
set_default(&tx, *self.profile_id, id, true)?; set_default(&tx, self.profile_id, id, true)?;
tx.commit()?; tx.commit()?;
Ok(()) Ok(())
} }