rustledger-parser 0.16.0

Beancount parser with error recovery and full syntax support
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
//! Parse error types.

use crate::Span;
use std::fmt;

/// A parse error with location information.
///
/// Marked `#[non_exhaustive]` so external consumers must go through
/// [`ParseError::new`] and the builder methods rather than constructing
/// via struct literal. Future fields (e.g., a suggested fix-it span,
/// related-information back-references) then land as non-breaking
/// additions. Same `SemVer` hygiene argument as `crate::ParseResult`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseError {
    /// The kind of error.
    pub kind: ParseErrorKind,
    /// The span where the error occurred.
    pub span: Span,
    /// Optional context message.
    pub context: Option<String>,
    /// Optional hint for fixing the error.
    pub hint: Option<String>,
}

impl ParseError {
    /// Create a new parse error.
    #[must_use]
    pub const fn new(kind: ParseErrorKind, span: Span) -> Self {
        Self {
            kind,
            span,
            context: None,
            hint: None,
        }
    }

    /// Add context to this error.
    #[must_use]
    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.context = Some(context.into());
        self
    }

    /// Add a hint for fixing this error.
    #[must_use]
    pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(hint.into());
        self
    }

    /// Get the span of this error.
    #[must_use]
    pub const fn span(&self) -> (usize, usize) {
        (self.span.start, self.span.end)
    }

    /// Get a numeric code for the error kind.
    #[must_use]
    pub const fn kind_code(&self) -> u32 {
        match &self.kind {
            ParseErrorKind::UnexpectedChar(_) => 1,
            ParseErrorKind::UnexpectedEof => 2,
            ParseErrorKind::Expected(_) => 3,
            ParseErrorKind::InvalidDate(_) => 4,
            ParseErrorKind::InvalidNumber(_) => 5,
            ParseErrorKind::InvalidAccount(_) => 6,
            ParseErrorKind::InvalidCurrency(_) => 7,
            ParseErrorKind::UnclosedString => 8,
            ParseErrorKind::InvalidEscape(_) => 9,
            ParseErrorKind::MissingField(_) => 10,
            ParseErrorKind::IndentationError => 11,
            ParseErrorKind::SyntaxError(_) => 12,
            ParseErrorKind::MissingNewline => 13,
            ParseErrorKind::MissingAccount => 14,
            ParseErrorKind::InvalidDateValue(_) => 15,
            ParseErrorKind::MissingAmount => 16,
            ParseErrorKind::MissingCurrency => 17,
            ParseErrorKind::InvalidAccountFormat(_) => 18,
            ParseErrorKind::MissingDirective => 19,
            ParseErrorKind::InvalidPoptag(_) => 20,
            ParseErrorKind::UnclosedPushtag(_) => 21,
            ParseErrorKind::InvalidPopmeta(_) => 22,
            ParseErrorKind::UnclosedPushmeta(_) => 23,
            ParseErrorKind::DeprecatedPipeSymbol => 24,
            ParseErrorKind::InvalidBookingMethod(_) => 25,
            ParseErrorKind::BomInDirectiveBody => 26,
        }
    }

    /// Get the error message.
    #[must_use]
    pub fn message(&self) -> String {
        format!("{}", self.kind)
    }

    /// One sample `ParseErrorKind` per variant. Used by cross-crate
    /// sync tests that need a complete variant set to verify
    /// schema/test arrays stay in sync with the enum.
    ///
    /// **Single source of truth (round-19).** Pre-round-19 this
    /// function had a `sample()` helper with an exhaustive match
    /// PLUS a hand-maintained `vec![]` that called sample for each
    /// variant. Only the `sample()` match was a compile-time gate —
    /// a contributor adding a variant could update `sample()` and
    /// forget the vec, leaving the returned list silently
    /// incomplete (no compile error, no test failure unless a
    /// downstream sync test specifically required N entries).
    /// Round-19 collapsed both into this single vec literal. The
    /// compile-time enforcement now lives in the
    /// `every_kind_sample_covers_every_variant` test below, whose
    /// exhaustive `variant_index` match catches a missing variant
    /// at compile time and a missing vec entry at test time.
    ///
    /// Marked `#[doc(hidden)]` because this exists for test
    /// infrastructure only — the stable public API is `ParseError`
    /// + `ParseErrorKind` + `kind_code()`; this helper is a
    /// convenience for integration tests.
    #[doc(hidden)]
    #[must_use]
    pub fn every_kind_sample() -> Vec<ParseErrorKind> {
        vec![
            ParseErrorKind::UnexpectedChar('x'),
            ParseErrorKind::UnexpectedEof,
            ParseErrorKind::Expected(String::new()),
            ParseErrorKind::InvalidDate(String::new()),
            ParseErrorKind::InvalidNumber(String::new()),
            ParseErrorKind::InvalidAccount(String::new()),
            ParseErrorKind::InvalidCurrency(String::new()),
            ParseErrorKind::UnclosedString,
            ParseErrorKind::InvalidEscape('x'),
            ParseErrorKind::MissingField(String::new()),
            ParseErrorKind::IndentationError,
            ParseErrorKind::SyntaxError(String::new()),
            ParseErrorKind::MissingNewline,
            ParseErrorKind::MissingAccount,
            ParseErrorKind::InvalidDateValue(String::new()),
            ParseErrorKind::MissingAmount,
            ParseErrorKind::MissingCurrency,
            ParseErrorKind::InvalidAccountFormat(String::new()),
            ParseErrorKind::MissingDirective,
            ParseErrorKind::InvalidPoptag(String::new()),
            ParseErrorKind::UnclosedPushtag(String::new()),
            ParseErrorKind::InvalidPopmeta(String::new()),
            ParseErrorKind::UnclosedPushmeta(String::new()),
            ParseErrorKind::DeprecatedPipeSymbol,
            ParseErrorKind::InvalidBookingMethod(String::new()),
            ParseErrorKind::BomInDirectiveBody,
        ]
    }

    /// Get a short label for the error.
    #[must_use]
    pub const fn label(&self) -> &str {
        match &self.kind {
            ParseErrorKind::UnexpectedChar(_) => "unexpected character",
            ParseErrorKind::UnexpectedEof => "unexpected end of file",
            ParseErrorKind::Expected(_) => "expected different token",
            ParseErrorKind::InvalidDate(_) => "invalid date",
            ParseErrorKind::InvalidNumber(_) => "invalid number",
            ParseErrorKind::InvalidAccount(_) => "invalid account",
            ParseErrorKind::InvalidCurrency(_) => "invalid currency",
            ParseErrorKind::UnclosedString => "unclosed string",
            ParseErrorKind::InvalidEscape(_) => "invalid escape",
            ParseErrorKind::MissingField(_) => "missing field",
            ParseErrorKind::IndentationError => "indentation error",
            ParseErrorKind::SyntaxError(_) => "parse error",
            ParseErrorKind::MissingNewline => "syntax error",
            ParseErrorKind::MissingAccount => "expected account name",
            ParseErrorKind::InvalidDateValue(_) => "invalid date value",
            ParseErrorKind::MissingAmount => "expected amount",
            ParseErrorKind::MissingCurrency => "expected currency",
            ParseErrorKind::InvalidAccountFormat(_) => "invalid account format",
            ParseErrorKind::MissingDirective => "expected directive",
            ParseErrorKind::InvalidPoptag(_) => "invalid poptag",
            ParseErrorKind::UnclosedPushtag(_) => "unclosed pushtag",
            ParseErrorKind::InvalidPopmeta(_) => "invalid popmeta",
            ParseErrorKind::UnclosedPushmeta(_) => "unclosed pushmeta",
            ParseErrorKind::DeprecatedPipeSymbol => "deprecated pipe symbol",
            ParseErrorKind::InvalidBookingMethod(_) => "invalid booking method",
            ParseErrorKind::BomInDirectiveBody => "mid-file BOM",
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.kind)?;
        if let Some(ctx) = &self.context {
            write!(f, " ({ctx})")?;
        }
        Ok(())
    }
}

