Skip to content

Commit bec0d87

Browse files
authored
Merge pull request #68 from frosklis/frosklis/issue22
Infer currency format from the journal file
2 parents 49602d5 + e462ffe commit bec0d87

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Changelog file for dinero-rs project, a command line application for managing finances.
33

44
## [0.21.0] - xxx
5+
### Added
6+
- Infer currency format from the journal file
57
### Changed
68
- Continuos integration pipeline is now better. No more problems like what happened between releases 0.18 and 0.20.
79
### Fixed

src/list.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ impl<'a, T: Eq + Hash + HasName + Clone + FromDirective + HasAliases + Debug> Li
5151
}
5252
}
5353
}
54+
/// Removes an ```element``` in the list
55+
pub fn remove(&mut self, element: &T) {
56+
let found = self.list.get(&element.get_name().to_lowercase());
57+
match found {
58+
Some(x) => {
59+
for alias in x.get_aliases() {
60+
self.aliases.remove(&alias.to_lowercase());
61+
}
62+
self.list.remove(&element.get_name().to_lowercase());
63+
}
64+
None => {
65+
for alias in element.get_aliases() {
66+
let value = self.aliases.remove(&alias.to_lowercase());
67+
if let Some(x) = value {
68+
self.list.remove(&x);
69+
}
70+
self.list.remove(&alias.to_lowercase());
71+
}
72+
}
73+
}
74+
}
5475
/// Add an alias
5576
pub fn add_alias(&mut self, alias: String, for_element: &'a T) {
5677
let element = self.aliases.get(&alias.to_lowercase());

src/models/currency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ impl Currency {
199199
rule = parsed.next().unwrap();
200200
}
201201
integer_format = Some(rule);
202-
self.negative_amount_display = NegativeAmountDisplay::AfterNumber;
202+
// todo think
203+
self.negative_amount_display = NegativeAmountDisplay::BeforeNumberBehindCurrency;
203204
}
204205
other => {
205206
panic!("Other: {:?}", other);

src/models/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ impl ParsedLedger {
6767
if let Some(payee) = p.payee.clone() {
6868
payee_strs.insert(payee);
6969
}
70-
// Currencies
71-
if let Some(c) = &p.money_currency {
72-
commodity_strs.insert(c.clone());
73-
}
74-
if let Some(c) = &p.cost_currency {
75-
commodity_strs.insert(c.clone());
76-
}
77-
if let Some(c) = &p.balance_currency {
78-
commodity_strs.insert(c.clone());
79-
}
8070
}
8171
}
8272
for price in self.prices.iter() {

src/parser/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ impl<'a> Tokenizer<'a> {
132132
ledger.tags.push(self.parse_tag(inner));
133133
}
134134
Rule::commodity => {
135-
ledger.commodities.insert(self.parse_commodity(inner));
135+
let commodity = self.parse_commodity(inner);
136+
ledger.commodities.remove(&commodity);
137+
ledger.commodities.insert(commodity);
136138
}
137139
Rule::account_dir => {
138140
ledger.accounts.insert(self.parse_account(inner));
@@ -144,7 +146,26 @@ impl<'a> Tokenizer<'a> {
144146
}
145147
}
146148
Rule::transaction | Rule::automated_transaction => {
147-
ledger.transactions.push(self.parse_transaction(element));
149+
let transaction = self.parse_transaction(element);
150+
for posting in transaction.postings.borrow().iter() {
151+
let currencies = &[
152+
(&posting.money_currency, &posting.money_format),
153+
(&posting.cost_currency, &posting.cost_format),
154+
(&posting.balance_currency, &posting.balance_format),
155+
];
156+
for (currency, format) in currencies {
157+
if let Some(c) = currency {
158+
let found = ledger.commodities.get(c);
159+
if found.is_err() {
160+
let mut commodity = Currency::from(c.as_str());
161+
commodity
162+
.set_format(format.as_ref().unwrap().to_owned());
163+
ledger.commodities.insert(commodity);
164+
}
165+
}
166+
}
167+
}
168+
ledger.transactions.push(transaction);
148169
}
149170
_x => {
150171
// eprintln!("{:?}", x);

src/parser/tokenizers/transaction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ pub struct RawPosting {
8585
pub date: Option<NaiveDate>,
8686
pub money_amount: Option<BigRational>,
8787
pub money_currency: Option<String>,
88+
pub money_format: Option<String>,
8889
pub cost_amount: Option<BigRational>,
8990
pub cost_currency: Option<String>,
91+
pub cost_format: Option<String>,
9092
pub cost_type: Option<PriceType>,
9193
pub balance_amount: Option<BigRational>,
9294
pub balance_currency: Option<String>,
95+
pub balance_format: Option<String>,
9396
pub comments: Vec<Comment>,
9497
pub amount_expr: Option<String>,
9598
pub kind: PostingType,
@@ -112,6 +115,9 @@ impl RawPosting {
112115
amount_expr: None,
113116
kind: PostingType::Real,
114117
payee: None,
118+
money_format: None,
119+
cost_format: None,
120+
balance_format: None,
115121
}
116122
}
117123
}
@@ -143,6 +149,7 @@ fn parse_posting(
143149
Some(PriceType::PerUnit)
144150
};
145151
let mut money = part.into_inner().next().unwrap().into_inner();
152+
let money_format = money.as_str().to_string();
146153
let amount: BigRational;
147154
let mut currency = None;
148155
match money.next() {
@@ -164,15 +171,18 @@ fn parse_posting(
164171
Rule::amount => {
165172
posting.money_amount = Some(amount);
166173
posting.money_currency = currency;
174+
posting.money_format = Some(money_format);
167175
}
168176
Rule::cost => {
169177
posting.cost_amount = Some(amount);
170178
posting.cost_currency = currency;
171179
posting.cost_type = cost_type;
180+
posting.cost_format = Some(money_format);
172181
}
173182
Rule::balance => {
174183
posting.balance_amount = Some(amount);
175184
posting.balance_currency = currency;
185+
posting.balance_format = Some(money_format);
176186
}
177187
x => panic!("Expected amount, cost or balance {:?}", x),
178188
}

0 commit comments

Comments
 (0)