Refactor/numeric backend consolidation - #247
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.