rustledger 0.16.4

Drop-in replacement for Beancount. Pure Rust, 10-30x faster.
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
//! Comprehensive tests for all beancount fixtures.
//!
//! This module tests:
//! - tests/fixtures/syntax-edge-cases.beancount - Parser edge cases
//! - tests/fixtures/booking-scenarios.beancount - Booking algorithm scenarios
//! - tests/fixtures/validation-errors.beancount - Expected validation errors
//! - tests/fixtures/lima-tests/*.beancount - 218 parser conformance tests
//! - tests/fixtures/examples/*.beancount - Official beancount examples

mod common;

use std::path::{Path, PathBuf};
use std::process::Command;

use common::{project_root, rledger_binary};

fn spec_fixtures_dir() -> PathBuf {
    project_root().join("tests/fixtures")
}

/// Run rledger check on a file and return (success, output).
/// Returns None if the rledger binary is not available.
fn rledger_check(path: &Path) -> Option<(bool, String)> {
    let binary = rledger_binary()?;
    let output = Command::new(binary)
        .arg("check")
        .arg(path)
        .output()
        .expect("Failed to run rledger check");

    let success = output.status.success();
    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    Some((success, combined))
}

/// Run rledger check with `auto_accounts` plugin (permissive mode like Python beancount).
/// Returns None if the rledger binary is not available.
fn rledger_check_permissive(path: &Path) -> Option<(bool, String)> {
    let binary = rledger_binary()?;
    let output = Command::new(binary)
        .arg("check")
        .arg("--native-plugin")
        .arg("auto_accounts")
        .arg(path)
        .output()
        .expect("Failed to run rledger check");

    let success = output.status.success();
    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    Some((success, combined))
}

/// Parse a file with the parser and return (success, `error_count`).
fn parse_file(path: &Path) -> (bool, usize) {
    let source = std::fs::read_to_string(path).expect("Failed to read file");
    let result = rustledger_parser::parse(&source);
    (result.errors.is_empty(), result.errors.len())
}

// =============================================================================
// SYNTAX EDGE CASES
// =============================================================================

#[test]
fn test_syntax_edge_cases_parses() {
    let path = spec_fixtures_dir().join("syntax-edge-cases.beancount");
    if !path.exists() {
        eprintln!("Skipping: syntax-edge-cases.beancount not found");
        return;
    }

    let (success, error_count) = parse_file(&path);
    // Note: This file contains some syntax that rustledger may not support yet.
    // Track parse error count for regression.
    if !success {
        eprintln!(
            "syntax-edge-cases.beancount has {error_count} parse errors (tracking for future fixes)"
        );
        assert!(
            error_count == 0,
            "syntax-edge-cases.beancount should have 0 parse errors, got {error_count}"
        );
    }
}

// =============================================================================
// BOOKING SCENARIOS
// =============================================================================

#[test]
fn test_booking_scenarios_parses() {
    let path = spec_fixtures_dir().join("booking-scenarios.beancount");
    if !path.exists() {
        eprintln!("Skipping: booking-scenarios.beancount not found");
        return;
    }

    let (success, error_count) = parse_file(&path);
    assert!(
        success,
        "booking-scenarios.beancount should parse without errors, but got {error_count} errors"
    );
}

#[test]
fn test_booking_scenarios_validates() {
    let path = spec_fixtures_dir().join("booking-scenarios.beancount");
    if !path.exists() {
        eprintln!("Skipping: booking-scenarios.beancount not found");
        return;
    }

    // Use permissive mode since this file may not have all Open directives
    let Some((success, output)) = rledger_check_permissive(&path) else {
        eprintln!("Skipping: rledger binary not found");
        return;
    };
    // Note: This file tests booking scenarios which may require the booking
    // engine to fill in missing amounts. Some E3001 (unbalanced) errors are
    // expected if the booking engine hasn't processed the transactions.
    if !success {
        // Check if errors are only E3001 (expected for booking scenarios)
        // or E1001 (account not opened - would be fixed by auto_accounts)
        let has_unexpected_errors = output.lines().any(|line| {
            line.contains("error[E") && !line.contains("E3001") && !line.contains("E1001")
        });
        assert!(
            !has_unexpected_errors,
            "booking-scenarios.beancount has unexpected errors: {output}"
        );
        eprintln!("booking-scenarios.beancount has expected booking-related errors (E3001/E1001)");
    }
}

