Skip to content

Commit

Permalink
refactor Error to GenericError #82
Browse files Browse the repository at this point in the history
  • Loading branch information
frosklis committed Aug 25, 2021
1 parent 41cdabf commit cd58489
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -451,7 +451,7 @@ fn execute_command(opt: Opt, maybe_ledger: Option<Ledger>) -> Result<(), AppErro
}

/// A parser for date expressions
pub fn date_parser(date: &str) -> Result<NaiveDate, Error> {
pub fn date_parser(date: &str) -> Result<NaiveDate, GenericError> {
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();
Expand All @@ -475,7 +475,7 @@ pub fn date_parser(date: &str) -> Result<NaiveDate, Error> {
Ok((t1, _t2, _b)) => Ok(t1.date()),
Err(e) => {
eprintln!("{:?}", e);
Err(Error {
Err(GenericError {
message: vec![format!("Invalid date {}", date)
.as_str()
.bold()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/accounts.rs
Original file line number Diff line number Diff line change
@@ -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<Ledger>) -> Result<(), Error> {
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), GenericError> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +18,7 @@ pub fn execute(
maybe_ledger: Option<Ledger>,
flat: bool,
show_total: bool,
) -> Result<(), Error> {
) -> Result<(), GenericError> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/commodities.rs
Original file line number Diff line number Diff line change
@@ -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<Ledger>) -> Result<(), Error> {
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), GenericError> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/payees.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::models::Ledger;
use crate::Error;
use crate::GenericError;
use crate::{
models::{HasName, Payee},
CommonOpts,
};
use std::convert::TryFrom;
use std::ops::Deref;

pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), GenericError> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ledger>) -> Result<(), Error> {
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), GenericError> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/register.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,7 +10,7 @@ use std::rc::Rc;
use terminal_size::{terminal_size, Width};

/// Register report
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), GenericError> {
// Get options from options
let _no_balance_check: bool = options.no_balance_check;
// Now work
Expand Down
4 changes: 2 additions & 2 deletions src/commands/roi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)?,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/statistics.rs
Original file line number Diff line number Diff line change
@@ -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<Ledger>) -> Result<(), Error> {
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), GenericError> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ pub enum LedgerError {
}

#[derive(Debug)]
pub struct Error {
pub struct GenericError {
pub message: Vec<ColoredString>,
}

impl Display for Error {
impl Display for GenericError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", ColoredStrings(&self.message))
}
}

impl From<LedgerError> for Error {
impl From<LedgerError> for GenericError {
fn from(error: LedgerError) -> Self {
eprintln!("{:?}", error);
// TODO prettier error conversion
Error { message: vec![] }
GenericError { message: vec![] }
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +12,7 @@ pub fn filter(
transaction: &Transaction<Posting>,
posting: &Posting,
commodities: &List<Currency>,
) -> Result<bool, Error> {
) -> Result<bool, GenericError> {
// Get what's needed
let real = options.real;

Expand Down Expand Up @@ -47,11 +47,11 @@ pub fn filter_expression(
transaction: &Transaction<Posting>,
commodities: &List<Currency>,
regexes: &mut HashMap<String, Regex>,
) -> Result<bool, Error> {
) -> Result<bool, GenericError> {
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(),
Expand Down
2 changes: 1 addition & 1 deletion src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 4 additions & 4 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,7 +47,7 @@ pub struct Ledger {
}

impl TryFrom<&CommonOpts> for Ledger {
type Error = Error;
type Error = GenericError;
fn try_from(options: &CommonOpts) -> Result<Self, Self::Error> {
// Get the options
let path: PathBuf = options.input_file.clone();
Expand Down Expand Up @@ -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<Ledger, Error> {
pub fn to_ledger(mut self, options: &CommonOpts) -> Result<Ledger, GenericError> {
let mut commodity_strs = HashSet::<String>::new();
let mut account_strs = HashSet::<String>::new();
let mut payee_strs = HashSet::<String>::new();
Expand Down Expand Up @@ -379,7 +379,7 @@ impl ParsedLedger {
fn _transaction_to_ledger(
&self,
parsed: &Transaction<tokenizers::transaction::RawPosting>,
) -> Result<TransactionTransformer, Error> {
) -> Result<TransactionTransformer, GenericError> {
let mut automated_transactions = vec![];
let mut prices = vec![];
let mut transactions = vec![];
Expand Down

0 comments on commit cd58489

Please sign in to comment.