doppio 0.2.0

A typed compiler pipeline for plain-text Ledger accounting — parse, resolve, and elaborate .ledger files with a library API built for programmatic use.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! doppio — a compiler and query library for the Ledger plain-text
//! accounting format.
//!
//! # `.dop` binary format
//!
//! The `dop compile` command serialises an elaborated journal to a `.dop`
//! file. The file begins with an 8-byte header followed by the postcard +
//! XZ-compressed journal body:
//!
//! ```text
//! Offset  Length  Content
//! 0       4       Magic: b"DOP\0"
//! 4       2       Version: u16 LE (currently 1)
//! 6       2       Reserved: u16 LE (write 0, ignore on read)
//! ```
//!
//! Use [`dop_write_header`] / [`dop_read_header`] for portable, tested I/O of
//! this header.
//!
//! # Pipeline
//!
//! Source text is processed through four stages:
//!
//! ```text
//! source text
//!   → [parser]      ast::Journal        (PEG grammar + Pratt expressions)
//!   → [resolution]  resolution::HIR     (dates, aliases, metadata)
//!   → [elaboration] elaboration::Journal (evaluation, balancing)
//!   → serialisation                     (postcard + XZ → .dop)
//! ```
//!
//! The top-level entry point is [`compile`], which runs all three in-memory
//! stages and returns the elaborated [`Journal`]. For CLI usage see the
//! `dop` binary in `src/main.rs`.
//!
//! # Modules
//!
//! - [`ast`] — abstract syntax tree produced by the parser.
//! - [`parser`] — pest-based parser and `include` directive handling.
//! - [`resolution`] — alias resolution, date normalisation, metadata
//!   extraction.
//! - [`elaboration`] — expression evaluation, transaction balancing, and the
//!   final serialisable [`Journal`] type.
//!
//! # Serialising transactions as Ledger text
//!
//! Use [`write_ledger`] to serialise a sequence of [`resolution::Transaction`]
//! values back to canonical Ledger source text:
//!
//! ```rust
//! # use doppio::resolution::{Transaction, Posting};
//! # use chrono::NaiveDate;
//! let txns = vec![
//!     Transaction::new(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(), "Groceries")
//!         .with_posting(Posting::new("Expenses:Food").with_amount((
//!             rust_decimal::Decimal::from(50u32), "$",
//!         )))
//!         .with_posting(Posting::new("Assets:Checking")),
//! ];
//! let mut out = Vec::new();
//! doppio::write_ledger(txns, &mut out).unwrap();
//! ```

pub mod ast;
pub mod elaboration;
pub mod parser;
pub mod resolution;

pub use elaboration::Journal;

/// Write a sequence of [`resolution::Transaction`] values to `writer` in
/// canonical Ledger source text format.
///
/// Each transaction is formatted using its [`std::fmt::Display`] impl and
/// separated from the next by a blank line. The output is suitable for
/// appending to or creating a `.ledger` source file and round-trips correctly
/// through the parser: `write_ledger(txns)` → parse → resolve should yield
/// semantically equivalent transactions.
///
/// # Errors
///
/// Returns an [`std::io::Error`] if any write to `writer` fails.
///
/// # Example
///
/// ```rust
/// # use doppio::resolution::{Transaction, Posting};
/// # use chrono::NaiveDate;
/// let txns = vec![
///     Transaction::new(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(), "Groceries")
///         .with_posting(Posting::new("Expenses:Food").with_amount((
///             rust_decimal::Decimal::from(50u32), "$",
///         )))
///         .with_posting(Posting::new("Assets:Checking")),
/// ];
/// let mut out = Vec::new();
/// doppio::write_ledger(txns, &mut out).unwrap();
/// let text = String::from_utf8(out).unwrap();
/// assert!(text.starts_with("2024-01-15 Groceries"));
/// ```
pub fn write_ledger<W>(
    entries: impl IntoIterator<Item = resolution::Transaction>,
    writer: &mut W,
) -> std::io::Result<()>
where
    W: std::io::Write,
{
    let mut first = true;
    for txn in entries {
        if !first {
            writeln!(writer)?;
        }
        first = false;
        write!(writer, "{txn}")?;
    }
    Ok(())
}

