diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app/mod.rs | 22 | ||||
| -rw-r--r-- | src/app/store/mod.rs | 8 | ||||
| -rw-r--r-- | src/app/store/sqlite.rs | 106 |
3 files changed, 33 insertions, 103 deletions
diff --git a/src/app/mod.rs b/src/app/mod.rs index 83531f9..299f82e 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -32,6 +32,7 @@ pub struct App { context: AppContext, error: Option<ErrReport>, mode: AppMode, + counter: Option<u8>, } #[derive(Diagnostic, Debug, Error, PartialEq)] @@ -48,6 +49,7 @@ impl App { let store = self.context.unwrap_store(); store.init()?; + self.counter = Some(0_u8); self.mode = AppMode::Counter; Ok(self) @@ -68,8 +70,7 @@ impl App { } pub fn decrement_counter(&mut self) -> Result<()> { - let store = self.context.unwrap_store(); - let count = store.get_counter()?; + let count = self.counter.unwrap(); tracing::event!(tracing::Level::TRACE, count, "Decrementing counter."); if count == 0 { @@ -78,14 +79,13 @@ impl App { } .into()) } else { - store.set_counter(count - 1)?; + self.counter = Some(count - 1); Ok(()) } } pub fn increment_counter(&mut self) -> Result<()> { - let store = self.context.unwrap_store(); - let count = store.get_counter()?; + let count = self.counter.unwrap(); tracing::event!(tracing::Level::TRACE, count, "Incrementing counter."); if count == u8::MAX { @@ -94,17 +94,13 @@ impl App { } .into()) } else { - store.set_counter(count + 1)?; + self.counter = Some(count + 1); Ok(()) } } pub fn display_counter(&self) -> String { - self.context - .get_store() - .get_counter() - .map(|u| u.to_string()) - .unwrap_or("Error".into()) + self.counter.map_or("Error".into(), |c| c.to_string()) } pub fn set_error(&mut self, error: ErrReport) -> Result<()> { @@ -158,7 +154,7 @@ mod counter_tests { use store::ContextError; fn get_count(app: &App) -> Result<u8, ContextError> { - app.context.get_store().get_counter() + Ok(app.counter.unwrap()) } #[test] @@ -252,7 +248,7 @@ mod counter_tests { fn increment_counter_error() { let mut app = App::default(); assert!(app.init_context().is_ok()); - assert!(app.context.unwrap_store().set_counter(u8::MAX).is_ok()); + app.counter = Some(u8::MAX); let res = app.increment_counter(); assert!(res.is_err()); diff --git a/src/app/store/mod.rs b/src/app/store/mod.rs index 213c4a2..a840976 100644 --- a/src/app/store/mod.rs +++ b/src/app/store/mod.rs @@ -55,8 +55,12 @@ impl Default for AppContext { pub trait Store { fn init(&mut self) -> Result<(), ContextError>; - fn get_counter(&self) -> Result<u8, ContextError>; - fn set_counter(&mut self, new_count: u8) -> Result<u8, ContextError>; + fn get_accounts(&self) -> Result<Vec<FiduciaryAccount>, ContextError>; + fn get_symbols(&self) -> Result<Vec<EquitySymbol>, ContextError>; + fn get_events(&self) -> Result<Vec<EquityChangeEvent>, ContextError>; + fn add_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError>; + fn add_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError>; + fn add_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError>; } impl AppContext { diff --git a/src/app/store/sqlite.rs b/src/app/store/sqlite.rs index 0fac2cd..002d0cb 100644 --- a/src/app/store/sqlite.rs +++ b/src/app/store/sqlite.rs @@ -472,29 +472,28 @@ impl Store for SQLiteStore { Ok(()) } - /// Selects counter from `RATAT` table. Since sqlite doesn't scrutinize the - /// value and we have a more restricted datatype (i64 -> u8), we use - /// `try_from` which may produce an error result. - fn get_counter(&self) -> Result<u8, ContextError> { - self.counter.ok_or(ContextError::DataError(sqlite::Error { - code: None, - message: None, - })) + fn get_accounts(&self) -> Result<Vec<FiduciaryAccount>, ContextError> { + self.select_accounts() } - /// Updates row with counter value on `RATAT` table - fn set_counter(&mut self, new_count: u8) -> Result<u8, ContextError> { - if self.counter.is_none() { - Err(sqlite::Error { - code: None, - message: None, - })?; - } - self.counter = Some(new_count); + fn get_symbols(&self) -> Result<Vec<EquitySymbol>, ContextError> { + self.select_symbols() + } - tracing::event!(tracing::Level::DEBUG, new_count, "Updated counter."); + fn get_events(&self) -> Result<Vec<EquityChangeEvent>, ContextError> { + self.select_events() + } - Ok(new_count) + fn add_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError> { + self.insert_account(account) + } + + fn add_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError> { + self.insert_symbol(symbol) + } + + fn add_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError> { + self.insert_event(event) } } @@ -525,72 +524,3 @@ mod tests { assert!(context.connection.is_some()); } } - -#[cfg(test)] -mod counter_tests { - use super::*; - - #[test] - fn get_counter() { - let mut context = SQLiteStore::default(); - assert!(context.init().is_ok()); - - let res = context.get_counter(); - - assert!(res.is_ok()); - assert_eq!(res.unwrap(), 0); - } - - #[test] - fn get_counter_error() { - let context = SQLiteStore::default(); - // don't initialize - - let res = context.get_counter(); - - assert!(res.is_err()); - } - - #[test] - fn set_counter() { - let mut context = SQLiteStore::default(); - assert!(context.init().is_ok()); - - let expected = 240; - let res = context.set_counter(expected); - - assert!(res.is_ok()); - assert_eq!(res.unwrap(), expected); - } - - #[test] - fn set_counter_error() { - let mut context = SQLiteStore::default(); - // don't initialize - - let res = context.set_counter(1); - - assert!(res.is_err()); - } - - #[test] - fn set_and_get() { - let mut context = SQLiteStore::default(); - assert!(context.init().is_ok()); - - let count_0 = context.get_counter().unwrap(); - - assert_eq!(count_0, 0); - - let new_num = 244; - let res = context.set_counter(new_num); - - assert!(res.is_ok()); - assert_eq!(res.unwrap(), new_num); - - let count_1 = context.get_counter().unwrap(); - - assert_eq!(count_1, new_num); - assert_ne!(count_0, count_1); - } -} |
