diff --git a/src/app.rs b/src/app.rs index e23e427..e2f6f83 100644 --- a/src/app.rs +++ b/src/app.rs @@ -14,7 +14,7 @@ use regex::Regex; use crate::commands::roi::Frequency; use crate::commands::{accounts, balance, commodities, payees, prices, register, roi, statistics}; use crate::models::Ledger; -use crate::Error; +use crate::GenericError; use chrono::NaiveDate; use colored::Colorize; @@ -451,7 +451,7 @@ fn execute_command(opt: Opt, maybe_ledger: Option) -> Result<(), AppErro } /// A parser for date expressions -pub fn date_parser(date: &str) -> Result { +pub fn date_parser(date: &str) -> Result { lazy_static! { static ref RE_MONTH: Regex = Regex::new(r"(\d{4})[/-](\d\d?)$").unwrap(); static ref RE_DATE: Regex = Regex::new(r"(\d{4})[/-](\d\d?)[/-](\d\d?)$").unwrap(); @@ -475,7 +475,7 @@ pub fn date_parser(date: &str) -> Result { Ok((t1, _t2, _b)) => Ok(t1.date()), Err(e) => { eprintln!("{:?}", e); - Err(Error { + Err(GenericError { message: vec![format!("Invalid date {}", date) .as_str() .bold() diff --git a/src/commands/accounts.rs b/src/commands/accounts.rs index 013054e..a03e9aa 100644 --- a/src/commands/accounts.rs +++ b/src/commands/accounts.rs @@ -1,10 +1,10 @@ use std::convert::TryFrom; use crate::models::{Account, HasName, Ledger}; -use crate::{error::Error, CommonOpts}; +use crate::{error::GenericError, CommonOpts}; use std::ops::Deref; -pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), Error> { +pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/commands/balance.rs b/src/commands/balance.rs index 1a56fc6..349d58d 100644 --- a/src/commands/balance.rs +++ b/src/commands/balance.rs @@ -5,7 +5,7 @@ use colored::Colorize; use crate::models::{conversion, Account, Balance, Currency, HasName, Ledger, Money}; use crate::parser::value_expr::build_root_node_from_expression; -use crate::Error; +use crate::GenericError; use crate::{filter, CommonOpts}; use chrono::Utc; use num::rational::BigRational; @@ -18,7 +18,7 @@ pub fn execute( maybe_ledger: Option, flat: bool, show_total: bool, -) -> Result<(), Error> { +) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/commands/commodities.rs b/src/commands/commodities.rs index b379b6a..abc3f0b 100644 --- a/src/commands/commodities.rs +++ b/src/commands/commodities.rs @@ -1,14 +1,14 @@ use std::convert::TryFrom; use crate::models::Ledger; -use crate::Error; +use crate::GenericError; use crate::{ models::{Currency, HasName}, CommonOpts, }; use std::ops::Deref; -pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), Error> { +pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/commands/payees.rs b/src/commands/payees.rs index 84433d0..76ea53b 100644 --- a/src/commands/payees.rs +++ b/src/commands/payees.rs @@ -1,5 +1,5 @@ use crate::models::Ledger; -use crate::Error; +use crate::GenericError; use crate::{ models::{HasName, Payee}, CommonOpts, @@ -7,7 +7,7 @@ use crate::{ use std::convert::TryFrom; use std::ops::Deref; -pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), Error> { +pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/commands/prices.rs b/src/commands/prices.rs index 135bdbf..5880beb 100644 --- a/src/commands/prices.rs +++ b/src/commands/prices.rs @@ -3,9 +3,9 @@ use std::ops::Deref; use crate::models::Ledger; use crate::CommonOpts; -use crate::Error; +use crate::GenericError; -pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), Error> { +pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/commands/register.rs b/src/commands/register.rs index 779c041..802d5af 100644 --- a/src/commands/register.rs +++ b/src/commands/register.rs @@ -1,7 +1,7 @@ use crate::models::{conversion, HasName, Ledger, Posting, PostingType}; use crate::models::{Balance, Money}; use crate::parser::value_expr::build_root_node_from_expression; -use crate::Error; +use crate::GenericError; use crate::{filter, CommonOpts}; use colored::Colorize; use std::collections::HashMap; @@ -10,7 +10,7 @@ use std::rc::Rc; use terminal_size::{terminal_size, Width}; /// Register report -pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), Error> { +pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), GenericError> { // Get options from options let _no_balance_check: bool = options.no_balance_check; // Now work diff --git a/src/commands/roi.rs b/src/commands/roi.rs index 80e26af..9a2a343 100644 --- a/src/commands/roi.rs +++ b/src/commands/roi.rs @@ -8,7 +8,7 @@ use crate::app::PeriodGroup; use crate::commands::balance::convert_balance; use crate::models::{conversion, Balance, Ledger, Money}; use crate::parser::value_expr::build_root_node_from_expression; -use crate::Error; +use crate::GenericError; use crate::{filter, CommonOpts}; use chrono::{Datelike, Duration, NaiveDate}; use num::{BigInt, BigRational, Zero}; @@ -24,7 +24,7 @@ pub fn execute( frequency: Frequency, calendar: bool, summary: bool, -) -> Result<(), Error> { +) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/commands/statistics.rs b/src/commands/statistics.rs index b62393a..8760360 100644 --- a/src/commands/statistics.rs +++ b/src/commands/statistics.rs @@ -1,12 +1,12 @@ use std::convert::TryFrom; use crate::models::Ledger; -use crate::{error::Error, CommonOpts}; +use crate::{error::GenericError, CommonOpts}; /// Statistics command /// /// Prints summary statistics from the ledger -pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), Error> { +pub fn execute(options: &CommonOpts, maybe_ledger: Option) -> Result<(), GenericError> { let ledger = match maybe_ledger { Some(ledger) => ledger, None => Ledger::try_from(options)?, diff --git a/src/error.rs b/src/error.rs index b388683..fe4d972 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,21 +12,21 @@ pub enum LedgerError { } #[derive(Debug)] -pub struct Error { +pub struct GenericError { pub message: Vec, } -impl Display for Error { +impl Display for GenericError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{}", ColoredStrings(&self.message)) } } -impl From for Error { +impl From for GenericError { fn from(error: LedgerError) -> Self { eprintln!("{:?}", error); // TODO prettier error conversion - Error { message: vec![] } + GenericError { message: vec![] } } } diff --git a/src/filter.rs b/src/filter.rs index ad24922..f5847a8 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -1,6 +1,6 @@ use crate::models::{Currency, Posting, PostingType, Transaction}; use crate::parser::value_expr::{eval, EvalResult, Node}; -use crate::{CommonOpts, Error, List}; +use crate::{CommonOpts, GenericError, List}; use colored::Colorize; use regex::Regex; use std::collections::HashMap; @@ -12,7 +12,7 @@ pub fn filter( transaction: &Transaction, posting: &Posting, commodities: &List, -) -> Result { +) -> Result { // Get what's needed let real = options.real; @@ -47,11 +47,11 @@ pub fn filter_expression( transaction: &Transaction, commodities: &List, regexes: &mut HashMap, -) -> Result { +) -> Result { let result = eval(predicate, posting, transaction, commodities, regexes); match result { EvalResult::Boolean(b) => Ok(b), - _ => Err(Error { + _ => Err(GenericError { message: vec![ format!("{:?}", predicate).red().bold(), "should return a boolean".normal(), diff --git a/src/mod.rs b/src/mod.rs index 53b5831..0619298 100644 --- a/src/mod.rs +++ b/src/mod.rs @@ -58,7 +58,7 @@ pub mod models; pub mod parser; pub use app::{run_app, CommonOpts}; -pub(crate) use error::{Error, LedgerError}; +pub(crate) use error::{GenericError, LedgerError}; pub use list::List; #[macro_use] extern crate prettytable; diff --git a/src/models.rs b/src/models.rs index cf56322..478070e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -22,7 +22,7 @@ use crate::parser::{tokenizers, value_expr}; use crate::{error::LedgerError, parser::value_expr::build_root_node_from_expression}; use crate::{filter::filter_expression, CommonOpts}; use crate::{models::transaction::Cost, parser::Tokenizer}; -use crate::{Error, List}; +use crate::{GenericError, List}; use num::BigInt; use std::cell::RefCell; use std::rc::Rc; @@ -47,7 +47,7 @@ pub struct Ledger { } impl TryFrom<&CommonOpts> for Ledger { - type Error = Error; + type Error = GenericError; fn try_from(options: &CommonOpts) -> Result { // Get the options let path: PathBuf = options.input_file.clone(); @@ -81,7 +81,7 @@ impl ParsedLedger { /// 5. Checks whether transactions are balanced again /// /// There may be room for optimization here - pub fn to_ledger(mut self, options: &CommonOpts) -> Result { + pub fn to_ledger(mut self, options: &CommonOpts) -> Result { let mut commodity_strs = HashSet::::new(); let mut account_strs = HashSet::::new(); let mut payee_strs = HashSet::::new(); @@ -379,7 +379,7 @@ impl ParsedLedger { fn _transaction_to_ledger( &self, parsed: &Transaction, - ) -> Result { + ) -> Result { let mut automated_transactions = vec![]; let mut prices = vec![]; let mut transactions = vec![];