Skip to content

refactor: extract shared truncate_str helper into renderers/utils.rs#183

Merged
inureyes merged 1 commit into
mainfrom
refactor/issue-179-shared-truncate-str
Apr 17, 2026
Merged

refactor: extract shared truncate_str helper into renderers/utils.rs#183
inureyes merged 1 commit into
mainfrom
refactor/issue-179-shared-truncate-str

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

  • The private truncate_str function existed as byte-identical copies in both vgpu_renderer.rs and mig_renderer.rs
  • Extracted the shared function into a new src/ui/renderers/utils.rs module (pub(crate) visibility)
  • Declared the module in src/ui/renderers/mod.rs as pub(crate) mod utils
  • Updated both vgpu_renderer.rs and mig_renderer.rs to import and use crate::ui::renderers::utils::truncate_str
  • Removed the old private copies and their local test duplicates from each renderer file

Implementation notes

  • Target location chosen: src/ui/renderers/utils.rs (new dedicated file). mod.rs only contained module declarations and re-exports, not shared helpers, so a separate utils.rs is the cleaner choice.
  • The implementation is preserved exactly as-is: char-based counting via chars().count() and chars().take(), not byte-based — no behavioral change.
  • No UTF-8 safety bugs were found. The existing implementation correctly uses char boundaries throughout.

Tests added

The new utils.rs module includes 8 unit tests covering:

  • Empty string with cap 0 and cap > 0
  • String shorter than cap (unchanged)
  • String equal to cap (unchanged)
  • String longer than cap (truncated with ellipsis, total char count == max_chars)
  • max_chars = 1 (only ellipsis returned)
  • Multibyte CJK characters (3 bytes each, counted as 1 char each)
  • Multibyte emoji (4 bytes each, counted as 1 char each)
  • max_chars = 0 edge case (empty input stays empty; non-empty input yields "…")

Test plan

  • cargo build passes
  • cargo test passes (499 unit tests + 547 integration tests — all green)
  • cargo clippy -- -D warnings produces no warnings
  • Pre-commit formatting hook (cargo fmt) applied and committed

Closes #179

Move the byte-identical private truncate_str functions from vgpu_renderer
and mig_renderer into a new pub(crate) src/ui/renderers/utils.rs module.
Add comprehensive unit tests covering empty string, shorter/equal/longer
than cap, max_chars=0, and multibyte UTF-8 (CJK and emoji) inputs.

Closes #179
@inureyes inureyes added type:refactor Code refactoring priority:low Low priority issue type:enhancement New feature or request status:review Under review labels Apr 17, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Extract the byte-identical private truncate_str helper from vgpu_renderer.rs and mig_renderer.rs into a shared src/ui/renderers/utils.rs module and wire both renderers to the shared copy.

Findings Addressed

None — no CRITICAL/HIGH/MEDIUM/LOW findings were raised.

Remaining Items

None.

Verification

  • All stated requirements implemented — utils.rs created, both renderers import crate::ui::renderers::utils::truncate_str, module declared as pub(crate) mod utils in mod.rs.
  • Behavioral identity confirmed — extracted function body is byte-for-byte identical to the pre-PR private copies (verified against main): same chars().count() short-circuit, same chars().take(max_chars.saturating_sub(1)), same ellipsis push.
  • No leftover duplicates — grep "fn truncate_str" across src/ finds exactly one definition (utils.rs:31). Old private bodies and their local test copies removed from both renderer files.
  • Integrated into project code flow — call sites in vgpu_renderer.rs (lines 95, 122) and mig_renderer.rs (line 84) now resolve to the shared helper. Imports correctly ordered per project Rust style (stdlib → external → internal alphabetical).
  • No placeholder/mock code remaining.
  • Project conventions followed:
    • pub(crate) visibility matches the codebase convention (zero pub(super) usage anywhere in src/; siblings like layout::max_gpu_lines_for_tab, gpu_renderer::format_hostname_with_scroll all use pub(crate)).
    • Apache-2.0 license header matches neighboring renderer files.
    • Commit prefix refactor:, imperative mood, English-only, no co-author trailer, no AI attribution.
  • Existing modules reused where applicable — no reimplementation; storage_renderer continues to use its separate crate::ui::text::truncate_to_width helper (different semantics, intentionally kept distinct).
  • No unintended structural changes — only 4 files touched; no renames, no API surface changes.
  • Test coverage strictly expanded, not lost:
    • Pre-PR: 2 tests per renderer (4 total), exercising ("abc", 10), ("abcdefghij", 10), ("abcdefghij", 5).
    • Post-PR: 8 tests in utils.rs fully subsume the old cases and add empty-string, max_chars=0, max_chars=1, CJK multibyte, and emoji scalar-value coverage.
  • Tests are discovered and run — cargo test ui::renderers::utils → 8 passed, confirming the module compiles into the test binary (mod declaration in mod.rs is correctly placed, no cfg gate).
  • Tests pass — cargo test --all-targets: 499 lib + 547 integration + all other targets green, 0 failures.
  • cargo build --all-targets clean.
  • cargo clippy --all-targets -- -D warnings clean.
  • cargo fmt -- --check clean.

Notes

  • Codex CLI was available (codex-cli 0.120.0) but the Task tool needed to spawn a codex-reviewer subagent is not exposed in this review session. Per the Phase 2.5 fallback protocol, the review proceeded with direct analysis.
  • This is a textbook low-risk refactor: bit-identical extraction, broader test coverage, project-consistent visibility, no scope creep.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Apr 17, 2026
@inureyes inureyes merged commit 9e6461c into main Apr 17, 2026
4 checks passed
@inureyes inureyes deleted the refactor/issue-179-shared-truncate-str branch April 17, 2026 07:13
@inureyes inureyes self-assigned this Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:low Low priority issue status:done Completed type:enhancement New feature or request type:refactor Code refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: Extract shared truncate_str helper from vGPU and MIG renderers

1 participant