// =============================================================================
// VALIDATION ERRORS (expected to produce specific errors)
// =============================================================================

#[test]
fn test_validation_errors_parses() {
    let path = spec_fixtures_dir().join("validation-errors.beancount");
    if !path.exists() {
        eprintln!("Skipping: validation-errors.beancount not found");
        return;
    }

    let (success, error_count) = parse_file(&path);
    assert!(
        success,
        "validation-errors.beancount should parse without errors, but got {error_count} errors"
    );
}

#[test]
fn test_validation_errors_produces_expected_errors() {
    let path = spec_fixtures_dir().join("validation-errors.beancount");
    if !path.exists() {
        eprintln!("Skipping: validation-errors.beancount not found");
        return;
    }

    let Some((success, output)) = rledger_check(&path) else {
        eprintln!("Skipping: rledger binary not found");
        return;
    };

    // This file should produce validation errors (not parse errors)
    assert!(
        !success,
        "validation-errors.beancount should produce validation errors"
    );

    // Check for expected error codes
    assert!(
        output.contains("E1001") || output.contains("Account") && output.contains("not open"),
        "Should contain E1001 (account not opened): {output}"
    );
}

// =============================================================================
// LIMA TESTS - Parser conformance tests
// =============================================================================

/// Get all lima test files
fn lima_test_files() -> Vec<PathBuf> {
    let lima_dir = spec_fixtures_dir().join("lima-tests");
    if !lima_dir.exists() {
        return vec![];
    }

    std::fs::read_dir(&lima_dir)
        .expect("Failed to read lima-tests directory")
        .filter_map(std::result::Result::ok)
        .map(|entry| entry.path())
        .filter(|path| path.extension().is_some_and(|ext| ext == "beancount"))
        .collect()
}

/// Lima tests that are expected to have parse errors (based on test name)
fn lima_test_expects_parse_error(name: &str) -> bool {
    name.contains("LexerAndParserErrors")
        || name.contains("GrammarException")
        || name.contains("GrammarSyntaxError")
        || name.contains("IndentError")
        || name.contains("InvalidOption")
        || name.contains("BlankLineNotAllowed")
        || name.contains("BlankLineWithSpacesNotAllowed")
        || name.contains("IndentEOF")
        || name.starts_with("SyntaxErrors")
        // Push/pop tag/meta validation errors:
        || name.contains("TagLeftUnclosed")       // pushtag never popped
        || name.contains("PopInvalidTag")         // poptag for unpushed tag
        || name.contains("PushmetaForgotten")     // pushmeta never popped
        || name.contains("PushmetaInvalidPop")    // popmeta for unpushed key
        // Parser features not yet supported:
        || name.contains("BalanceWithCost")       // balance with cost spec
        || name.contains("CostEmptyComponents")   // empty cost spec components
        || name.contains("CostWithSlashes")       // cost with slashes notation
        || name.contains("CommentInPostingsInvalid") // invalid comment in postings
        || name.contains("TagThenLink") // tag ordering edge case
}