/// Load and concatenate all files matching a glob pattern.
///
/// This is the default file-opener used by the CLI when processing `include`
/// directives. It is passed to [`parser::Parser`] as the `opener` field.
///
/// ## Glob patterns
///
/// Any path containing `*`, `?`, or `[` is treated as a glob pattern. Matched
/// files are read in **lexicographic order** (sorted by path after expansion)
/// and concatenated into a single string.
///
/// A glob pattern that matches **zero** files is an error — it almost always
/// indicates a misconfigured `include` directive or a missing file tree.
///
/// ## Literal paths
///
/// A path with no glob metacharacters is treated as a single-file include. If
/// the file does not exist, an I/O error is returned.
///
/// ## Errors
///
/// Returns a boxed error if:
/// - `pattern` is not a valid glob expression,
/// - the pattern contains glob metacharacters but matches no files,
/// - a matched path cannot be read (I/O error), or
/// - a literal path does not exist (I/O error).
pub fn file_opener(pattern: &str) -> Result<String, Box<dyn std::error::Error>> {
    use std::io::Read as _;

    // Collect and sort all matching paths. Sorting ensures lexicographic,
    // deterministic ordering regardless of filesystem traversal order.
    let mut paths: Vec<_> = glob::glob(pattern)?
        .collect::<Result<_, _>>()
        .map_err(|e| format!("glob match error for {pattern:?}: {e}"))?;
    paths.sort();

    // A glob with metacharacters that resolves to nothing is always an error.
    // A plain literal path that doesn't exist is caught below by the file open.
    let is_glob = pattern.contains(['*', '?', '[']);
    if is_glob && paths.is_empty() {
        return Err(format!("include glob {pattern:?} matched no files").into());
    }

    // Literal path: glob returns empty when the file doesn't exist (glob
    // silently skips non-existent literal paths). Detect this early.
    if !is_glob && paths.is_empty() {
        return Err(format!("include: file not found: {pattern}").into());
    }

    let mut buf = String::new();
    for path in &paths {
        // Ensure each appended file starts on a fresh line. If the previous
        // file didn't end with a newline, gluing the next file's first line
        // onto the previous one can change parse meaning (e.g. attach a
        // posting to the wrong transaction).
        if !buf.is_empty() && !buf.ends_with('\n') {
            buf.push('\n');
        }
        std::fs::File::open(path)
            .map_err(|e| format!("include: cannot open {}: {e}", path.display()))?
            .read_to_string(&mut buf)
            .map_err(|e| format!("include: cannot read {}: {e}", path.display()))?;
    }

    Ok(buf)
}

/// Compile Ledger source text into a fully elaborated [`Journal`].
///
/// Runs the three in-memory pipeline stages in sequence:
///
/// 1. [`parser::Parser::parse`] — tokenise `input` into an [`ast::Journal`].
/// 2. [`resolution::HIR::try_from`] — resolve dates, aliases, and metadata.
/// 3. [`elaboration::Journal::try_from`] — evaluate amounts and balance
///    transactions.
///
/// The `parser` argument supplies the file-opener for `include` directives and
/// the base path for relative path resolution. For single-file inputs without
/// includes, use [`parser::parse_ledger`] instead.
///
/// # Errors
///
/// Returns a boxed error from the first failing stage (parse error, resolution
/// error, or elaboration error).
pub fn compile<F>(
    input: &str,
    mut parser: parser::Parser<F>,
) -> Result<elaboration::Journal, Box<dyn std::error::Error>>
where
    F: Fn(&str) -> Result<String, Box<dyn std::error::Error>>,
{
    let output = parser.parse(input)?;
    let hir: resolution::HIR = output.try_into()?;
    Ok(hir.try_into()?)
}