impl std::error::Error for ParseError {}

/// Kinds of parse errors.
///
/// Marked `#[non_exhaustive]` because new variants land routinely
/// (the most recent was `BomInDirectiveBody` — variant 26). Without
/// the attribute, every new variant would be a `SemVer`-breaking change
/// for external consumers that `match err.kind { ... }` exhaustively
/// without a wildcard arm.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseErrorKind {
    /// Unexpected character in input.
    UnexpectedChar(char),
    /// Unexpected end of file.
    UnexpectedEof,
    /// Expected a specific token.
    Expected(String),
    /// Invalid date format.
    InvalidDate(String),
    /// Invalid number format.
    InvalidNumber(String),
    /// Invalid account name.
    InvalidAccount(String),
    /// Invalid currency code.
    InvalidCurrency(String),
    /// Unclosed string literal.
    UnclosedString,
    /// Invalid escape sequence in string.
    InvalidEscape(char),
    /// Missing required field.
    MissingField(String),
    /// Indentation error.
    IndentationError,
    /// Generic syntax error.
    SyntaxError(String),
    /// Missing final newline.
    MissingNewline,
    /// Missing account name (e.g., after 'open' keyword).
    MissingAccount,
    /// Invalid date value (e.g., month 13, day 32).
    InvalidDateValue(String),
    /// Missing amount in posting.
    MissingAmount,
    /// Missing currency after number.
    MissingCurrency,
    /// Invalid account format (e.g., missing colon).
    InvalidAccountFormat(String),
    /// Missing directive after date.
    MissingDirective,
    /// Poptag for a tag that was never pushed.
    InvalidPoptag(String),
    /// Pushtag that was never popped (unclosed).
    UnclosedPushtag(String),
    /// Popmeta for a key that was never pushed.
    InvalidPopmeta(String),
    /// Pushmeta that was never popped (unclosed).
    UnclosedPushmeta(String),
    /// Deprecated pipe symbol in transaction.
    DeprecatedPipeSymbol,
    /// Invalid booking method (must be uppercase: FIFO, STRICT, `STRICT_WITH_SIZE`, LIFO, HIFO, NONE, AVERAGE).
    InvalidBookingMethod(String),
    /// UTF-8 byte-order mark detected in a directive body (mid-file
    /// BOM that survived the leading-BOM strip at the parser's
    /// public entry — typically a concatenation accident or an
    /// embedded-BOM payload).
    ///
    /// A dedicated variant rather than `SyntaxError(String)` so that:
    ///
    /// 1. The diagnostic text isn't re-stringified at every emission
    ///    site (Display renders the static message from a `&'static
    ///    str`, so no `to_string()` heap allocation per error).
    /// 2. External consumers get a structural discriminant to match
    ///    on instead of a brittle string-equality compare against a
    ///    message body that might be reworded.
    /// 3. The message text becomes an implementation detail (it
    ///    lives in this enum's Display impl) rather than a public
    ///    `&'static str` constant whose wording would be a `SemVer`
    ///    stability commitment.
    BomInDirectiveBody,
}