#[test]
fn test_lima_tests_parse() {
    let files = lima_test_files();
    if files.is_empty() {
        eprintln!("Skipping: No lima test files found");
        return;
    }

    let mut failures = Vec::new();

    for path in &files {
        let file_name = path.file_stem().unwrap().to_string_lossy().to_string();
        let expects_error = lima_test_expects_parse_error(&file_name);

        let (parse_success, error_count) = parse_file(path);

        if expects_error {
            // These tests are expected to have parse errors
            // We just verify we don't panic
            continue;
        }

        if !parse_success {
            failures.push(format!(
                "{}: {} parse errors",
                path.file_name().unwrap().to_string_lossy(),
                error_count
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "Lima tests with unexpected parse errors:\n{}",
        failures.join("\n")
    );
}

#[test]
fn test_lima_test_count() {
    let files = lima_test_files();
    // Skip if lima-tests directory doesn't exist (e.g., in CI without fixtures)
    if files.is_empty() {
        eprintln!("Skipping: lima-tests directory not found or empty");
        return;
    }
    // We expect at least 200 lima test files when the directory exists
    assert!(
        files.len() >= 200,
        "Expected at least 200 lima test files, found {}",
        files.len()
    );
}

// =============================================================================
// EXAMPLE FILES
// =============================================================================

/// Get all example beancount files
fn example_files() -> Vec<PathBuf> {
    let examples_dir = spec_fixtures_dir().join("examples");
    if !examples_dir.exists() {
        return vec![];
    }

    walkdir(&examples_dir)
        .into_iter()
        .filter(|path| path.extension().is_some_and(|ext| ext == "beancount"))
        .collect()
}

/// Files known to have unbalanced transactions or other issues in Python beancount too
fn example_file_has_known_issues(path: &Path) -> bool {
    let path_str = path.to_string_lossy();
    let file_name = path.file_name().unwrap().to_string_lossy();
    // These files have issues even in Python beancount or use plugins we don't support
    path_str.contains("forecast") // Uses Python plugins for forecasting
        || path_str.contains("vesting") // Uses complex Python plugins
        || path_str.contains("ingest") // Importer framework files
        || path_str.contains("sharing") // Uses beancount.plugins.split_expenses
        // These files parse correctly but have validation issues (unbalanced transactions, etc.)
        || file_name == "starter.beancount"
        || file_name == "basic.beancount"
}

/// Simple recursive directory walker
fn walkdir(dir: &Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                files.extend(walkdir(&path));
            } else {
                files.push(path);
            }
        }
    }
    files
}

