Skip to content

Commit 9398f18

Browse files
authored
Sort balance directives ahead of others for the same day (#31)
Fixes #30
1 parent 1ab1e4f commit 9398f18

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "beancount-parser-lima"
3-
version = "0.11.0"
3+
version = "0.11.1"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "A zero-copy parser for Beancount"

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,17 @@ impl<'s> BeancountParser<'s> {
551551
let error_paths = self.sources.error_path_iter().collect::<HashMap<_, _>>();
552552
let mut p = PragmaProcessor::new(self.root_path(), parsed_sources, error_paths, options);
553553

554+
// directives are stable-sorted by date, where balance directives sort ahead of the other directives for that day
555+
// as per https://beancount.github.io/docs/beancount_design_doc.html#stream-invariants
554556
let directives = p
555557
.by_ref()
556-
.sort(|d| *d.item().date().item())
558+
.sort(|d| {
559+
(
560+
*d.item().date().item(),
561+
// secondary sort of Balance ahead of others
562+
!matches!(d.variant(), DirectiveVariant::Balance(_)),
563+
)
564+
})
557565
.collect::<Vec<_>>();
558566
let (options, plugins, mut pragma_errors) = p.result();
559567
errors.append(&mut pragma_errors);
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# ANOMALY: Lima returns directives in date order
21
directives {
32
date { year: 2000 month: 1 day: 1 }
43
open { account: "Assets:Before" }
54
}
6-
directives {
7-
date { year: 2010 month: 1 day: 1 }
8-
close { account: "Assets:Before" }
9-
}
105
directives {
116
date { year: 2010 month: 1 day: 1 }
127
balance {
138
account: "Assets:Before"
149
amount { number { exact: "100" } currency: "USD" }
1510
}
1611
}
12+
directives {
13+
date { year: 2010 month: 1 day: 1 }
14+
close { account: "Assets:Before" }
15+
}

0 commit comments

Comments
 (0)