impl fmt::Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnexpectedChar(c) => write!(f, "syntax error: unexpected '{c}'"),
            Self::UnexpectedEof => write!(f, "unexpected end of file"),
            Self::Expected(what) => write!(f, "expected {what}"),
            Self::InvalidDate(s) => write!(f, "invalid date '{s}'"),
            Self::InvalidNumber(s) => write!(f, "invalid number '{s}'"),
            Self::InvalidAccount(s) => write!(f, "Invalid account '{s}'"),
            Self::InvalidCurrency(s) => write!(f, "invalid currency '{s}'"),
            Self::UnclosedString => write!(f, "unclosed string literal"),
            Self::InvalidEscape(c) => write!(f, "invalid escape sequence '\\{c}'"),
            Self::MissingField(field) => write!(f, "missing required field: {field}"),
            Self::IndentationError => write!(f, "indentation error"),
            Self::SyntaxError(msg) => write!(f, "parse error: {msg}"),
            Self::MissingNewline => write!(f, "syntax error: missing final newline"),
            Self::MissingAccount => write!(f, "expected account name"),
            Self::InvalidDateValue(msg) => write!(f, "invalid date: {msg}"),
            Self::MissingAmount => write!(f, "expected amount in posting"),
            Self::MissingCurrency => write!(f, "expected currency after number"),
            Self::InvalidAccountFormat(s) => {
                write!(f, "invalid account '{s}': must contain ':'")
            }
            Self::MissingDirective => write!(f, "expected directive after date"),
            Self::InvalidPoptag(tag) => {
                write!(f, "poptag attempted on tag '{tag}' which was never pushed")
            }
            Self::UnclosedPushtag(tag) => {
                write!(f, "pushtag '{tag}' was never popped")
            }
            Self::InvalidPopmeta(key) => {
                write!(f, "popmeta attempted on key '{key}' which was never pushed")
            }
            Self::UnclosedPushmeta(key) => {
                write!(f, "pushmeta '{key}' was never popped")
            }
            Self::DeprecatedPipeSymbol => {
                write!(f, "Pipe symbol is deprecated")
            }
            Self::InvalidBookingMethod(m) => {
                write!(
                    f,
                    "invalid booking method '{m}': must be one of FIFO, STRICT, STRICT_WITH_SIZE, LIFO, HIFO, NONE, AVERAGE"
                )
            }
            Self::BomInDirectiveBody => f.write_str(BOM_MIDFILE_DIAGNOSTIC),
        }
    }
}