#[test]
fn test_example_files_parse() {
    let files = example_files();
    if files.is_empty() {
        eprintln!("Skipping: No example files found");
        return;
    }

    let mut failures = Vec::new();

    for path in &files {
        let (parse_success, error_count) = parse_file(path);
        if !parse_success {
            failures.push(format!(
                "{}: {} parse errors",
                path.strip_prefix(project_root()).unwrap_or(path).display(),
                error_count
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "Example files with parse errors:\n{}",
        failures.join("\n")
    );
}

#[test]
fn test_example_files_validate() {
    // These example files are from Python beancount and expect permissive behavior
    // (auto-opening accounts). We use the auto_accounts plugin to match that.
    let files = example_files();
    if files.is_empty() {
        eprintln!("Skipping: No example files found");
        return;
    }

    // Check if rledger binary is available before iterating
    if rledger_binary().is_none() {
        eprintln!("Skipping: rledger binary not found");
        return;
    }

    let mut failures = Vec::new();
    let mut skipped = 0;

    for path in &files {
        // Skip files with known validation issues
        if example_file_has_known_issues(path) {
            skipped += 1;
            continue;
        }

        // Use permissive mode (auto_accounts) since these examples don't always
        // have explicit Open directives - Python beancount auto-creates them
        let Some((success, output)) = rledger_check_permissive(path) else {
            continue; // Should not happen since we checked above
        };
        if !success {
            // Truncate output for readability
            let short_output: String = output.lines().take(3).collect::<Vec<_>>().join("\n");
            failures.push(format!(
                "{}: {}",
                path.strip_prefix(project_root()).unwrap_or(path).display(),
                short_output
            ));
        }
    }

    if skipped > 0 {
        eprintln!("Skipped {skipped} files with known validation issues");
    }

    assert!(
        failures.is_empty(),
        "Example files with validation errors (with auto_accounts):\n{}",
        failures.join("\n\n")
    );
}

#[test]
fn test_example_file_count() {
    let files = example_files();
    // Skip if examples directory doesn't exist (e.g., in CI without fixtures)
    if files.is_empty() {
        eprintln!("Skipping: examples directory not found or empty");
        return;
    }
    // We expect at least 5 example files when the directory exists
    assert!(
        files.len() >= 5,
        "Expected at least 5 example files, found {}",
        files.len()
    );
}

// =============================================================================
// INDIVIDUAL LIMA TESTS (for more granular failure reporting)
// =============================================================================

macro_rules! lima_test {
    ($test_name:ident, $file_name:literal) => {
        #[test]
        fn $test_name() {
            let path = spec_fixtures_dir().join("lima-tests").join($file_name);
            if !path.exists() {
                eprintln!("Skipping: {} not found", $file_name);
                return;
            }
            let (success, error_count) = parse_file(&path);
            assert!(
                success,
                "{} should parse without errors, got {} errors",
                $file_name, error_count
            );
        }
    };
    ($test_name:ident, $file_name:literal, expect_error) => {
        #[test]
        fn $test_name() {
            let path = spec_fixtures_dir().join("lima-tests").join($file_name);
            if !path.exists() {
                eprintln!("Skipping: {} not found", $file_name);
                return;
            }
            // Just verify we don't panic - errors are expected
            let _ = parse_file(&path);
        }
    };
}

// Parser entry type tests
lima_test!(lima_balance, "ParserEntryTypes.Balance.beancount");
lima_test!(
    lima_balance_with_cost,
    "ParserEntryTypes.BalanceWithCost.beancount",
    expect_error // Parser doesn't yet support balance with cost specs
);
lima_test!(lima_commodity, "ParserEntryTypes.Commodity.beancount");
lima_test!(lima_note, "ParserEntryTypes.Note.beancount");
lima_test!(lima_open3, "ParserEntryTypes.Open3.beancount");
lima_test!(lima_pad, "ParserEntryTypes.Pad.beancount");
lima_test!(lima_price, "ParserEntryTypes.Price.beancount");
lima_test!(
    lima_transaction_one_string,
    "ParserEntryTypes.TransactionOneString.beancount"
);

// Balance tests
lima_test!(lima_balance_total_cost, "Balance.TotalCost.beancount");
lima_test!(lima_balance_total_price, "Balance.TotalPrice.beancount");

// Cost/lot parsing tests
lima_test!(lima_cost_none, "ParseLots.CostNone.beancount");
lima_test!(
    lima_cost_total_just_currency,
    "ParseLots.CostTotalJustCurrency.beancount"
);
lima_test!(
    lima_cost_three_components,
    "ParseLots.CostThreeComponents.beancount"
);

// Currency tests
lima_test!(
    lima_parse_currencies,
    "Currencies.ParseCurrencies.beancount"
);

// Metadata tests
lima_test!(
    lima_metadata_transaction_indented,
    "MetaData.MetadataTransactionIndented.beancount"
);

// Multiline tests
lima_test!(
    lima_multiline_narration,
    "MultipleLines.MultilineNarration.beancount"
);

// Push/pop tag tests
lima_test!(lima_pushtag_multiple, "PushPopTag.Multiple.beancount");
lima_test!(
    lima_pushtag_left_unclosed,
    "PushPopTag.TagLeftUnclosed.beancount",
    expect_error
);

// Push/pop meta tests
lima_test!(lima_pushmeta_shadow, "PushPopMeta.PushmetaShadow.beancount");
lima_test!(
    lima_pushmeta_override,
    "PushPopMeta.PushmetaOverride.beancount"
);
lima_test!(
    lima_pushmeta_forgotten,
    "PushPopMeta.PushmetaForgotten.beancount",
    expect_error
);

// Totals and signs tests
lima_test!(
    lima_total_price_negative,
    "TotalsAndSigns.TotalPriceNegative.beancount"
);
lima_test!(
    lima_total_price_inverted,
    "TotalsAndSigns.TotalPriceInverted.beancount"
);
lima_test!(
    lima_total_price_with_missing,
    "TotalsAndSigns.TotalPriceWithMissing.beancount"
);
lima_test!(lima_cost_negative, "TotalsAndSigns.CostNegative.beancount");

// Transaction tests
lima_test!(lima_txn_no_postings, "Transactions.NoPostings.beancount");

// Incomplete input tests
lima_test!(
    lima_incomplete_price_missing_number,
    "IncompleteInputs.PriceMissingNumber.beancount"
);
lima_test!(
    lima_incomplete_price_missing,
    "IncompleteInputs.PriceMissing.beancount"
);
lima_test!(
    lima_incomplete_price_none,
    "IncompleteInputs.PriceNone.beancount"
);
lima_test!(
    lima_incomplete_units_missing_currency,
    "IncompleteInputs.UnitsMissingCurrency.beancount"
);
lima_test!(
    lima_incomplete_units_missing_with_cost,
    "IncompleteInputs.UnitsMissingWithCost.beancount"
);
lima_test!(
    lima_incomplete_units_missing_currency_with_price,
    "IncompleteInputs.UnitsMissingCurrencyWithPrice.beancount"
);
lima_test!(
    lima_incomplete_cost_missing_number_total,
    "IncompleteInputs.CostMissingNumberTotal.beancount"
);
lima_test!(
    lima_incomplete_cost_missing_numbers,
    "IncompleteInputs.CostMissingNumbers.beancount"
);
lima_test!(
    lima_incomplete_cost_average_with_other,
    "IncompleteInputs.CostAverageWithOther.beancount"
);

// Parser options tests
lima_test!(
    lima_readonly_option,
    "ParserOptions.ReadonlyOption.beancount"
);
lima_test!(
    lima_invalid_option,
    "ParserOptions.InvalidOption.beancount",
    expect_error
);

// Parser complete tests (whitespace handling)
lima_test!(
    lima_extra_whitespace_note,
    "ParserComplete.ExtraWhitespaceNote.beancount"
);
lima_test!(
    lima_extra_whitespace_transaction,
    "ParserComplete.ExtraWhitespaceTransaction.beancount"
);
lima_test!(lima_comment_eof, "ParserComplete.CommentEOF.beancount");

// Display context tests
lima_test!(
    lima_render_commas_no,
    "DisplayContextOptions.RenderCommasNo.beancount"
);

// Document tests
lima_test!(lima_document_links, "Document.DocumentLinks.beancount");

// Deprecated options tests
lima_test!(
    lima_deprecated_option,
    "DeprecatedOptions.DeprecatedOption.beancount"
);

// Error tests (expected to have parse errors)
lima_test!(
    lima_grammar_syntax_error_multiple,
    "LexerAndParserErrors.GrammarSyntaxErrorMultiple.beancount",
    expect_error
);
lima_test!(
    lima_lexer_exception_recovery,
    "LexerAndParserErrors.LexerExceptionRecovery.beancount",
    expect_error
);
lima_test!(
    lima_grammar_exceptions_plugin,
    "LexerAndParserErrors.GrammarExceptionsPlugin.beancount",
    expect_error
);
lima_test!(
    lima_grammar_exceptions_tag_link_pipe,
    "LexerAndParserErrors.GrammarExceptionsTagLinkPipe.beancount",
    expect_error
);
lima_test!(
    lima_grammar_exceptions_option,
    "LexerAndParserErrors.GrammarExceptionsOption.beancount",
    expect_error
);
lima_test!(
    lima_grammar_exceptions_poptag,
    "LexerAndParserErrors.GrammarExceptionsPoptag.beancount",
    expect_error
);

// Whitespace/indent error tests
lima_test!(
    lima_indent_error_0,
    "Whitespace.IndentError0.beancount",
    expect_error
);
lima_test!(
    lima_indent_error_1,
    "Whitespace.IndentError1.beancount",
    expect_error
);

// Syntax error tests
lima_test!(
    lima_syntax_error_in_posting,
    "SyntaxErrors.ErrorInPosting.beancount",
    expect_error
);
lima_test!(
    lima_syntax_error_in_transaction_line,
    "SyntaxErrors.ErrorInTransactionLine.beancount",
    expect_error
);
lima_test!(
    lima_syntax_no_final_newline,
    "SyntaxErrors.NoFinalNewline.beancount",
    expect_error
);
lima_test!(
    lima_syntax_single_error_token,
    "SyntaxErrors.SingleErrorTokenAtTopLevel.beancount",
    expect_error
);