refactor: tighten Rust idioms (saturating casts, parse_tag split, or-named helper)#11
Merged
Conversation
Replaces TS-brain reflexes with the conventional Rust shape: - Saturating conversions (~17 sites) → `SourcePosition::offset_usize()` helper or `.expect()`. `MAX_INPUT_BYTES` is checked at parse entry so every u32↔usize round-trip is infallible; saturating silently to `usize::MAX` was hiding bugs instead of letting them surface. - `parse_tag` (145-line monolith with `#[allow(too_many_lines)]`) → thin dispatcher over `parse_end_tag` and `parse_opening_tag`, with attribute-list parsing extracted into its own function returning a named `AttributeList` struct. Drops the allow and the comment churn. - `skip_comment_or_cdata` → `try_skip_comment` + `try_skip_cdata`, sharing a `scan_to_terminator` primitive. Eliminates the multi-purpose `or`-named helper. - Tokenizer attribute-dedup logic is now two single-purpose helpers (`seen_attribute` + `record_seen_attr`) instead of one fused block. - `children.clone().next()` peek (TextSegments) → `as_slice().first()`. Same Big-O, reads as "peek" instead of "clone iterator". - Qualified `std::collections::HashSet<…>` paths in mutate.rs → single `use` import.
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.
Summary
unwrap_or(usize::MAX)/unwrap_or(u32::MAX)pattern (17 sites) withSourcePosition::offset_usize()or.expect().MAX_INPUT_BYTESis gated at parse entry, so the conversions are infallible — silently saturating was hiding bugs.parse_tag(previously#[allow(clippy::too_many_lines)]) intoparse_end_tag+parse_opening_tag+parse_attribute_list, returning a namedAttributeListstruct. Drops the allow.or-namedskip_comment_or_cdataintotry_skip_comment+try_skip_cdataover a sharedscan_to_terminator.children.clone().next()peek →as_slice().first(); qualifiedstd::collections::HashSet<…>paths inmutate.rs→ singleuseimport.Test plan
cargo fmt --all -- --checkcargo clippy --all-targets --all-features -- -D warningscargo test --all-features --workspace(198 tests pass)bindings/node:pnpm build:debug && pnpm test(23 pass)