Skip to content

Refactor/numeric backend consolidation - #247

Merged
greyblake merged 4 commits into
masterfrom
refactor/numeric-backend-consolidation
Jun 12, 2026
Merged

Refactor/numeric backend consolidation#247
greyblake merged 4 commits into
masterfrom
refactor/numeric-backend-consolidation

Conversation

@greyblake

Copy link
Copy Markdown
Owner

No description provided.

…er, float, decimal

Integer, float and decimal newtypes generated almost identical code for the
`__sanitize__` and `__validate__` functions and for the validation error enum.
The decimal backend in particular was a near-verbatim copy of the integer one.

Introduce `common/generate/numeric.rs` with the `NumericValidatorTokens` and
`NumericSanitizerTokens` traits plus shared generators
(`gen_numeric_fn_sanitize`, `gen_numeric_fn_validate`,
`gen_numeric_validation_error_type`). Each kind now implements the two small
view traits in its `models.rs` and delegates code generation. The per-kind
`generate/error.rs` files are removed.

Two incidental, behavior-preserving normalizations fall out of the merge:
- float's `__validate__` now uses `::core::result::Result` like the others
  (was `core::result::Result`, resolves to the same path);
- float's `Less` / `LessOrEqual` error messages are now correct. Float
  previously swapped them ("less than" for an inclusive bound and vice versa);
  the shared generator uses the correct wording, matching integer and decimal.
  Updated the one test that asserted the old (buggy) float message.

Verified with `cargo expand` (only the intended changes appear in the output)
and the full `test-all` matrix plus `clippy -D warnings`.
The `validate_*_guard`, `validate_validators` and `validate_sanitizers`
functions in integer/float/decimal `validate.rs` were byte-identical. Move them
into `common/validate.rs` as `validate_numeric_guard`,
`validate_numeric_validators` and `validate_numeric_sanitizers`; each kind's
guard validation is now a one-line delegation.

To make the shared functions infer cleanly, `NumericBoundValidator<T>` is
converted to use an associated `Bound` type (the bound's value type is a
function of the validator, not an independent parameter), which also simplifies
`validate_numeric_bounds`.

The per-kind derive-trait mapping (`to_*_derive_trait`,
`validate_*_derive_traits`) stays per-kind: integer/decimal differ in the
external traits they accept, and float carries its NaN-aware Eq/Ord/Hash rules.

No change to generated code: `cargo expand` output is identical, and the full
`test-all` matrix plus `clippy -D warnings` pass.
…d float

`define_integer_inner_type!` and `define_float_inner_type!` were structurally
identical, differing only in the enum name, marker trait and the list of
concrete types. Replace both with a single `define_numeric_inner_type!` macro in
`common/models.rs`. Decimal keeps its hand-written unit-struct inner type.

No change to generated code (`cargo expand` output identical); `test-all` and
`clippy -D warnings` pass.
@greyblake
greyblake merged commit 26641e9 into master Jun 12, 2026
6 checks passed
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