|
@@ -51,6 +51,7 @@ fn try_parse_decimal(from: &str) -> Result<Decimal, ImportError> {
|
|
fn import_from_csv(
|
|
fn import_from_csv(
|
|
csv_spec: &CsvImportSpec,
|
|
csv_spec: &CsvImportSpec,
|
|
aspec: &AccountSpec,
|
|
aspec: &AccountSpec,
|
|
|
|
+ account: AccountName,
|
|
target: AccountName,
|
|
target: AccountName,
|
|
reader: impl std::io::Read,
|
|
reader: impl std::io::Read,
|
|
) -> Result<Vec<Transaction>, ImportError> {
|
|
) -> Result<Vec<Transaction>, ImportError> {
|
|
@@ -76,8 +77,6 @@ fn import_from_csv(
|
|
let date_format = Box::leak(csv_spec.date_format.clone().into_boxed_str());
|
|
let date_format = Box::leak(csv_spec.date_format.clone().into_boxed_str());
|
|
let date_parser = strptime::Parser::new(date_format);
|
|
let date_parser = strptime::Parser::new(date_format);
|
|
|
|
|
|
- let unbalanced = AccountName::new("unbalanced");
|
|
|
|
-
|
|
|
|
let mut txns = vec![];
|
|
let mut txns = vec![];
|
|
|
|
|
|
for record in csv_reader.records() {
|
|
for record in csv_reader.records() {
|
|
@@ -144,14 +143,14 @@ fn import_from_csv(
|
|
annotations: vec![],
|
|
annotations: vec![],
|
|
changes: vec![
|
|
changes: vec![
|
|
Change {
|
|
Change {
|
|
- account: target.into(),
|
|
|
|
|
|
+ account: account.into(),
|
|
amount: txn_change.unwrap().into(),
|
|
amount: txn_change.unwrap().into(),
|
|
balance: txn_balance.map(Into::into),
|
|
balance: txn_balance.map(Into::into),
|
|
unit: txn_unit.or(aspec.unit).map(Into::into).unwrap(),
|
|
unit: txn_unit.or(aspec.unit).map(Into::into).unwrap(),
|
|
}
|
|
}
|
|
.into(),
|
|
.into(),
|
|
Change {
|
|
Change {
|
|
- account: unbalanced.into(),
|
|
|
|
|
|
+ account: target.into(),
|
|
amount: Decimal::ZERO
|
|
amount: Decimal::ZERO
|
|
.checked_sub(txn_change.unwrap())
|
|
.checked_sub(txn_change.unwrap())
|
|
.unwrap()
|
|
.unwrap()
|
|
@@ -239,19 +238,20 @@ fn postprocess(account: AccountName, transactions: &mut Vec<Transaction>) {
|
|
|
|
|
|
pub fn import_from(
|
|
pub fn import_from(
|
|
aspec: &AccountSpec,
|
|
aspec: &AccountSpec,
|
|
|
|
+ account: AccountName,
|
|
target: AccountName,
|
|
target: AccountName,
|
|
path: &std::path::Path,
|
|
path: &std::path::Path,
|
|
) -> Result<Vec<Transaction>, ImportError> {
|
|
) -> Result<Vec<Transaction>, ImportError> {
|
|
let reader = std::fs::File::open(path)?;
|
|
let reader = std::fs::File::open(path)?;
|
|
|
|
|
|
let mut output = match &aspec.import {
|
|
let mut output = match &aspec.import {
|
|
- Some(ImportFileFormat::Csv(csv)) => import_from_csv(csv, aspec, target, reader),
|
|
|
|
|
|
+ Some(ImportFileFormat::Csv(csv)) => import_from_csv(csv, aspec, account, target, reader),
|
|
None => Err(ImportError::ConfigError(format!(
|
|
None => Err(ImportError::ConfigError(format!(
|
|
- "no import configuration for {target}"
|
|
|
|
|
|
+ "no import configuration for {account}"
|
|
))),
|
|
))),
|
|
}?;
|
|
}?;
|
|
|
|
|
|
- postprocess(target, &mut output);
|
|
|
|
|
|
+ postprocess(account, &mut output);
|
|
|
|
|
|
Ok(output)
|
|
Ok(output)
|
|
}
|
|
}
|