feat(models): add LineNumber newtype to replace raw usize for start_line/end_line#596
Merged
feat(models): add LineNumber newtype to replace raw usize for start_line/end_line#596
Conversation
ec871c8 to
d635146
Compare
d635146 to
b30a5e1
Compare
8f6f942 to
571938e
Compare
…ine/end_line
Introduce LineNumber (backed by NonZeroUsize) and LineSpan newtypes to
replace raw usize fields for start_line/end_line across all output,
detection, and internal types. This eliminates the possibility of
invalid zero-valued line numbers at the type level, removes runtime
guards that checked for start_line > 0, and makes the 1-indexed
semantics explicit in the type system.
Key changes:
- New src/models/line_number.rs with LineNumber(NonZeroUsize) and
LineSpan { start, end } types
- All output structs (Match, Copyright, Holder, Author, OutputEmail,
OutputURL) now use LineNumber for start_line/end_line
- All internal detection types (CopyrightDetection, HolderDetection,
AuthorDetection, Token, EmailDetection, UrlDetection, LicenseMatch,
FileRegion, DeclaredLicenseMatchMetadata) migrated
- SerializableLicenseMatch preserves JSON backward compatibility via
serde(transparent) on LineNumber
- Removed now-unnecessary start_line > 0 / end_line > 0 guards
- line_for_pos() returns Option<LineNumber> instead of Option<usize>
- matched_text_from_text() takes (LineNumber, LineNumber) params
- ranges_overlap() takes LineNumber params (Ord-based comparison)
- LineSpan defined but not yet adopted (for future use)
571938e to
ad4e59d
Compare
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
LineNumber(backed byNonZeroUsize) andLineSpannewtypes to replace rawusizefields forstart_line/end_lineacross the entire codebasestart_line > 0Key changes
src/models/line_number.rswithLineNumber(NonZeroUsize)andLineSpan { start, end }typesMatch,Copyright,Holder,Author,OutputEmail,OutputURL) now useLineNumberforstart_line/end_lineCopyrightDetection,HolderDetection,AuthorDetection,Token,EmailDetection,UrlDetection,LicenseMatch,FileRegion,DeclaredLicenseMatchMetadata) migratedSerializableLicenseMatchpreserves JSON backward compatibility viaserde(transparent)onLineNumberstart_line > 0/end_line > 0runtime guardsline_for_pos()returnsOption<LineNumber>instead ofOption<usize>matched_text_from_text()takes(LineNumber, LineNumber)paramsranges_overlap()takesLineNumberparams (Ord-based comparison)LineSpandefined but not yet adopted (for future use)LicenseMatch::default()usesLineNumber::ONEinstead of0Performance
Neutral —
NonZeroUsizehas the same layout asusize, andOption<LineNumber>benefits from niche optimization (same size asOption<usize>).Testing
cargo build— 0 errorscargo fmt— cleancargo clippy --all-targets --all-features— 0 errorsHow I tested
Ran targeted test suites for copyright detection, license detection models, finder, and debian parser modules. All pass except one pre-existing failure in
test_normalize_company_suffix_period_holder_variants.