data.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. use std::collections::HashMap;
  2. use itertools::Itertools;
  3. use ariadne::Cache;
  4. use chumsky::span::Span as _;
  5. pub use rust_decimal::Decimal;
  6. use crate::prelude::*;
  7. pub mod spec;
  8. mod parse;
  9. pub use parse::parse_ledger;
  10. mod format;
  11. pub use format::format_ledger;
  12. pub struct UnitTag;
  13. impl stringstore::NamespaceTag for UnitTag {
  14. const PREFIX: &'static str = "unit";
  15. }
  16. pub type UnitName = stringstore::StoredString<UnitTag>;
  17. pub struct AccountTag;
  18. impl stringstore::NamespaceTag for AccountTag {
  19. const PREFIX: &'static str = "acc";
  20. }
  21. pub type AccountName = stringstore::StoredString<AccountTag>;
  22. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
  23. pub struct Datestamp {
  24. pub year: u16,
  25. pub month: u8,
  26. pub day: u8,
  27. }
  28. impl std::fmt::Display for Datestamp {
  29. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  30. write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
  31. }
  32. }
  33. impl std::fmt::Debug for Datestamp {
  34. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  35. write!(f, "Datestamp ({self})")
  36. }
  37. }
  38. #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq)]
  39. pub struct Change {
  40. pub account: Spanned<AccountName>,
  41. pub amount: Spanned<Decimal>,
  42. pub balance: Option<Spanned<Decimal>>,
  43. pub unit: Spanned<UnitName>,
  44. }
  45. #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
  46. pub struct Transaction {
  47. pub datestamp: Datestamp,
  48. pub title: Option<String>,
  49. pub annotations: Vec<String>,
  50. pub changes: Vec<Spanned<Change>>,
  51. }
  52. impl Transaction {
  53. pub fn modifies(&self, account: AccountName) -> bool {
  54. self.changes.iter().any(|b| b.account.as_ref() == &account)
  55. }
  56. pub fn change_for(&self, account: AccountName) -> Option<&Spanned<Change>> {
  57. self.changes.iter().find(|b| b.account.as_ref() == &account)
  58. }
  59. pub fn split_changes(
  60. &self,
  61. account: AccountName,
  62. ) -> Option<(&Spanned<Change>, impl Iterator<Item = &Spanned<Change>>)> {
  63. let index = self
  64. .changes
  65. .iter()
  66. .position(|b| b.account.as_ref() == &account)?;
  67. Some((
  68. &self.changes[index],
  69. self.changes[0..index]
  70. .iter()
  71. .chain(self.changes[index + 1..].iter()),
  72. ))
  73. }
  74. pub fn is_mono_unit(&self) -> bool {
  75. self.changes.iter().unique_by(|b| *b.unit).count() == 1
  76. }
  77. pub fn mono_unit(&self) -> Option<UnitName> {
  78. let mut it = self.changes.iter().unique_by(|b| *b.unit);
  79. let uniq = it.next()?;
  80. it.next().is_none().then_some(*uniq.unit)
  81. }
  82. pub fn get_annotation(&self, label: &str) -> Option<&str> {
  83. for anno in self.annotations.iter() {
  84. if let Some(body) = anno.strip_prefix(label) {
  85. return Some(body);
  86. }
  87. }
  88. None
  89. }
  90. }
  91. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
  92. pub enum LedgerEntry {
  93. Transaction(Spanned<Transaction>),
  94. Comment(Spanned<String>),
  95. }
  96. impl LedgerEntry {
  97. pub fn as_transaction(&self) -> Option<&Spanned<Transaction>> {
  98. match self {
  99. Self::Transaction(tx) => Some(tx),
  100. _ => None,
  101. }
  102. }
  103. pub fn as_transaction_mut(&mut self) -> Option<&mut Spanned<Transaction>> {
  104. match self {
  105. Self::Transaction(tx) => Some(tx),
  106. _ => None,
  107. }
  108. }
  109. pub fn span(&self) -> io::Span {
  110. match self {
  111. Self::Transaction(ts) => ts.span(),
  112. Self::Comment(c) => c.span(),
  113. }
  114. }
  115. }
  116. #[derive(Debug)]
  117. pub struct Hoard {
  118. path: std::path::PathBuf,
  119. spec_root: spec::SpecRoot,
  120. ledger_data: Vec<LedgerEntry>,
  121. account_ledger_data: HashMap<AccountName, Vec<Spanned<Transaction>>>,
  122. }
  123. impl Hoard {
  124. pub fn load(
  125. fsdata: &mut io::FilesystemData,
  126. path: &std::path::Path,
  127. check_level: check::CheckLevel,
  128. ) -> Result<Self, DataError> {
  129. let sf = io::SourceFile::new(path.as_os_str());
  130. let root_data = fsdata.fetch(&sf).unwrap();
  131. match toml::from_str::<spec::SpecRoot>(root_data.text()) {
  132. Ok(spec_root) => {
  133. let mut r = Self {
  134. path: path.into(),
  135. spec_root,
  136. ledger_data: vec![],
  137. account_ledger_data: Default::default(),
  138. };
  139. r.load_ledgers(fsdata)?;
  140. r.preprocess_ledger_data();
  141. crate::check::run_checks(&mut r, check_level)?;
  142. Ok(r)
  143. }
  144. Err(te) => {
  145. let Some(range) = te.span() else {
  146. panic!("TOML parse error with no range: {te}");
  147. };
  148. let report = ariadne::Report::build(
  149. ariadne::ReportKind::Error,
  150. io::Span::new(sf, range.clone()),
  151. )
  152. .with_label(
  153. ariadne::Label::new(io::Span::new(sf, range)).with_message(te.message()),
  154. )
  155. .with_message("Failed to parse root TOML")
  156. .finish();
  157. Err(report.into())
  158. }
  159. }
  160. }
  161. fn load_ledger(
  162. &mut self,
  163. fsdata: &mut io::FilesystemData,
  164. path: &mut std::path::PathBuf,
  165. ) -> Result<(), DataError> {
  166. log::debug!("Loading ledger data from {}", path.display());
  167. let md = std::fs::metadata(path.as_path()).map_err(DataError::IOError)?;
  168. if md.is_dir() {
  169. // recurse
  170. for de in std::fs::read_dir(path.as_path()).map_err(DataError::IOError)? {
  171. let de = de.map_err(DataError::IOError)?;
  172. path.push(de.file_name());
  173. self.load_ledger(fsdata, path)?;
  174. path.pop();
  175. }
  176. } else {
  177. let path = std::fs::canonicalize(path)?;
  178. let Some(filename) = path.file_name() else {
  179. return Ok(());
  180. };
  181. // skip filenames beginning with a dot
  182. if filename.as_encoded_bytes()[0] == b'.' {
  183. log::info!("Skipping file {}", path.display());
  184. return Ok(());
  185. }
  186. let sf = io::SourceFile::new_from_string(path.into_os_string());
  187. if let Ok(data) = fsdata.fetch(&sf) {
  188. self.ledger_data
  189. .extend(parse_ledger(sf, &self.spec_root, data.text())?);
  190. } else {
  191. log::error!(
  192. "Failed to load data from {}",
  193. std::path::Path::new(sf.as_str()).display()
  194. );
  195. }
  196. }
  197. Ok(())
  198. }
  199. fn load_ledgers(&mut self, fsdata: &mut io::FilesystemData) -> Result<(), DataError> {
  200. let mut ledger_path = std::fs::canonicalize(self.path.as_path())?;
  201. ledger_path.pop();
  202. ledger_path.push(&self.spec_root.ledger_path);
  203. let mut ledger_path = std::fs::canonicalize(ledger_path)?;
  204. self.load_ledger(fsdata, &mut ledger_path)?;
  205. Ok(())
  206. }
  207. fn preprocess_ledger_data(&mut self) {
  208. for entry in &self.ledger_data {
  209. let LedgerEntry::Transaction(tx) = &entry else {
  210. continue;
  211. };
  212. for bal in &tx.changes {
  213. self.account_ledger_data
  214. .entry(*bal.account)
  215. .or_default()
  216. .push(tx.clone());
  217. }
  218. }
  219. }
  220. pub fn all_ledger_data(&self) -> &[LedgerEntry] {
  221. self.ledger_data.as_slice()
  222. }
  223. pub fn all_ledger_data_mut(&mut self) -> &mut [LedgerEntry] {
  224. self.ledger_data.as_mut_slice()
  225. }
  226. pub fn ledger_data_for(&self, aname: AccountName) -> Option<&[Spanned<Transaction>]> {
  227. self.account_ledger_data.get(&aname).map(Vec::as_slice)
  228. }
  229. pub fn ledger_data_for_mut(
  230. &mut self,
  231. aname: AccountName,
  232. ) -> Option<&mut [Spanned<Transaction>]> {
  233. self.account_ledger_data
  234. .get_mut(&aname)
  235. .map(Vec::as_mut_slice)
  236. }
  237. pub fn ledger_data_from(&self, source: io::SourceFile) -> impl Iterator<Item = &LedgerEntry> {
  238. self.all_ledger_data()
  239. .iter()
  240. .filter(move |le| le.span().context == Some(source))
  241. }
  242. pub fn account_names(&self) -> impl Iterator<Item = AccountName> {
  243. self.spec_root.accounts.keys().cloned()
  244. }
  245. pub fn account_spec(&self, aname: AccountName) -> Option<&spec::AccountSpec> {
  246. self.spec_root.accounts.get(&aname)
  247. }
  248. pub fn unit_spec(&self, unit: UnitName) -> Option<&spec::UnitSpec> {
  249. self.spec_root.units.get(&unit)
  250. }
  251. pub fn spec_root(&self) -> &spec::SpecRoot {
  252. &self.spec_root
  253. }
  254. pub fn balance(&self, aname: AccountName) -> Option<HashMap<UnitName, Decimal>> {
  255. let mut running = HashMap::<UnitName, Decimal>::new();
  256. for le in self.ledger_data_for(aname)? {
  257. for b in &le.changes {
  258. if *b.account != aname {
  259. continue;
  260. }
  261. let v = running.entry(*b.unit).or_default();
  262. *v = v.checked_add(*b.amount)?;
  263. }
  264. }
  265. Some(running)
  266. }
  267. }