/// Evaluate a single [`resolution::Transaction`] through the elaboration stage.
///
/// This is the bridge between programmatic transaction construction (via the
/// [`resolution::Transaction`] builder API) and full elaboration. It resolves
/// aliases, evaluates amount expressions, balances postings, and applies cost
/// basis — returning a fully resolved transaction or an error.
///
/// The `context` parameter supplies alias definitions, commodity aliases, and
/// the default commodity. Use [`resolution::Context::default()`] when no
/// aliases or default commodity are needed.
///
/// Internally this constructs a minimal [`resolution::HIR`] containing the
/// single transaction, runs the elaboration pipeline, and extracts the result.
///
/// # Errors
///
/// Returns an [`elaboration::ElaborationError`] if the transaction cannot be
/// elaborated (e.g. unbalanced postings, expression evaluation failure, or
/// too many null postings).
///
/// # Example
///
/// ```rust
/// use doppio::resolution::{Context, Transaction, Posting};
/// use chrono::NaiveDate;
/// use rust_decimal::Decimal;
///
/// let txn = Transaction::new(
///     NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
///     "Groceries",
/// )
/// .with_posting(
///     Posting::new("Expenses:Food").with_amount((Decimal::from(50u32), "$")),
/// )
/// .with_posting(Posting::new("Assets:Checking"));
///
/// let resolved = doppio::eval_transaction(txn, &Context::default()).unwrap();
/// assert_eq!(resolved.description, "Groceries");
/// assert_eq!(resolved.postings.len(), 2);
/// ```
pub fn eval_transaction(
    txn: resolution::Transaction,
    context: &resolution::Context,
) -> Result<elaboration::ResolvedTransaction, elaboration::ElaborationError> {
    let hir = resolution::HIR {
        entries: vec![resolution::ResolutionEntry {
            context_id: 0,
            data: resolution::Entry::Transaction(txn),
        }],
        contexts: vec![context.clone()],
        ..Default::default()
    };
    let journal = elaboration::Journal::try_from(hir)?;
    // The HIR contained exactly one transaction, so the journal has exactly one.
    Ok(journal
        .transactions
        .into_iter()
        .next()
        .expect("journal should contain exactly one transaction"))
}

// ──────────────────────────────────────────────────────────────────────────────
// .dop header helpers
// ──────────────────────────────────────────────────────────────────────────────

/// Four-byte magic that identifies every `.dop` file.
pub const DOP_MAGIC: [u8; 4] = *b"DOP\0";

/// Format version embedded in every `.dop` header.
///
/// Bump this constant (and update [`dop_read_header`]) whenever the
/// serialisation format changes in a breaking way.
pub const DOP_FORMAT_VERSION: u16 = 2;

/// Write the 8-byte `.dop` header to `writer`.
///
/// Layout: magic (4 bytes) + version LE u16 (2 bytes) + reserved u16 (2 bytes).
///
/// # Errors
///
/// Propagates any [`std::io::Error`] from `writer`.
pub fn dop_write_header<W: std::io::Write>(writer: &mut W) -> std::io::Result<()> {
    writer.write_all(&DOP_MAGIC)?;
    writer.write_all(&DOP_FORMAT_VERSION.to_le_bytes())?;
    writer.write_all(&0u16.to_le_bytes())?;
    Ok(())
}

/// Read and validate the 8-byte `.dop` header from `reader`.
///
/// `path` is used only for error messages; it should be the path of the file
/// being opened so diagnostics point to the right location.
///
/// # Errors
///
/// Returns a boxed error with a user-actionable message if:
/// - the magic bytes are missing or incorrect,
/// - the format version is not [`DOP_FORMAT_VERSION`].
pub fn dop_read_header<R: std::io::Read>(
    reader: &mut R,
    path: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut magic = [0u8; 4];
    // A short read here means the file is too small to be valid.
    reader.read_exact(&mut magic).map_err(|_| {
        format!(
            "{}: not a valid .dop file (missing magic header); \
             recompile from source with `dop compile`",
            path.display()
        )
    })?;
    if magic != DOP_MAGIC {
        return Err(format!(
            "{}: not a valid .dop file (missing magic header); \
             recompile from source with `dop compile`",
            path.display()
        )
        .into());
    }

    let mut version_bytes = [0u8; 2];
    reader.read_exact(&mut version_bytes)?;
    let version = u16::from_le_bytes(version_bytes);
    if version != DOP_FORMAT_VERSION {
        return Err(format!(
            "{}: incompatible .dop format version {} \
             (this binary supports version {}); \
             recompile from source with `dop compile`",
            path.display(),
            version,
            DOP_FORMAT_VERSION,
        )
        .into());
    }

    // Skip the 2 reserved bytes — ignore their value on read.
    let mut reserved = [0u8; 2];
    reader.read_exact(&mut reserved)?;
    Ok(())
}

#[cfg(test)]
mod write_ledger_tests {
    use chrono::NaiveDate;
    use rust_decimal::Decimal;

    use super::*;

    fn date(y: i32, m: u32, d: u32) -> NaiveDate {
        NaiveDate::from_ymd_opt(y, m, d).unwrap()
    }

