refactor(evaluator): reorganize types module into submodules#156
Conversation
This commit restructures the `types` module by splitting it into focused submodules for numeric and string handling. The main `types.rs` file has been removed, and its functionality has been distributed across `numeric.rs`, `string.rs`, and a new `mod.rs` that serves as the public API surface. This change enhances code organization and maintainability while preserving existing functionality. - Removed `types.rs` and migrated its content to `numeric.rs` and `string.rs`. - Introduced a new `mod.rs` to expose the public API and manage submodule imports. - Updated error handling and type reading functions to ensure consistency across the new structure. No public API changes have been made, and all existing tests have been updated accordingly to reflect the new module organization. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
|
Caution Review failedPull request was closed or merged during review Summary by CodeRabbitRelease Notes
WalkthroughThe monolithic Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
…pes-directory-module
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 CI must passWonderful, this rule succeeded.All CI checks must pass. Release-plz PRs are exempt because they only bump versions and changelogs (code was already tested on main), and GITHUB_TOKEN-triggered force-pushes suppress CI.
🟢 Do not merge outdated PRsWonderful, this rule succeeded.Make sure PRs are within 10 commits of the base branch before merging
|
|
Related Documentation 1 document(s) may need updating based on files changed in this PR: libMagic-rs architecture
|
There was a problem hiding this comment.
Stale comment
Automated risk assessment for this synchronize update:
- Risk level: Low
- Why: The diff is a structural refactor of
src/evaluator/types.rsintosrc/evaluator/types/{mod,numeric,string,tests}.rs. Public API and core type-reading/coercion logic are preserved, and no new production codepaths outside the evaluator types module were introduced.- Evidence checked: file-level diff, current review state, and direct comparison of key functions (
read_byte,read_short,read_long,read_quad,read_string,read_typed_value,coerce_value_to_type) showing no behavioral logic changes.- Reviewer assignment: Not required for Low risk under policy.
Approving based on low-risk mechanical refactor characteristics.
There was a problem hiding this comment.
Stale comment
Security Review Result
No high-confidence vulnerabilities found in this PR.
What I reviewed
src/evaluator/types/mod.rssrc/evaluator/types/numeric.rssrc/evaluator/types/string.rssrc/evaluator/types/tests.rs- removal of
src/evaluator/types.rsThreat-model checks
- Buffer safety: numeric readers continue to use
checked_add+ slice.get(...)bounds checks; string reader validatesoffsetand bounds all slicing.- Integer safety: no new unchecked offset arithmetic introduced.
- DoS/resource exhaustion: no new unbounded recursion/allocations; string scan remains bounded by buffer length or
max_length.- Unsafe/panics in library code: no new
unsafeand nounwrap/expectin non-test code paths.- Dependency risk: no changes to
Cargo.toml/Cargo.lockin this PR.- Information disclosure: error messages remain limited to offset/buffer length metadata.
Validation
- Ran focused tests:
cargo test evaluator::types --quiet(pass).Residual note:
cargo audit/cargo denyare not available in this runtime, but there are no dependency changes in this diff.
There was a problem hiding this comment.
Pull request overview
This PR refactors the evaluator’s type-reading layer by replacing the monolithic src/evaluator/types.rs module with a src/evaluator/types/ directory that splits numeric and string handling into focused submodules while keeping the crate::evaluator::types public entrypoint.
Changes:
- Replaced
src/evaluator/types.rswithsrc/evaluator/types/mod.rsas the public API surface and dispatcher. - Added
numeric.rsandstring.rssubmodules to isolate type-reading logic by domain. - Reorganized and expanded unit tests for the new module layout.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/evaluator/types/mod.rs | New public API surface for type-reading + dispatcher + error type + coercion helper |
| src/evaluator/types/numeric.rs | Numeric type readers (byte/short/long/quad) + unit tests |
| src/evaluator/types/string.rs | String reader implementation + unit tests |
| src/evaluator/types/tests.rs | New consolidated tests for types module behavior and dispatch |
| src/evaluator/types.rs | Removed old monolithic implementation |
| #[test] | ||
| fn test_unsupported_type_error_variants() { | ||
| let error = TypeReadError::UnsupportedType { | ||
| type_name: "CustomType".to_string(), | ||
| }; | ||
| assert!(format!("{error}").contains("CustomType")); | ||
| assert!(format!("{error:?}").contains("UnsupportedType")); | ||
|
|
||
| assert_eq!( | ||
| error, | ||
| TypeReadError::UnsupportedType { | ||
| type_name: "CustomType".to_string(), | ||
| } | ||
| ); | ||
| } | ||
|
|
There was a problem hiding this comment.
This test only validates derived Display/Debug/PartialEq for TypeReadError::UnsupportedType, but the variant is not produced by the type-reading API anywhere in the codebase. If UnsupportedType is removed (or made meaningful), this test should be adjusted accordingly to avoid locking in unused/unsupported API surface.
| #[test] | |
| fn test_unsupported_type_error_variants() { | |
| let error = TypeReadError::UnsupportedType { | |
| type_name: "CustomType".to_string(), | |
| }; | |
| assert!(format!("{error}").contains("CustomType")); | |
| assert!(format!("{error:?}").contains("UnsupportedType")); | |
| assert_eq!( | |
| error, | |
| TypeReadError::UnsupportedType { | |
| type_name: "CustomType".to_string(), | |
| } | |
| ); | |
| } |
| /// # Returns | ||
| /// | ||
| /// Returns `Ok(Value::String(string))` if the read is successful, or an appropriate error | ||
| /// if the read fails due to buffer overrun or invalid UTF-8 sequences. | ||
| /// |
There was a problem hiding this comment.
The rustdoc claims read_string can fail due to “invalid UTF-8 sequences”, but the implementation uses String::from_utf8_lossy(...) and therefore never returns an error for invalid UTF-8 (it replaces invalid bytes with U+FFFD). Please update the docs to match the actual behavior (or change the implementation to return an error on invalid UTF-8 if that’s the intended contract).
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Stale comment
Security Review Result
No high-confidence vulnerabilities found in this PR.
What I verified
- Buffer safety: numeric readers in
src/evaluator/types/numeric.rscontinue usingchecked_addplus slice.get(..)before decoding (read_short,read_long,read_quad), andread_byteuses.get(offset).- Offset arithmetic: no unchecked offset math was introduced; arithmetic remains overflow-checked before slicing.
- String handling:
src/evaluator/types/string.rsvalidatesoffset < buffer.len()before slicing, boundssearch_lenwithmin(max_len, remaining_buffer.len()), and only slices within validated bounds.- Unsafe/panics in library code: no new
unsafe,unwrap(), orexpect()in non-test library paths touched by this PR.- Dependency risk: no
Cargo.toml/Cargo.lockchanges in this diff; CI checkauditis passing for this PR.Residual risk / notes
- This change is a module split/refactor; behavior appears equivalent to the previous
types.rsimplementation. I did not find newly introduced resource-exhaustion or info-leak paths in the changed code.- Local
cargo audit/cargo denyexecution was not possible in this runner becausecargo-auditandcargo-denyare not installed, so dependency posture was validated via PR CI status instead.
🧪 CI InsightsHere's what we observed from your CI run for eb0aac2. 🟢 All jobs passed!But CI Insights is watching 👀 |
There was a problem hiding this comment.
Stale comment
Automated Risk Assessment
Risk level: Medium
Evidence from diff
- Core library evaluator path changed substantially:
src/evaluator/types.rsremoved and replaced withsrc/evaluator/types/{mod.rs,numeric.rs,string.rs,tests.rs}.- Large structural refactor footprint (about 3.3k changed lines shown as delete/add due file split), which increases regression surface even when intent is organizational.
- Production logic in key functions (
read_byte,read_short,read_long,read_quad,read_string,read_typed_value,coerce_value_to_type) appears behaviorally equivalent after split; observed differences are predominantly docs/comments/module organization.- Targeted test validation run for evaluator types passed (
65 passed, 0 failed).Decision
- Code review required (Medium risk due core shared module refactor and broad structural change).
- Requested reviewer:
@param-jasani.- No approval posted per policy for Medium risk.
- Fix inaccurate read_string docs that claimed errors for invalid UTF-8 (actually uses from_utf8_lossy replacement) - Restore # Arguments doc sections on numeric read functions - Add checked_add overflow tests for read_short/read_long/read_quad - Clarify UnsupportedType variant is reserved for future types Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
There was a problem hiding this comment.
Stale comment
Automated Risk Assessment
Risk level: Low
This PR is a structural refactor of
evaluator::types(file split into submodules) with no material behavior change detected in production logic.Evidence checked:
- Core functions (
read_byte,read_short,read_long,read_quad,read_string,read_typed_value,coerce_value_to_type) remain behaviorally equivalent after reorganization.- Changes are primarily module layout/docs/tests movement.
- Targeted validation passed:
cargo test evaluator::types --lib(65 passed, 0 failed).Per policy for Low risk, this PR is approved.
There was a problem hiding this comment.
No high-confidence vulnerabilities found in this refactor.
Security-focused review results:
- Buffer safety: Numeric readers continue using
checked_add+ slice.get(offset..end)before decoding; byte reads use.get(offset). String reads validateoffset < buffer.len()before slicing and clamp reads withmin(max_len, remaining.len()). - Integer safety: Offset arithmetic for fixed-width numeric reads still uses checked arithmetic, preventing overflow/underflow-driven OOB access.
- Unsafe/panics in library code: No new
unsafeblocks, raw pointers,unwrap(), orexpect()introduced in production paths. - DoS/resource exhaustion: No new unbounded recursion or allocation patterns were introduced by this PR; behavior appears equivalent to pre-refactor implementation.
- Dependency risk: PR diff does not modify
Cargo.toml/Cargo.lock; no new dependency attack surface introduced.
Validation run:
cargo test --lib evaluator::typespassed (68 tests).cargo audit/cargo denycould not be executed in this runner because the corresponding cargo subcommands are not installed (no such command: audit/deny). Given no dependency changes in this PR, this does not indicate a new PR-local supply-chain risk, but CI should continue enforcing these checks.
There was a problem hiding this comment.
Automated Risk Assessment
Risk level: Low
Evidence from actual diff
- Changed paths are isolated to
src/evaluator/types*and consist of splittingtypes.rsintotypes/mod.rs,types/numeric.rs,types/string.rs, andtypes/tests.rs. - Core production functions were compared across base/head and remain behaviorally equivalent (
read_byte,read_short,read_long,read_quad,read_string,read_typed_value,coerce_value_to_type). - No dependency, infra, auth, permissions, schema, or cross-system changes.
- Focused validation passed on current head:
cargo test evaluator::types --lib(68 passed,0 failed).
Decision
- Code review required: No (Low risk under policy).
- Reviewer assignment: None requested.
- Approval action: No new approval posted because this PR is already approved on the current head commit (
3f031417690f9d7f5201e52b3bc21978ece49411). - Re-approval/unapproval check: No risk increase detected versus current approved head, so no dismissal/revocation needed.
## 🤖 New release * `libmagic-rs`: 0.4.1 -> 0.4.2 (✓ API compatible changes) <details><summary><i><b>Changelog</b></i></summary><p> <blockquote> ## [0.4.2] - 2026-03-06 ### Refactor - **evaluator**: Reorganize types module into submodules ([#156](#156)) <!-- generated by git-cliff --> </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/release-plz/release-plz/). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>


This commit restructures the
typesmodule by splitting it into focused submodules for numeric and string handling. The maintypes.rsfile has been removed, and its functionality has been distributed acrossnumeric.rs,string.rs, and a newmod.rsthat serves as the public API surface. This change enhances code organization and maintainability while preserving existing functionality.types.rsand migrated its content tonumeric.rsandstring.rs.mod.rsto expose the public API and manage submodule imports.No public API changes have been made, and all existing tests have been updated accordingly to reflect the new module organization.