A grouping pattern must be static, so none can be built at run time #38

Closed
opened 2026-08-16 00:35:30 +00:00 by rosa · 1 comment
Owner

What to build

Format::grouping takes a &'static [u8], so a grouping pattern can only be
written into the program. One read from a configuration file or chosen at run
time cannot be used.

The same lifetime is why Format has no serde impls, while RoundingMode — a
type that only ever appears inside a Format — has them. A caller can save the
rounding mode from their formatting options but not the options themselves.

Store the pattern inline so Format owns everything it holds, then give it
Serialize and Deserialize under the optional serde feature.

Two shapes to choose between before building. A fixed-size array with a
length keeps the current generality. A pair of numbers — the size nearest the
decimal mark, then the size that repeats — covers every convention the crate
documents, including the Indian grouping, and is simpler to read and to
serialize. Pick one.

This is a breaking change to a public method.

Acceptance criteria

  • The shape is chosen and the reasoning recorded
  • A grouping pattern can be built at run time
  • Format no longer borrows, and Format::new and the builder methods stay const where they are today
  • Format serializes and deserializes under the optional serde feature
  • Western and Indian grouping both still render as their tests expect
  • The breaking change is noted for the next release
  • mise run ci passes

Blocked by

None — can start immediately.

## What to build `Format::grouping` takes a `&'static [u8]`, so a grouping pattern can only be written into the program. One read from a configuration file or chosen at run time cannot be used. The same lifetime is why `Format` has no serde impls, while `RoundingMode` — a type that only ever appears inside a `Format` — has them. A caller can save the rounding mode from their formatting options but not the options themselves. Store the pattern inline so `Format` owns everything it holds, then give it `Serialize` and `Deserialize` under the optional `serde` feature. **Two shapes to choose between before building.** A fixed-size array with a length keeps the current generality. A pair of numbers — the size nearest the decimal mark, then the size that repeats — covers every convention the crate documents, including the Indian grouping, and is simpler to read and to serialize. Pick one. This is a breaking change to a public method. ## Acceptance criteria - [ ] The shape is chosen and the reasoning recorded - [ ] A grouping pattern can be built at run time - [ ] `Format` no longer borrows, and `Format::new` and the builder methods stay `const` where they are today - [ ] `Format` serializes and deserializes under the optional `serde` feature - [ ] Western and Indian grouping both still render as their tests expect - [ ] The breaking change is noted for the next release - [ ] `mise run ci` passes ## Blocked by None — can start immediately.
Author
Owner

Shape chosen: a pair of numbers

Format now stores first and repeat — the group size next to the decimal
mark, then the size that repeats — instead of &'static [u8].

Format::grouping(&[3, 2]) becomes Format::grouping(3, 2), and
Format::grouping(&[3]) becomes Format::grouping(3, 3).

Why the pair over a fixed-size array with a length:

  • No convention the crate documents needs a third size. Western is (3, 3)
    and Indian is (3, 2). An array would carry a capacity chosen for cases
    nobody has.
  • The array keeps two rules a reader has to learn — sizes run from the mark
    leftward, and the last entry repeats. The pair states the repetition in
    the parameter's name instead.
  • It serializes as two numbers, {"first": 3, "repeat": 2}, rather than a
    list whose length carries meaning. Nothing has to reject a list that is
    longer than the capacity, so a whole class of read error stops existing.
  • Format stays Copy and every builder method stays const.

The zero cases carry over unchanged: a first of zero leaves every digit in
one run (that is what no_grouping sets), and a repeat of zero leaves the
digits past the first group in one run.

Also in this change

Format gets Serialize and Deserialize under the serde feature. One
field per option; a field the document leaves out keeps the value
Format::new starts with, so a configuration file need only state what
differs. position, spaced, and precision are left out when unset rather
than written as null, so TOML can hold the result.

{
  "identifier": "symbol",
  "position": "prefix",
  "spaced": false,
  "negative": "parentheses",
  "precision": { "digits": 2, "rounding": "half-up" },
  "grouping": { "first": 3, "repeat": 2 },
  "group_separator": ",",
  "decimal_separator": "."
}
## Shape chosen: a pair of numbers `Format` now stores `first` and `repeat` — the group size next to the decimal mark, then the size that repeats — instead of `&'static [u8]`. `Format::grouping(&[3, 2])` becomes `Format::grouping(3, 2)`, and `Format::grouping(&[3])` becomes `Format::grouping(3, 3)`. Why the pair over a fixed-size array with a length: - No convention the crate documents needs a third size. Western is `(3, 3)` and Indian is `(3, 2)`. An array would carry a capacity chosen for cases nobody has. - The array keeps two rules a reader has to learn — sizes run from the mark leftward, and *the last entry repeats*. The pair states the repetition in the parameter's name instead. - It serializes as two numbers, `{"first": 3, "repeat": 2}`, rather than a list whose length carries meaning. Nothing has to reject a list that is longer than the capacity, so a whole class of read error stops existing. - `Format` stays `Copy` and every builder method stays `const`. The zero cases carry over unchanged: a `first` of zero leaves every digit in one run (that is what `no_grouping` sets), and a `repeat` of zero leaves the digits past the first group in one run. ## Also in this change `Format` gets `Serialize` and `Deserialize` under the `serde` feature. One field per option; a field the document leaves out keeps the value `Format::new` starts with, so a configuration file need only state what differs. `position`, `spaced`, and `precision` are left out when unset rather than written as null, so TOML can hold the result. ```json { "identifier": "symbol", "position": "prefix", "spaced": false, "negative": "parentheses", "precision": { "digits": 2, "rounding": "half-up" }, "grouping": { "first": 3, "repeat": 2 }, "group_separator": ",", "decimal_separator": "." } ```
rosa closed this issue 2026-08-16 15:05:18 +00:00
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rosa/lucre#38
No description provided.