    /// Parse a Ledger-format source string and return the resolved transactions.
    fn parse_transactions(source: &str) -> Vec<resolution::Transaction> {
        let mut p = parser::Parser {
            opener: |_: &str| Ok(String::new()),
            base_path: std::path::PathBuf::new(),
        };
        let ast_journal = p.parse(&source.to_string()).expect("parse failed");
        let hir: resolution::HIR = ast_journal.try_into().expect("resolution failed");
        hir.transactions().collect()
    }

    #[test]
    fn write_empty_iterator_produces_no_output() {
        let mut out: Vec<u8> = Vec::new();
        write_ledger(std::iter::empty::<resolution::Transaction>(), &mut out).unwrap();
        assert!(out.is_empty());
    }

    #[test]
    fn write_single_transaction_basic() {
        let txn = resolution::Transaction::new(date(2024, 1, 15), "Groceries")
            .with_posting(
                resolution::Posting::new("Expenses:Food").with_amount((Decimal::from(50u32), "$")),
            )
            .with_posting(resolution::Posting::new("Assets:Checking"));

        let mut out: Vec<u8> = Vec::new();
        write_ledger([txn], &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();

        assert_eq!(
            text,
            "2024-01-15 Groceries\n    Expenses:Food  50 $\n    Assets:Checking\n"
        );
    }

    #[test]
    fn multiple_transactions_separated_by_blank_line() {
        let txns = vec![
            resolution::Transaction::new(date(2024, 1, 1), "First"),
            resolution::Transaction::new(date(2024, 1, 2), "Second"),
        ];

        let mut out: Vec<u8> = Vec::new();
        write_ledger(txns, &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();

        assert_eq!(text, "2024-01-01 First\n\n2024-01-02 Second\n");
    }

    #[test]
    fn round_trip_preserves_date_and_description() {
        let original = resolution::Transaction::new(date(2024, 3, 15), "Salary payment")
            .with_state(ast::TransactionState::Cleared)
            .with_posting(
                resolution::Posting::new("Income:Salary")
                    .with_amount((Decimal::from(5000u32), "USD")),
            )
            .with_posting(resolution::Posting::new("Assets:Checking"));

        let mut out: Vec<u8> = Vec::new();
        write_ledger([original], &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();

        let parsed = parse_transactions(&text);
        assert_eq!(parsed.len(), 1);
        let roundtripped = &parsed[0];

        assert_eq!(roundtripped.date, date(2024, 3, 15));
        assert_eq!(roundtripped.description, "Salary payment");
        assert!(matches!(roundtripped.state, ast::TransactionState::Cleared));
        assert_eq!(roundtripped.postings.len(), 2);
        assert_eq!(roundtripped.postings[0].account, "Income:Salary");
        assert_eq!(roundtripped.postings[1].account, "Assets:Checking");
    }

    #[test]
    fn round_trip_preserves_metadata_and_tags() {
        // Use two comments so they are emitted as indented `; comment` note
        // lines (rather than inlined on the header), which round-trip cleanly
        // through the parser/resolver pipeline.
        let original = resolution::Transaction::new(date(2024, 6, 1), "Grant revenue")
            .with_tag("income")
            .with_comment("Q2 payment")
            .with_comment("approved")
            .with_metadata("program", "Grant:UW:HARVEST")
            .with_metadata("ref", "INV-001")
            .with_posting(
                resolution::Posting::new("Income:Grants")
                    .with_amount((Decimal::from(10_000u32), "$")),
            )
            .with_posting(resolution::Posting::new("Assets:Checking"));

        let mut out: Vec<u8> = Vec::new();
        write_ledger([original], &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();

        let parsed = parse_transactions(&text);
        assert_eq!(parsed.len(), 1);
        let rt = &parsed[0];

        assert!(
            rt.tags.contains(&"income".to_string()),
            "tag 'income' missing from {rt:?}"
        );
        assert!(
            rt.comments.contains(&"Q2 payment".to_string()),
            "comment 'Q2 payment' missing from {rt:?}",
        );
        assert!(
            rt.comments.contains(&"approved".to_string()),
            "comment 'approved' missing from {rt:?}",
        );
        assert_eq!(
            rt.metadata.get("program").map(String::as_str),
            Some("Grant:UW:HARVEST")
        );
        assert_eq!(rt.metadata.get("ref").map(String::as_str), Some("INV-001"));
    }

    #[test]
    fn round_trip_multiple_transactions() {
        let txns = vec![
            resolution::Transaction::new(date(2024, 1, 10), "Food")
                .with_posting(
                    resolution::Posting::new("Expenses:Food")
                        .with_amount((Decimal::from(30u32), "$")),
                )
                .with_posting(resolution::Posting::new("Assets:Checking")),
            resolution::Transaction::new(date(2024, 1, 20), "Rent")
                .with_state(ast::TransactionState::Cleared)
                .with_posting(
                    resolution::Posting::new("Expenses:Rent")
                        .with_amount((Decimal::from(1200u32), "$")),
                )
                .with_posting(resolution::Posting::new("Assets:Checking")),
        ];

        let mut out: Vec<u8> = Vec::new();
        write_ledger(txns, &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();

        let parsed = parse_transactions(&text);
        assert_eq!(parsed.len(), 2);

        assert_eq!(parsed[0].description, "Food");
        assert_eq!(parsed[0].date, date(2024, 1, 10));

        assert_eq!(parsed[1].description, "Rent");
        assert_eq!(parsed[1].date, date(2024, 1, 20));
        assert!(matches!(parsed[1].state, ast::TransactionState::Cleared));
    }

    #[test]
    fn round_trip_posting_with_metadata() {
        let original = resolution::Transaction::new(date(2024, 4, 1), "Payroll")
            .with_posting(
                resolution::Posting::new("Expenses:Salary")
                    .with_amount((Decimal::from(3000u32), "$"))
                    .with_metadata("employee", "alice")
                    .with_tag("payroll"),
            )
            .with_posting(resolution::Posting::new("Assets:Bank"));

        let mut out: Vec<u8> = Vec::new();
        write_ledger([original], &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();

        let parsed = parse_transactions(&text);
        assert_eq!(parsed.len(), 1);
        let posting = &parsed[0].postings[0];

        assert_eq!(posting.account, "Expenses:Salary");
        assert_eq!(
            posting.metadata.get("employee").map(String::as_str),
            Some("alice")
        );
        assert!(posting.tags.contains(&"payroll".to_string()));
    }
}

#[cfg(test)]
mod eval_transaction_tests {
    use chrono::NaiveDate;
    use rust_decimal::{Decimal, dec};

    use super::*;

    fn date(y: i32, m: u32, d: u32) -> NaiveDate {
        NaiveDate::from_ymd_opt(y, m, d).unwrap()
    }

    #[test]
    fn simple_two_posting_transaction() {
        let txn = resolution::Transaction::new(date(2024, 1, 15), "Groceries")
            .with_posting(
                resolution::Posting::new("Expenses:Food").with_amount((Decimal::from(50u32), "$")),
            )
            .with_posting(resolution::Posting::new("Assets:Checking"));

        let resolved = eval_transaction(txn, &resolution::Context::default()).unwrap();

        assert_eq!(resolved.description, "Groceries");
        assert_eq!(resolved.postings.len(), 2);

        let food = resolved
            .postings
            .iter()
            .find(|p| p.account == "Expenses:Food")
            .unwrap();
        assert_eq!(food.amount.0.get("$").copied(), Some(dec!(50)));

        let checking = resolved
            .postings
            .iter()
            .find(|p| p.account == "Assets:Checking")
            .unwrap();
        assert_eq!(checking.amount.0.get("$").copied(), Some(dec!(-50)));
    }

    #[test]
    fn null_posting_inferred() {
        let txn = resolution::Transaction::new(date(2024, 2, 1), "Rent")
            .with_posting(
                resolution::Posting::new("Expenses:Rent")
                    .with_amount((Decimal::from(1200u32), "$")),
            )
            .with_posting(resolution::Posting::new("Assets:Checking"));

        let resolved = eval_transaction(txn, &resolution::Context::default()).unwrap();

        let checking = resolved
            .postings
            .iter()
            .find(|p| p.account == "Assets:Checking")
            .unwrap();
        assert_eq!(
            checking.amount.0.get("$").copied(),
            Some(dec!(-1200)),
            "null posting should be inferred as -$1200"
        );
    }

    #[test]
    fn explicit_balanced_amounts() {
        let txn = resolution::Transaction::new(date(2024, 3, 1), "Transfer")
            .with_posting(
                resolution::Posting::new("Assets:Savings")
                    .with_amount((Decimal::from(500u32), "$")),
            )
            .with_posting(
                resolution::Posting::new("Assets:Checking")
                    .with_amount((Decimal::from(-500i32), "$")),
            );

        let resolved = eval_transaction(txn, &resolution::Context::default()).unwrap();
        assert_eq!(resolved.postings.len(), 2);
    }

    #[test]
    fn unbalanced_transaction_returns_error() {
        let txn = resolution::Transaction::new(date(2024, 4, 1), "Bad")
            .with_posting(
                resolution::Posting::new("Expenses:Food").with_amount((Decimal::from(100u32), "$")),
            )
            .with_posting(
                resolution::Posting::new("Assets:Checking")
                    .with_amount((Decimal::from(-50i32), "$")),
            );

        let result = eval_transaction(txn, &resolution::Context::default());
        assert!(
            result.is_err(),
            "unbalanced transaction should return an error"
        );
        assert!(matches!(
            result.unwrap_err(),
            elaboration::ElaborationError::TransactionDoesNotBalance(_)
        ));
    }

    #[test]
    fn account_alias_resolved_via_context() {
        let mut context = resolution::Context::default();
        context
            .account_aliases
            .insert("Checking".into(), "Assets:Checking:Mercury:7920".into());

        let txn = resolution::Transaction::new(date(2024, 5, 1), "Deposit")
            .with_posting(
                resolution::Posting::new("Income:Salary")
                    .with_amount((Decimal::from(5000u32), "$")),
            )
            .with_posting(resolution::Posting::new("Checking"));

        let resolved = eval_transaction(txn, &context).unwrap();

        let checking = resolved
            .postings
            .iter()
            .find(|p| p.account == "Assets:Checking:Mercury:7920")
            .expect("alias should resolve to canonical account name");
        assert_eq!(checking.amount.0.get("$").copied(), Some(dec!(-5000)));
    }

    #[test]
    fn default_commodity_from_context() {
        let mut context = resolution::Context::default();
        context.default_commodity = Some("USD".into());

        // Amount with no commodity — should use the context default.
        // Use ValueExpr::Amount with commodity: None to produce a bare amount.
        let bare = ast::ValueExpr::Amount {
            value: Decimal::from(25u32),
            commodity: None,
        };
        let txn = resolution::Transaction::new(date(2024, 6, 1), "Bare amount")
            .with_posting(resolution::Posting::new("Expenses:Food").with_amount(bare))
            .with_posting(resolution::Posting::new("Assets:Cash"));

        let resolved = eval_transaction(txn, &context).unwrap();

        let food = resolved
            .postings
            .iter()
            .find(|p| p.account == "Expenses:Food")
            .unwrap();
        assert_eq!(
            food.amount.0.get("USD").copied(),
            Some(dec!(25)),
            "bare amount should use default commodity from context"
        );
    }

    #[test]
    fn resolved_transaction_preserves_fields() {
        let txn = resolution::Transaction::new(date(2024, 7, 4), "Independence Day")
            .with_state(ast::TransactionState::Cleared)
            .with_code("IND-04")
            .with_secondary_date(date(2024, 7, 5))
            .with_tag("holiday")
            .with_metadata("ref", "USA")
            .with_posting(
                resolution::Posting::new("Expenses:Celebration")
                    .with_amount((Decimal::from(200u32), "$")),
            )
            .with_posting(resolution::Posting::new("Assets:Checking"));

        let resolved = eval_transaction(txn, &resolution::Context::default()).unwrap();

        assert_eq!(resolved.description, "Independence Day");
        assert!(matches!(
            resolved.state,
            elaboration::TransactionState::Cleared
        ));
        assert_eq!(resolved.code.as_deref(), Some("IND-04"));
        assert!(resolved.secondary_date.is_some());
        assert!(resolved.tags.contains(&"holiday".to_string()));
        assert_eq!(
            resolved.metadata.get("ref").map(String::as_str),
            Some("USA")
        );
    }

    #[test]
    fn too_many_null_postings_returns_error() {
        let txn = resolution::Transaction::new(date(2024, 8, 1), "Ambiguous")
            .with_posting(resolution::Posting::new("Expenses:A"))
            .with_posting(resolution::Posting::new("Expenses:B"));

        let result = eval_transaction(txn, &resolution::Context::default());
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            elaboration::ElaborationError::TooManyNullPostings
        ));
    }
}