/// Mid-file BOM diagnostic message.
///
/// Private — emission sites construct `ParseErrorKind::BomInDirectiveBody`
/// and the Display impl renders this text. External consumers detect
/// the diagnostic structurally via the enum variant, not by string
/// compare against this constant. Reworking the wording is therefore
/// not a `SemVer` break.
///
/// Spelled via `concat!` rather than a `\<newline>` line-continuation
/// literal so editor 'strip trailing whitespace on save' and similar
/// hooks can't collapse word boundaries inside the string.
const BOM_MIDFILE_DIAGNOSTIC: &str = concat!(
    "Invalid token: UTF-8 BOM detected in directive body ",
    "(only a leading BOM is permitted); ",
    "did you concatenate two BOM-prefixed files or paste content with an embedded BOM?",
);

#[cfg(test)]
mod tests {
    use super::*;

    /// Compile-time + test-time gate for `every_kind_sample`'s
    /// completeness. The `variant_index` match is exhaustive over
    /// `ParseErrorKind` — adding a new variant breaks compilation
    /// until an arm is added with a unique index. The runtime
    /// assertion below then catches the case where a contributor
    /// added the variant + the `variant_index` arm but forgot to
    /// add a constructor to `every_kind_sample`'s vec.
    ///
    /// Two compile gates working together:
    ///   1. `variant_index` match exhaustiveness — catches added
    ///      variants at compile time.
    ///   2. The unique-index + max-index assertions — catches a
    ///      missing or duplicate `every_kind_sample` vec entry at
    ///      test time.
    #[test]
    fn every_kind_sample_covers_every_variant() {
        // Assign each variant a unique sequential index. The match
        // is exhaustive — a new variant breaks compile until an
        // arm is added with the next index.
        fn variant_index(k: &ParseErrorKind) -> u32 {
            match k {
                ParseErrorKind::UnexpectedChar(_) => 0,
                ParseErrorKind::UnexpectedEof => 1,
                ParseErrorKind::Expected(_) => 2,
                ParseErrorKind::InvalidDate(_) => 3,
                ParseErrorKind::InvalidNumber(_) => 4,
                ParseErrorKind::InvalidAccount(_) => 5,
                ParseErrorKind::InvalidCurrency(_) => 6,
                ParseErrorKind::UnclosedString => 7,
                ParseErrorKind::InvalidEscape(_) => 8,
                ParseErrorKind::MissingField(_) => 9,
                ParseErrorKind::IndentationError => 10,
                ParseErrorKind::SyntaxError(_) => 11,
                ParseErrorKind::MissingNewline => 12,
                ParseErrorKind::MissingAccount => 13,
                ParseErrorKind::InvalidDateValue(_) => 14,
                ParseErrorKind::MissingAmount => 15,
                ParseErrorKind::MissingCurrency => 16,
                ParseErrorKind::InvalidAccountFormat(_) => 17,
                ParseErrorKind::MissingDirective => 18,
                ParseErrorKind::InvalidPoptag(_) => 19,
                ParseErrorKind::UnclosedPushtag(_) => 20,
                ParseErrorKind::InvalidPopmeta(_) => 21,
                ParseErrorKind::UnclosedPushmeta(_) => 22,
                ParseErrorKind::DeprecatedPipeSymbol => 23,
                ParseErrorKind::InvalidBookingMethod(_) => 24,
                ParseErrorKind::BomInDirectiveBody => 25,
            }
        }
        let samples = ParseError::every_kind_sample();
        let indices: std::collections::BTreeSet<u32> = samples.iter().map(variant_index).collect();
        assert_eq!(
            indices.len(),
            samples.len(),
            "every_kind_sample has duplicate variants (collapsed by variant_index): \
             samples = {samples:?}, unique indices = {indices:?}"
        );
        let max = indices.iter().max().copied().unwrap_or(0);
        assert_eq!(
            samples.len() as u32,
            max + 1,
            "every_kind_sample is missing variants: highest variant_index = {max}, \
             expected {} entries in the vec, got {}. Add the missing constructor \
             to every_kind_sample's vec.",
            max + 1,
            samples.len()
        );
    }

