use crate::indexer::Index;
use crate::parser::located::Located;
use crate::parser::posting::Posting;
use crate::parser::transaction::Transaction;
pub fn rebalance(
transactions: &mut [Located<Transaction>],
target: &str,
db: &Index,
fixed_date: Option<&str>,
) {
for lt in transactions {
let lookup_date: String = match fixed_date {
Some(d) => d.to_string(),
None => lt.value.date.to_string(),
};
for lp in &mut lt.value.postings {
convert(&mut lp.value, target, db, &lookup_date);
}
}
}
fn convert(p: &mut Posting, target: &str, db: &Index, date: &str) {
let Some(amount) = p.amount.as_mut() else { return };
if amount.commodity == target {
return;
}
let Some(rate) = db.find(&amount.commodity, target, date) else {
return;
};
amount.value = amount.value.mul_rounded(rate);
amount.commodity = target.to_string();
}