From 7f2c83a9388ba4c5f134f304c60c261aada86f3d Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Tue, 5 May 2026 11:35:58 -0400 Subject: sqlite.rs: Add Result types for legibility --- src/app/store/sqlite.rs | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/app/store/sqlite.rs b/src/app/store/sqlite.rs index 77c45c7..7375a40 100644 --- a/src/app/store/sqlite.rs +++ b/src/app/store/sqlite.rs @@ -7,10 +7,11 @@ use super::records::{EquityChangeEvent, EquitySymbol, FiduciaryAccount}; use super::{ContextError, Store}; use bigdecimal::ToPrimitive; use chrono::Local; -use miette::Result; use sqlite::{Connection, Row}; type DateTime = chrono::DateTime; +type Result = miette::Result; +type RowResult = miette::Result; pub struct SQLiteMigration { migration_id: usize, @@ -24,9 +25,9 @@ pub struct SQLiteStore { } /// Define data conversion from sqlite::Row to SQLiteMigration -impl TryFrom> for SQLiteMigration { +impl TryFrom for SQLiteMigration { type Error = ContextError; - fn try_from(value: Result) -> Result { + fn try_from(value: RowResult) -> Result { let result = value?; let id: i64 = result.try_read("migration_id")?; @@ -42,9 +43,9 @@ impl TryFrom> for SQLiteMigration { } /// Define data conversion from sqlite::Row to FiduciaryAccount -impl TryFrom> for FiduciaryAccount { +impl TryFrom for FiduciaryAccount { type Error = ContextError; - fn try_from(value: Result) -> Result { + fn try_from(value: RowResult) -> Result { let result = value?; let id: i64 = result.try_read("fiduciary_account_id")?; @@ -64,9 +65,9 @@ impl TryFrom> for FiduciaryAccount { } /// Define data conversion from sqlite::Row to EquitySymbol -impl TryFrom> for EquitySymbol { +impl TryFrom for EquitySymbol { type Error = ContextError; - fn try_from(value: Result) -> Result { + fn try_from(value: RowResult) -> Result { let result = value?; let id: i64 = result.try_read("equity_symbol_id")?; @@ -86,9 +87,9 @@ impl TryFrom> for EquitySymbol { } /// Define data conversion from sqlite::Row to EquityChangeEvent -impl TryFrom> for EquityChangeEvent { +impl TryFrom for EquityChangeEvent { type Error = ContextError; - fn try_from(value: Result) -> Result { + fn try_from(value: RowResult) -> Result { let result = value?; let event_id: i64 = result.try_read("equity_change_event_id")?; @@ -188,18 +189,19 @@ impl SQLiteStore { )"; /// Wrap connection in a Result to make it easier to unwrap later - fn get_connection(&self) -> Result<&Connection, sqlite::Error> { + fn get_connection(&self) -> Result<&Connection> { match &self.connection { Some(conn) => Ok(conn), None => Err(sqlite::Error { code: None, message: Some("Uninitialized".into()), - }), + } + .into()), } } /// Attempts to get the migration list from the database; if that fails, creates the migration table and returns an empty vec - fn exec_migration_list(&mut self) -> Result, ContextError> { + fn exec_migration_list(&mut self) -> Result> { let conn = self.get_connection()?; tracing::event!( @@ -225,11 +227,7 @@ impl SQLiteStore { } /// Executes a migration against the database, and inserts a migration record into the migration_control table. - fn run_and_insert_migration( - &mut self, - id: usize, - migration: &db::Migration, - ) -> Result<(), ContextError> { + fn run_and_insert_migration(&mut self, id: usize, migration: &db::Migration) -> Result<()> { let conn = self.get_connection()?; let timestamp = Local::now().to_string(); tracing::event!( @@ -259,7 +257,7 @@ impl SQLiteStore { } /// Gets list of available migrations, asserts whether they need to be run, and executes them against the sqlite db. - fn run_migrations(&mut self) -> Result<(), ContextError> { + fn run_migrations(&mut self) -> Result<()> { // - get list of sql files let migrations = db::get_migration_scripts()?; @@ -307,7 +305,7 @@ impl Default for SQLiteStore { impl Store for SQLiteStore { /// Initialize SQLite database connection and execute any pending migrations - fn init(&mut self) -> Result<(), ContextError> { + fn init(&mut self) -> Result<()> { let conn_path = self.location.clone(); let conn = sqlite::open(conn_path)?; self.connection = Option::Some(conn); @@ -318,7 +316,7 @@ impl Store for SQLiteStore { } /// Selects accounts from the fiduciary_account table - fn get_accounts(&self) -> Result, ContextError> { + fn get_accounts(&self) -> Result> { let conn = self.get_connection()?; tracing::event!( @@ -334,7 +332,7 @@ impl Store for SQLiteStore { } /// Inserts an account into the fiduciary_account table - fn add_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError> { + fn add_account(&mut self, account: &FiduciaryAccount) -> Result<()> { let conn = self.get_connection()?; tracing::event!( @@ -373,7 +371,7 @@ impl Store for SQLiteStore { } /// Selects symbols from the equity_symbol table - fn get_symbols(&self) -> Result, ContextError> { + fn get_symbols(&self) -> Result> { let conn = self.get_connection()?; tracing::event!( @@ -386,7 +384,7 @@ impl Store for SQLiteStore { } /// Inserts a symbol into the equity_symbol table - fn add_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError> { + fn add_symbol(&mut self, symbol: &EquitySymbol) -> Result<()> { let conn = self.get_connection()?; tracing::event!( @@ -422,7 +420,7 @@ impl Store for SQLiteStore { } /// Selects events from the equity_change_event table - fn get_events(&self) -> Result, ContextError> { + fn get_events(&self) -> Result> { let conn = self.get_connection()?; tracing::event!( @@ -438,7 +436,7 @@ impl Store for SQLiteStore { } /// Inserts an event into the equity_change_event table - fn add_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError> { + fn add_event(&mut self, event: &EquityChangeEvent) -> Result<()> { let conn = self.get_connection()?; tracing::event!( -- cgit v1.3