diff options
Diffstat (limited to 'src/app/store')
| -rw-r--r-- | src/app/store/sqlite.rs | 48 |
1 files changed, 23 insertions, 25 deletions
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<chrono::Utc>; +type Result<T> = miette::Result<T, ContextError>; +type RowResult = miette::Result<Row, sqlite::Error>; pub struct SQLiteMigration { migration_id: usize, @@ -24,9 +25,9 @@ pub struct SQLiteStore { } /// Define data conversion from sqlite::Row to SQLiteMigration -impl TryFrom<Result<Row, sqlite::Error>> for SQLiteMigration { +impl TryFrom<RowResult> for SQLiteMigration { type Error = ContextError; - fn try_from(value: Result<Row, sqlite::Error>) -> Result<Self, Self::Error> { + fn try_from(value: RowResult) -> Result<Self> { let result = value?; let id: i64 = result.try_read("migration_id")?; @@ -42,9 +43,9 @@ impl TryFrom<Result<Row, sqlite::Error>> for SQLiteMigration { } /// Define data conversion from sqlite::Row to FiduciaryAccount -impl TryFrom<Result<Row, sqlite::Error>> for FiduciaryAccount { +impl TryFrom<RowResult> for FiduciaryAccount { type Error = ContextError; - fn try_from(value: Result<Row, sqlite::Error>) -> Result<Self, Self::Error> { + fn try_from(value: RowResult) -> Result<Self> { let result = value?; let id: i64 = result.try_read("fiduciary_account_id")?; @@ -64,9 +65,9 @@ impl TryFrom<Result<Row, sqlite::Error>> for FiduciaryAccount { } /// Define data conversion from sqlite::Row to EquitySymbol -impl TryFrom<Result<Row, sqlite::Error>> for EquitySymbol { +impl TryFrom<RowResult> for EquitySymbol { type Error = ContextError; - fn try_from(value: Result<Row, sqlite::Error>) -> Result<Self, Self::Error> { + fn try_from(value: RowResult) -> Result<Self> { let result = value?; let id: i64 = result.try_read("equity_symbol_id")?; @@ -86,9 +87,9 @@ impl TryFrom<Result<Row, sqlite::Error>> for EquitySymbol { } /// Define data conversion from sqlite::Row to EquityChangeEvent -impl TryFrom<Result<Row, sqlite::Error>> for EquityChangeEvent { +impl TryFrom<RowResult> for EquityChangeEvent { type Error = ContextError; - fn try_from(value: Result<Row, sqlite::Error>) -> Result<Self, Self::Error> { + fn try_from(value: RowResult) -> Result<Self> { 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<Vec<SQLiteMigration>, ContextError> { + fn exec_migration_list(&mut self) -> Result<Vec<SQLiteMigration>> { 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<Vec<FiduciaryAccount>, ContextError> { + fn get_accounts(&self) -> Result<Vec<FiduciaryAccount>> { 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<Vec<EquitySymbol>, ContextError> { + fn get_symbols(&self) -> Result<Vec<EquitySymbol>> { 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<Vec<EquityChangeEvent>, ContextError> { + fn get_events(&self) -> Result<Vec<EquityChangeEvent>> { 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!( |
