import.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. use std::collections::HashMap;
  2. use crate::data::{
  3. AccountName, Datestamp, Decimal, UnitName,
  4. ledger::{Change, Transaction},
  5. spec::{AccountSpec, CsvColumnSpec, CsvImportSpec, ImportFileFormat},
  6. };
  7. #[derive(Debug)]
  8. pub enum ImportError {
  9. IOError(std::io::Error),
  10. ConfigError(String),
  11. InputError(String),
  12. CsvError(csv::Error),
  13. }
  14. impl From<std::io::Error> for ImportError {
  15. fn from(value: std::io::Error) -> Self {
  16. Self::IOError(value)
  17. }
  18. }
  19. impl From<csv::Error> for ImportError {
  20. fn from(value: csv::Error) -> Self {
  21. Self::CsvError(value)
  22. }
  23. }
  24. impl From<strptime::ParseError> for ImportError {
  25. fn from(value: strptime::ParseError) -> Self {
  26. Self::InputError(value.to_string())
  27. }
  28. }
  29. impl From<rust_decimal::Error> for ImportError {
  30. fn from(value: rust_decimal::Error) -> Self {
  31. Self::InputError(value.to_string())
  32. }
  33. }
  34. fn try_parse_decimal(from: &str) -> Result<Decimal, ImportError> {
  35. // remove any '$' or units from the string
  36. let filtered = from
  37. .chars()
  38. .filter(|f| char::is_digit(*f, 10) || *f == '.' || *f == '-')
  39. .collect::<String>();
  40. Decimal::from_str_radix(filtered.as_str(), 10).map_err(Into::into)
  41. }
  42. fn import_from_csv(
  43. csv_spec: &CsvImportSpec,
  44. aspec: &AccountSpec,
  45. target: AccountName,
  46. reader: impl std::io::Read,
  47. ) -> Result<Vec<Transaction>, ImportError> {
  48. let mut csv_reader = csv::Reader::from_reader(reader);
  49. // validate CSV spec
  50. if !csv_spec.cols.contains(&CsvColumnSpec::Datestamp) {
  51. return Err(ImportError::ConfigError(
  52. "CSV config does not have a datestamp column".into(),
  53. ));
  54. }
  55. if !csv_spec.cols.contains(&CsvColumnSpec::Change)
  56. && (!csv_spec.cols.contains(&CsvColumnSpec::Withdraw)
  57. || !csv_spec.cols.contains(&CsvColumnSpec::Deposit))
  58. {
  59. return Err(ImportError::ConfigError(
  60. "CSV config needs either a change column or both withdraw and deposit columns!".into(),
  61. ));
  62. }
  63. // strptime is silly and wants a &'static format string
  64. let date_format = Box::leak(csv_spec.date_format.clone().into_boxed_str());
  65. let date_parser = strptime::Parser::new(date_format);
  66. let unbalanced = AccountName::new("unbalanced");
  67. let mut txns = vec![];
  68. for record in csv_reader.records() {
  69. let record = record?;
  70. let mut txn_datestamp: Option<Datestamp> = None;
  71. let mut txn_title: Option<String> = None;
  72. let mut txn_change: Option<Decimal> = None;
  73. let mut txn_balance: Option<Decimal> = None;
  74. let mut txn_unit: Option<UnitName> = None;
  75. for (record, spec) in record.iter().zip(csv_spec.cols.iter()) {
  76. match spec {
  77. CsvColumnSpec::Ignore => (),
  78. CsvColumnSpec::Datestamp => {
  79. let date = date_parser.parse(record)?.date()?;
  80. txn_datestamp = Some(Datestamp {
  81. year: date.year() as u16,
  82. month: date.month(),
  83. day: date.day(),
  84. });
  85. }
  86. CsvColumnSpec::Title => {
  87. txn_title = Some(record.into());
  88. }
  89. CsvColumnSpec::Deposit => {
  90. if record.trim().is_empty() {
  91. continue;
  92. }
  93. txn_change = Some(try_parse_decimal(record)?);
  94. }
  95. CsvColumnSpec::Withdraw => {
  96. if record.trim().is_empty() {
  97. continue;
  98. }
  99. let mut dec = try_parse_decimal(record)?;
  100. dec.set_sign_negative(true);
  101. txn_change = Some(dec);
  102. }
  103. CsvColumnSpec::Change => {
  104. if record.trim().is_empty() {
  105. continue;
  106. }
  107. txn_change = Some(try_parse_decimal(record)?);
  108. }
  109. CsvColumnSpec::Balance => {
  110. if record.trim().is_empty() {
  111. continue;
  112. }
  113. txn_balance = Some(try_parse_decimal(record)?);
  114. }
  115. CsvColumnSpec::Unit => {
  116. txn_unit = Some(UnitName::new(record));
  117. }
  118. }
  119. }
  120. txns.push(Transaction {
  121. datestamp: txn_datestamp.unwrap(),
  122. title: txn_title,
  123. annotations: vec![],
  124. changes: vec![
  125. Change {
  126. account: target.into(),
  127. amount: txn_change.unwrap().into(),
  128. balance: txn_balance.map(Into::into),
  129. unit: txn_unit.or(aspec.unit).map(Into::into).unwrap(),
  130. }
  131. .into(),
  132. Change {
  133. account: unbalanced.into(),
  134. amount: Decimal::ZERO
  135. .checked_sub(txn_change.unwrap())
  136. .unwrap()
  137. .into(),
  138. balance: None,
  139. unit: txn_unit.or(aspec.unit).map(Into::into).unwrap(),
  140. }
  141. .into(),
  142. ],
  143. });
  144. }
  145. Ok(txns)
  146. }
  147. fn postprocess(account: AccountName, transactions: &mut Vec<Transaction>) {
  148. // check if we need to re-order transactions due to balances not lining up because of ordering
  149. let mut running_balances = HashMap::<UnitName, Decimal>::new();
  150. let mut idx = 0;
  151. // first get things vaguely sorted
  152. transactions.sort_by_key(|tx| tx.datestamp);
  153. let check_for_match = |running_balances: &mut HashMap<UnitName, Decimal>, change: &Change| {
  154. let bal = *change.balance.unwrap();
  155. match running_balances.entry(*change.unit) {
  156. std::collections::hash_map::Entry::Vacant(entry) => {
  157. entry.insert(bal);
  158. return true
  159. },
  160. std::collections::hash_map::Entry::Occupied(mut entry) => {
  161. let rbal = entry.get_mut();
  162. let new_rbal = rbal.checked_add(*change.amount).unwrap();
  163. if new_rbal != bal {
  164. return false
  165. } else {
  166. *rbal = new_rbal;
  167. return true
  168. }
  169. },
  170. }
  171. };
  172. let mut removed : Vec<Transaction> = vec![];
  173. 'outer: loop {
  174. for ridx in 0..removed.len() {
  175. if check_for_match(&mut running_balances, removed[ridx].change_for(account).unwrap()) {
  176. transactions.insert(idx, removed.remove(ridx));
  177. log::trace!("pulling transaction out of removed");
  178. idx += 1;
  179. continue 'outer
  180. }
  181. }
  182. if idx >= transactions.len() {
  183. break
  184. }
  185. let tx = &transactions[idx];
  186. let change = tx.change_for(account).unwrap();
  187. if change.balance.is_none() {
  188. idx += 1;
  189. continue
  190. };
  191. if check_for_match(&mut running_balances, change) {
  192. log::trace!("transaction is good! balance is now: {}", running_balances[&*change.unit]);
  193. idx += 1;
  194. } else {
  195. log::trace!("shifting transaction to removed");
  196. removed.push(transactions.remove(idx));
  197. }
  198. }
  199. if removed.len() > 0 {
  200. log::error!("Not all transactions are consistent!");
  201. }
  202. }
  203. pub fn import_from(
  204. aspec: &AccountSpec,
  205. target: AccountName,
  206. path: &std::path::Path,
  207. ) -> Result<Vec<Transaction>, ImportError> {
  208. let reader = std::fs::File::open(path)?;
  209. let mut output = match &aspec.import {
  210. Some(ImportFileFormat::Csv(csv)) => import_from_csv(csv, aspec, target, reader),
  211. None => Err(ImportError::ConfigError(format!(
  212. "no import configuration for {target}"
  213. ))),
  214. }?;
  215. postprocess(target, &mut output);
  216. Ok(output)
  217. }