    #[test]
    fn test_parse_error_new() {
        let err = ParseError::new(ParseErrorKind::UnexpectedEof, Span::new(0, 5));
        assert_eq!(err.span(), (0, 5));
        assert!(err.context.is_none());
        assert!(err.hint.is_none());
    }

    #[test]
    fn test_parse_error_with_context() {
        let err = ParseError::new(ParseErrorKind::UnexpectedEof, Span::new(0, 5))
            .with_context("in transaction");
        assert_eq!(err.context, Some("in transaction".to_string()));
    }

    #[test]
    fn test_parse_error_with_hint() {
        let err = ParseError::new(ParseErrorKind::UnexpectedEof, Span::new(0, 5))
            .with_hint("add more input");
        assert_eq!(err.hint, Some("add more input".to_string()));
    }

    #[test]
    fn test_parse_error_display_with_context() {
        let err = ParseError::new(ParseErrorKind::UnexpectedEof, Span::new(0, 5))
            .with_context("parsing header");
        let display = format!("{err}");
        assert!(display.contains("unexpected end of file"));
        assert!(display.contains("parsing header"));
    }

    #[test]
    fn test_kind_codes() {
        // Test all error codes are unique and in expected range
        let kinds = [
            (ParseErrorKind::UnexpectedChar('x'), 1),
            (ParseErrorKind::UnexpectedEof, 2),
            (ParseErrorKind::Expected("foo".to_string()), 3),
            (ParseErrorKind::InvalidDate("bad".to_string()), 4),
            (ParseErrorKind::InvalidNumber("nan".to_string()), 5),
            (ParseErrorKind::InvalidAccount("bad".to_string()), 6),
            (ParseErrorKind::InvalidCurrency("???".to_string()), 7),
            (ParseErrorKind::UnclosedString, 8),
            (ParseErrorKind::InvalidEscape('n'), 9),
            (ParseErrorKind::MissingField("name".to_string()), 10),
            (ParseErrorKind::IndentationError, 11),
            (ParseErrorKind::SyntaxError("oops".to_string()), 12),
            (ParseErrorKind::MissingNewline, 13),
            (ParseErrorKind::MissingAccount, 14),
            (ParseErrorKind::InvalidDateValue("month 13".to_string()), 15),
            (ParseErrorKind::MissingAmount, 16),
            (ParseErrorKind::MissingCurrency, 17),
            (
                ParseErrorKind::InvalidAccountFormat("Assets".to_string()),
                18,
            ),
            (ParseErrorKind::MissingDirective, 19),
            (ParseErrorKind::InvalidPoptag("bad".to_string()), 20),
            (ParseErrorKind::UnclosedPushtag("tag".to_string()), 21),
            (ParseErrorKind::InvalidPopmeta("key".to_string()), 22),
            (ParseErrorKind::UnclosedPushmeta("key".to_string()), 23),
            (ParseErrorKind::DeprecatedPipeSymbol, 24),
            (ParseErrorKind::InvalidBookingMethod("BAD".to_string()), 25),
            (ParseErrorKind::BomInDirectiveBody, 26),
        ];

        for (kind, expected_code) in kinds {
            let err = ParseError::new(kind, Span::new(0, 1));
            assert_eq!(err.kind_code(), expected_code);
        }
    }

    #[test]
    fn test_error_labels() {
        // Test that all error kinds have non-empty labels
        let kinds = [
            ParseErrorKind::UnexpectedChar('x'),
            ParseErrorKind::UnexpectedEof,
            ParseErrorKind::Expected("foo".to_string()),
            ParseErrorKind::InvalidDate("bad".to_string()),
            ParseErrorKind::InvalidNumber("nan".to_string()),
            ParseErrorKind::InvalidAccount("bad".to_string()),
            ParseErrorKind::InvalidCurrency("???".to_string()),
            ParseErrorKind::UnclosedString,
            ParseErrorKind::InvalidEscape('n'),
            ParseErrorKind::MissingField("name".to_string()),
            ParseErrorKind::IndentationError,
            ParseErrorKind::SyntaxError("oops".to_string()),
            ParseErrorKind::MissingNewline,
            ParseErrorKind::MissingAccount,
            ParseErrorKind::InvalidDateValue("month 13".to_string()),
            ParseErrorKind::MissingAmount,
            ParseErrorKind::MissingCurrency,
            ParseErrorKind::InvalidAccountFormat("Assets".to_string()),
            ParseErrorKind::MissingDirective,
            ParseErrorKind::InvalidPoptag("bad".to_string()),
            ParseErrorKind::UnclosedPushtag("tag".to_string()),
            ParseErrorKind::InvalidPopmeta("key".to_string()),
            ParseErrorKind::UnclosedPushmeta("key".to_string()),
            ParseErrorKind::DeprecatedPipeSymbol,
            ParseErrorKind::InvalidBookingMethod("BAD".to_string()),
            ParseErrorKind::BomInDirectiveBody,
        ];

        for kind in kinds {
            let err = ParseError::new(kind, Span::new(0, 1));
            assert!(!err.label().is_empty());
        }
    }

