fix: prevent panic when selecting difficulty with no challenges#278
fix: prevent panic when selecting difficulty with no challenges#278unhappychoice merged 1 commit intomainfrom
Conversation
- Add validation to check if challenges are available for selected difficulty - Display error message when no challenges are available - Clear error message when difficulty is changed - Prevent transition to Typing screen when challenge count is 0 Closes: #276 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
WalkthroughAdds error-state handling to TitleScreen when starting a game with no available challenges. Introduces an optional error message, clears it on navigation, validates challenge availability on Space, and passes the message to the difficulty selection view, which conditionally renders the error instead of descriptions. Also updates challenge count initialization. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant TitleScreen
participant StageRepository as Stage Repo
participant DifficultyView as Difficulty View
participant Game as Game State
rect rgb(240,248,255)
note over TitleScreen: Init
TitleScreen->>StageRepository: fetch challenge_counts
StageRepository-->>TitleScreen: counts per difficulty
TitleScreen->>DifficultyView: draw(..., error_message=None)
end
User->>TitleScreen: Left/Right
TitleScreen->>TitleScreen: update selected_difficulty<br/>clear error_message
TitleScreen->>DifficultyView: draw(..., error_message=None)
User->>TitleScreen: Space (Start)
TitleScreen->>TitleScreen: check challenge_counts[selected]
alt No challenges
TitleScreen->>TitleScreen: set error_message = "No challenges..."
TitleScreen->>DifficultyView: draw(..., error_message=Some)
TitleScreen-->>User: stay on TitleScreen
else Has challenges
TitleScreen->>Game: transition to Typing
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #278 +/- ##
==========================================
- Coverage 24.61% 24.59% -0.03%
==========================================
Files 195 195
Lines 21720 21743 +23
==========================================
Hits 5347 5347
- Misses 16373 16396 +23
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/presentation/game/views/title/difficulty_selection_view.rs (1)
21-21: PreferOption<&str>overOption<&String>.Using
&Stringis less idiomatic in Rust.&stris more flexible and works withString,&str, and string literals without requiring an intermediate reference.Apply this diff:
- error_message: Option<&String>, + error_message: Option<&str>,Also update line 184 in
title_screen.rs:- self.error_message.as_ref(), + self.error_message.as_deref(),
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/presentation/game/screens/title_screen.rs(5 hunks)src/presentation/game/views/title/difficulty_selection_view.rs(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/presentation/game/views/title/difficulty_selection_view.rs (2)
src/presentation/ui/colors.rs (2)
error(41-43)to_crossterm(163-185)src/domain/models/difficulty_level.rs (1)
description(23-31)
🔇 Additional comments (4)
src/presentation/game/views/title/difficulty_selection_view.rs (1)
79-104: LGTM!The conditional rendering logic correctly displays error messages when present and falls back to difficulty descriptions otherwise. The error styling (red + bold) provides clear visual feedback to users.
src/presentation/game/screens/title_screen.rs (3)
105-117: LGTM! Validation prevents the panic condition.The challenge count check correctly prevents screen transition when no challenges are available, addressing the panic issue from #276. The error message is clear and actionable.
Minor observation: The error message is 95 characters long, which might wrap or truncate on narrow terminals, though the
saturating_subcentering logic handles this gracefully.
119-128: LGTM! Error cleared on navigation.Clearing the error message when the user changes difficulty selection is correct behavior, ensuring the error state doesn't persist inappropriately.
184-184: LGTM! Error state correctly passed to view.The error message is properly passed to the view using
as_ref(), enabling the conditional rendering of errors in the UI.
| git_repository: Option<GitRepository>, | ||
| action_result: Option<TitleAction>, | ||
| needs_render: bool, | ||
| error_message: Option<String>, |
There was a problem hiding this comment.
Clear error_message in the init() method.
The field is properly initialized in new(), but the init() method (line 89) doesn't clear error_message. If the screen is re-initialized after displaying an error (e.g., returning from another screen), the stale error message will persist until the user navigates.
Apply this diff to clear the error message in init():
fn init(&mut self) -> Result<()> {
self.action_result = None;
+ self.error_message = None;
self.needs_render = true;Also applies to: 53-53
🤖 Prompt for AI Agents
In src/presentation/game/screens/title_screen.rs around lines 36 and 53 (field
declaration) and update the init() method (around line 89) to clear any stale
error state: set the screen's error_message to None at the start of init() so
that re-initializing the screen removes any previously displayed error; ensure
you mutate the proper struct field (e.g., self.error_message = None) before
returning from init().
Closes: #276
Summary
Fix panic that occurs when selecting difficulty on the title screen when repository contains no extractable challenges.
Changes
Test plan
cargo checkcargo testcargo clippy --all-targets --all-features -- -D warningscargo fmt🤖 Generated with Claude Code
Summary by CodeRabbit