49 lines
1.5 KiB
Rust
49 lines
1.5 KiB
Rust
use std::fmt::Debug;
|
|
|
|
use crate::query::{AccountField, PostingField, TransactionField};
|
|
|
|
pub trait ParseField: Debug + Sized + Clone {
|
|
fn parse(input: &str) -> Option<Self>;
|
|
}
|
|
|
|
impl ParseField for TransactionField {
|
|
fn parse(input: &str) -> Option<Self> {
|
|
match input.to_lowercase().as_str() {
|
|
"date" => Some(TransactionField::Date),
|
|
"flag" => Some(TransactionField::Flag),
|
|
"payee" => Some(TransactionField::Payee),
|
|
"narration" => Some(TransactionField::Narration),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ParseField for AccountField {
|
|
fn parse(input: &str) -> Option<Self> {
|
|
match input.to_lowercase().as_str() {
|
|
"name" => Some(AccountField::Name),
|
|
"open" => Some(AccountField::OpenDate),
|
|
"close" => Some(AccountField::CloseDate),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ParseField for PostingField {
|
|
fn parse(input: &str) -> Option<Self> {
|
|
match input
|
|
.to_lowercase()
|
|
.split('.')
|
|
.collect::<Vec<_>>()
|
|
.as_slice()
|
|
{
|
|
["amount"] => Some(PostingField::Amount),
|
|
["cost"] => Some(PostingField::Cost),
|
|
["price"] => Some(PostingField::Price),
|
|
["transaction", t] => TransactionField::parse(t).map(|v| PostingField::Transaction(v)),
|
|
["account", t] => AccountField::parse(t).map(|v| PostingField::Account(v)),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|