Skip to content

Commit 0db525f

Browse files
authored
Fix core dump parsing transaction entry with only comment (#26)
1 parent 646cc70 commit 0db525f

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2025-05-20 - [0.2.8]
4+
5+
### Bug fixes
6+
7+
- Fix core dump parsing transaction entry with only comment.
8+
39
## 2025-05-19 - [0.2.7]
410

511
### Bug fixes
@@ -102,6 +108,7 @@
102108

103109
First beta release
104110

111+
[0.2.8]: https://github.com/mondeja/hledger-fmt/compare/v0.2.7...v0.2.8
105112
[0.2.7]: https://github.com/mondeja/hledger-fmt/compare/v0.2.6...v0.2.7
106113
[0.2.6]: https://github.com/mondeja/hledger-fmt/compare/v0.2.5...v0.2.6
107114
[0.2.5]: https://github.com/mondeja/hledger-fmt/compare/v0.2.4...v0.2.5

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 = "hledger-fmt"
3-
version = "0.2.7"
3+
version = "0.2.8"
44
rust-version = "1.74.1"
55
edition = "2021"
66
description = "An opinionated hledger's journal files formatter."

src/formatter/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,15 @@ fn lots() {
490490
"#,
491491
);
492492
}
493+
494+
#[test]
495+
fn issue25() {
496+
assert_format(
497+
r#"1/1/1 * transaction
498+
; vacation $2350 hawaii flight
499+
"#,
500+
r#"1/1/1 * transaction
501+
; vacation $2350 hawaii flight
502+
"#,
503+
);
504+
}

src/parser/mod.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
367367
}
368368
}
369369
} else {
370-
// inside transaction entry
370+
// maybe inside transaction entry
371371
let mut at_indent = c != '\t';
372372
let mut indent = if at_indent { 1 } else { 4 };
373373
let mut entry_name = String::with_capacity(64);
@@ -381,7 +381,7 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
381381
indent += 1;
382382
} else if c == ';' || c == '#' {
383383
// transaction entry with empty value
384-
let comment = parse_inline_comment(
384+
let maybe_comment = parse_inline_comment(
385385
&mut chars_iter,
386386
lineno,
387387
coln + 1,
@@ -391,7 +391,7 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
391391
CommentPrefix::Semicolon
392392
}),
393393
);
394-
if comment.is_some() {
394+
if let Some(comment) = maybe_comment {
395395
is_comment_only = true;
396396
// if the first comment is indented with >=2 and first entry indent
397397
// is not setted, set it
@@ -400,11 +400,8 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
400400
if indent >= 2 && data.first_entry_indent == 0 {
401401
data.first_entry_indent = indent;
402402
}
403-
data.transaction_entries.push(
404-
TransactionNode::SingleLineComment(
405-
comment.unwrap(),
406-
),
407-
);
403+
data.transaction_entries
404+
.push(TransactionNode::SingleLineComment(comment));
408405
}
409406
break;
410407
} else {
@@ -422,6 +419,23 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
422419
prev_was_whitespace = true;
423420
} else {
424421
prev_was_whitespace = false;
422+
423+
if c == ';' && entry_name.is_empty() {
424+
// inside comment
425+
let maybe_comment = parse_inline_comment(
426+
&mut chars_iter,
427+
lineno,
428+
coln + 1,
429+
Some(CommentPrefix::Semicolon),
430+
);
431+
if let Some(comment) = maybe_comment {
432+
is_comment_only = true;
433+
data.transaction_entries.push(
434+
TransactionNode::SingleLineComment(comment),
435+
);
436+
}
437+
break;
438+
}
425439
}
426440
entry_name.push(c);
427441
}

0 commit comments

Comments
 (0)