Change object model and add crate Error type
This commit is contained in:
+7
-9
@@ -1,20 +1,18 @@
|
||||
use crate::Error;
|
||||
|
||||
pub struct Transaction {
|
||||
pub date: String,
|
||||
pub payee: String,
|
||||
pub amount: i64,
|
||||
}
|
||||
|
||||
fn parse_amount_str(amount: &str) -> Result<i64, String> {
|
||||
fn parse_amount_str(amount: &str) -> Result<i64, Error> {
|
||||
if amount.is_empty() {
|
||||
return Err("empty amount".to_string());
|
||||
return Err(Error::AmountEmpty);
|
||||
}
|
||||
let (whole, frac) = amount.split_once('.').unwrap_or((amount, "00"));
|
||||
let whole = whole
|
||||
.parse::<i64>()
|
||||
.map_err(|e| format!("parse error on whole {}: {}", whole, e))?;
|
||||
let frac = frac
|
||||
.parse::<i64>()
|
||||
.map_err(|e| format!("parse error on fraction {}: {}", frac, e))?;
|
||||
let whole = whole.parse::<i64>()?;
|
||||
let frac = frac.parse::<i64>()?;
|
||||
|
||||
let mut amount: i64 = 0;
|
||||
amount += whole * 100 * 10;
|
||||
@@ -24,7 +22,7 @@ fn parse_amount_str(amount: &str) -> Result<i64, String> {
|
||||
|
||||
impl Transaction {
|
||||
/// Construct a Transaction from [date, payee, amount] strings
|
||||
pub fn from_fields(date: &str, payee: &str, amount: &str) -> Result<Transaction, String> {
|
||||
pub fn from_fields(date: &str, payee: &str, amount: &str) -> Result<Transaction, Error> {
|
||||
Ok(Transaction {
|
||||
date: date.to_owned(),
|
||||
payee: payee.to_owned(),
|
||||
|
||||
Reference in New Issue
Block a user