Skip to content

refactor(menu): remove the panic surfaces in parse_selection_char and truncate_with_ansi - #1165

Open
kronberger-droid wants to merge 3 commits into
nushell:mainfrom
kronberger-droid:refactor/menu-functions-panics
Open

refactor(menu): remove the panic surfaces in parse_selection_char and truncate_with_ansi#1165
kronberger-droid wants to merge 3 commits into
nushell:mainfrom
kronberger-droid:refactor/menu-functions-panics

Conversation

@kronberger-droid

Copy link
Copy Markdown
Collaborator

Summary

Two of the menu_functions.rs sites flagged by clippy::expect_used / clippy::indexing_slicing,
part of a sweep to move the crate's panic surfaces toward zero.
Neither was a reachable panic in practice; both were invariants the code re-checked because the types did not carry them.

  • parse_selection_char: to_digit(10).expect(..) right after is_ascii_digit().
    Fused into if let Some(d) = c.to_digit(10) so the guard and the conversion are one operation.
  • truncate_with_ansi: the find loop carried a bool and two indexes that only described the same cut through the budget arithmetic.
    A Fit enum names the three ways a segment can sit against the width budget,
    a Cut struct is set in exactly one place,
    and the emit phase reads slices via split_at_checked and first() instead of indexing.

Additional notes

The ... moved into a TRUNCATION_SUFFIX const and the grapheme walk into prefix_len_within_width.

Middle commit adds eleven characterisation cases for truncate_with_ansi ahead of the rewrite.

string_difference still has four sites in this file; it wants its own pass and follows separately.

Characterisation cases ahead of restructuring the truncation loop.
The existing cases only used escapes with a trailing ';', which the
parser reads as a reset, so none of them exercised style preservation
across the cut.
…th ansi

The find loop carried a bool and two indexes that only described the
same cut through the budget arithmetic: the byte offset survived
later segments because their walk hit a zero budget before assigning.
A Fit enum names the three outcomes and a Cut struct is set in one
place, thus the emit phase reads slices via split_at_checked instead
of indexing.

Also pulls the suffix into a const and the grapheme walk into a
helper.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant