Skip to content

Commit f6d916e

Browse files
authored
Fix subtraction with overflow in some assertions (#14)
1 parent 538f7b3 commit f6d916e

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/rbubley/mirrors-prettier
3-
rev: v3.3.3
3+
rev: v3.4.2
44
hooks:
55
- id: prettier
66
args:
@@ -14,14 +14,14 @@ repos:
1414
- id: check-useless-excludes
1515
name: check-useless-excludes
1616
- repo: https://github.com/pre-commit/pre-commit-hooks
17-
rev: v4.6.0
17+
rev: v5.0.0
1818
hooks:
1919
- id: trailing-whitespace
2020
name: trailing-whitespace
2121
- id: end-of-file-fixer
2222
name: end-of-file-fixer
2323
- repo: https://github.com/DavidAnson/markdownlint-cli2
24-
rev: v0.13.0
24+
rev: v0.17.1
2525
hooks:
2626
- id: markdownlint-cli2
2727
exclude: ^LICENSE$
@@ -34,12 +34,8 @@ repos:
3434
name: clippy-no-features
3535
args:
3636
[
37-
--exclude=leptos-fluent-ssr-hydrate-axum-example,
38-
--workspace,
3937
--,
4038
-D,
41-
warnings,
42-
-D,
4339
clippy::perf,
4440
-D,
4541
clippy::print_stdout,
@@ -51,7 +47,7 @@ repos:
5147
clippy::semicolon_if_nothing_returned,
5248
]
5349
- repo: https://github.com/mondeja/rust-pc-hooks
54-
rev: v1.2.0
50+
rev: v1.3.0
5551
hooks:
5652
- id: cargo-machete
5753
args:

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# CHANGELOG
22

3+
## 2025-01-13 - [0.2.2]
4+
5+
### Bug fixes
6+
7+
- Fix substraction with overflow formatting some assertions.
8+
39
## 2024-11-13 - [0.2.1]
410

511
### Bug fixes
612

7-
- Set exitcode 2 when CLI detects possible changes for files.
13+
- Set exitcode 2 when CLI detects possible changes for files.
814

915
## 2024-10-18 - [0.2.0]
1016

@@ -60,6 +66,7 @@
6066

6167
First beta release
6268

69+
[0.2.2]: https://github.com/mondeja/hledger-fmt/compare/v0.2.1...v0.2.2
6370
[0.2.1]: https://github.com/mondeja/hledger-fmt/compare/v0.2.0...v0.2.1
6471
[0.2.0]: https://github.com/mondeja/hledger-fmt/compare/v0.1.4...v0.2.0
6572
[0.1.4]: https://github.com/mondeja/hledger-fmt/compare/v0.1.3...v0.1.4

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.1"
3+
version = "0.2.2"
44
edition = "2021"
55
description = "An opinionated hledger's journal files formatter."
66
repository = "https://github.com/mondeja/hledger-fmt"

src/formatter/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,17 @@ pub fn format_content(nodes: &JournalFile) -> String {
187187
value_second_part_units,
188188
)
189189
} else {
190-
2 + max_entry_value_first_part_commodity_trailing_len
191-
- value_first_part_decimal.chars().count()
192-
- trailing_commodity_len_from_units(
190+
let pos =
191+
2 + max_entry_value_first_part_commodity_trailing_len;
192+
let neg = value_first_part_decimal.chars().count()
193+
+ trailing_commodity_len_from_units(
193194
value_first_part_units,
194-
)
195+
);
196+
if pos > neg {
197+
pos - neg
198+
} else {
199+
2
200+
}
195201
})
196202
} else {
197203
"".to_string()

src/formatter/tests.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,19 @@ fn complex_transaction() {
460460
"#,
461461
);
462462
}
463+
464+
#[test]
465+
fn issue_13() {
466+
assert_format(
467+
r#"
468+
2022-01-01 SHELL OIL
469+
asset:checking $-8.42 = $11373.17
470+
expense:transport:gas $8.42
471+
"#,
472+
r#"
473+
2022-01-01 SHELL OIL
474+
asset:checking $-8.42 = $11373.17
475+
expense:transport:gas $8.42
476+
"#,
477+
);
478+
}

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
278278
state = ParserState::MultilineComment;
279279
data.multiline_comment_start_lineno = lineno + 1;
280280
data.multiline_comment_content = String::new();
281-
} else if colno == 0 && line.chars().map(char::is_whitespace).all(|x| x) {
281+
} else if colno == 0 && line.chars().all(char::is_whitespace) {
282282
process_empty_line(lineno + 1, &mut journal, &mut data);
283283
} else if colno == 0
284284
&& (line.starts_with("account ")

0 commit comments

Comments
 (0)