    #[test]
    fn test_error_messages() {
        // Test Display for all error kinds
        let test_cases = [
            (ParseErrorKind::UnexpectedChar('$'), "unexpected '$'"),
            (ParseErrorKind::UnexpectedEof, "unexpected end of file"),
            (
                ParseErrorKind::Expected("number".to_string()),
                "expected number",
            ),
            (
                ParseErrorKind::InvalidDate("2024-13-01".to_string()),
                "invalid date '2024-13-01'",
            ),
            (
                ParseErrorKind::InvalidNumber("abc".to_string()),
                "invalid number 'abc'",
            ),
            (
                ParseErrorKind::InvalidAccount("bad".to_string()),
                "Invalid account 'bad'",
            ),
            (
                ParseErrorKind::InvalidCurrency("???".to_string()),
                "invalid currency '???'",
            ),
            (ParseErrorKind::UnclosedString, "unclosed string literal"),
            (
                ParseErrorKind::InvalidEscape('x'),
                "invalid escape sequence '\\x'",
            ),
            (
                ParseErrorKind::MissingField("date".to_string()),
                "missing required field: date",
            ),
            (ParseErrorKind::IndentationError, "indentation error"),
            (
                ParseErrorKind::SyntaxError("bad token".to_string()),
                "parse error: bad token",
            ),
            (ParseErrorKind::MissingNewline, "missing final newline"),
            (ParseErrorKind::MissingAccount, "expected account name"),
            (
                ParseErrorKind::InvalidDateValue("month 13".to_string()),
                "invalid date: month 13",
            ),
            (ParseErrorKind::MissingAmount, "expected amount in posting"),
            (
                ParseErrorKind::MissingCurrency,
                "expected currency after number",
            ),
            (
                ParseErrorKind::InvalidAccountFormat("Assets".to_string()),
                "must contain ':'",
            ),
            (
                ParseErrorKind::MissingDirective,
                "expected directive after date",
            ),
            (
                ParseErrorKind::InvalidPoptag("bad".to_string()),
                "poptag attempted on tag 'bad'",
            ),
            (
                ParseErrorKind::UnclosedPushtag("tag".to_string()),
                "pushtag 'tag' was never popped",
            ),
            (
                ParseErrorKind::InvalidPopmeta("key".to_string()),
                "popmeta attempted on key 'key'",
            ),
            (
                ParseErrorKind::UnclosedPushmeta("key".to_string()),
                "pushmeta 'key' was never popped",
            ),
            (
                ParseErrorKind::DeprecatedPipeSymbol,
                "Pipe symbol is deprecated",
            ),
            (
                ParseErrorKind::InvalidBookingMethod("BAD".to_string()),
                "invalid booking method 'BAD'",
            ),
            // BOM diagnostic — Display renders BOM_MIDFILE_DIAGNOSTIC.
            // Assert on the salient substrings rather than the full text
            // so a future copyedit of the message body doesn't have to
            // touch this test, but a regression that drops the BOM
            // mention entirely (e.g., Display returning "") would fail.
            (
                ParseErrorKind::BomInDirectiveBody,
                "UTF-8 BOM detected in directive body",
            ),
        ];

        for (kind, expected_substring) in test_cases {
            let msg = format!("{kind}");
            assert!(
                msg.contains(expected_substring),
                "Expected '{expected_substring}' in '{msg}'"
            );
        }
    }

    #[test]
    fn test_parse_error_is_error_trait() {
        let err = ParseError::new(ParseErrorKind::UnexpectedEof, Span::new(0, 1));
        // Verify it implements std::error::Error
        let _: &dyn std::error::Error = &err;
    }
}