Skip to content

feat: support parser rounding modes - #24

Open
tisonkun wants to merge 6 commits into
mainfrom
codex/define-parse-rounding
Open

feat: support parser rounding modes#24
tisonkun wants to merge 6 commits into
mainfrom
codex/define-parse-rounding

Conversation

@tisonkun

@tisonkun tisonkun commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a public RoundMode with five non-redundant modes: Ceil, Floor, HalfCeil, HalfFloor, and HalfEven.
  • Add a non-exhaustive ParseOptions with a public round_mode field and HalfCeil as its default.
  • Add ByteSize::parse_with(input, options); FromStr delegates to it with ParseOptions::default(), preserving the existing ties-to-larger behavior for standard and Serde string parsing.
  • Apply the unit multiplier first, then round exactly once to a whole byte without floating point; preserve enough discarded-digit state to distinguish exact ties from values just above a tie.
  • Cover every supported mode with boundary tests, decimal and binary unit tests, overflow tests, and QuickCheck comparisons against exact u128 arithmetic.
  • Document the contract in the API overview, README, and changelog.
  • Verify with cargo x test, cargo x lint, and cargo x bench.

Design Notes

The rounding semantics are adapted from Jiff’s RoundMode, but the API does not copy all nine variants. ByteSize only accepts non-negative values, making Expand equivalent to Ceil, Trunc equivalent to Floor, HalfExpand equivalent to HalfCeil, and HalfTrunc equivalent to HalfFloor. The public enum keeps one direction-based name for each distinct behavior relevant to this domain.

The parsing API follows the cronexpr ParseOptions pattern: configurable behavior lives in a defaultable, non-exhaustive options type, while the ordinary parsing path delegates to the configurable entry point with default options. This gives future parser settings a compatible place to compose without adding more specialized parsing methods.

Fraction parsing always records whether any digit after the first discarded digit is nonzero. This removes both const-generic specialization and a per-digit mode branch; A/B benchmarks showed no material regression.

@tisonkun tisonkun changed the title docs: define parser rounding contract feat: support parser rounding modes Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant