Skip to content

Refactor CLI args and add input validation with shift range support#9

Merged
T3pp31 merged 1 commit into
mainfrom
claude/code-review-improvements-rhWJj
Feb 21, 2026
Merged

Refactor CLI args and add input validation with shift range support#9
T3pp31 merged 1 commit into
mainfrom
claude/code-review-improvements-rhWJj

Conversation

@T3pp31
Copy link
Copy Markdown
Owner

@T3pp31 T3pp31 commented Feb 21, 2026

Summary

This PR refactors the CLI argument handling to reduce code duplication, adds comprehensive input size validation, expands shift value range support, and improves error messages for better user experience.

Key Changes

CLI Refactoring

  • Introduced CipherArgs struct to consolidate common arguments between Encrypt and Decrypt commands, eliminating ~40 lines of duplicated field definitions
  • Updated command variants to use Encrypt(CipherArgs) and Decrypt(CipherArgs) tuple struct syntax
  • Simplified command matching logic in run_cli()

Input Validation

  • Added MAX_INPUT_SIZE constant (1MB) to prevent processing excessively large inputs
  • Implemented file size validation before reading to avoid memory issues
  • Added input size checks for text arguments and interactive mode input
  • Enhanced error messages to be more descriptive (e.g., "Input text cannot be empty or whitespace-only")

Shift Value Handling

  • Added MIN_SHIFT constant (-25) to support negative shift values
  • Introduced validate_shift_input() function with comprehensive validation:
    • Returns parsed shift value with optional warning message
    • Warns when shift is outside typical range (-25 to 25) but still accepts it
    • Gracefully handles invalid input by returning default shift
  • Updated prompt_for_shift() to use new validation function and display more helpful prompts
  • Updated brute force to include shift 0 (showing original text for reference)

Whitespace Handling

  • Enhanced empty text validation to reject whitespace-only input
  • Updated error messages across encrypt_safe and decrypt_safe functions
  • Added comprehensive test coverage for whitespace edge cases

Documentation & Tests

  • Updated help text descriptions to reflect new shift range support
  • Added 20+ new tests covering:
    • Input size limit validation
    • Shift value validation with boundary cases
    • Whitespace-only input rejection
    • Leading/trailing whitespace preservation
    • Brute force shift 0 inclusion
  • Updated existing tests to match new error messages

Minor Improvements

  • Optimized main() to use env::args().count() instead of collecting into vector
  • Updated version to 1.0.4
  • Improved code organization with better comments and test grouping

https://claude.ai/code/session_01UwAwi9MgGMEYuzf5NqHPuF

- CLIヘルプテキストのシフト範囲記述を実態に合わせて修正
- prompt_for_shiftに範囲外警告付きバリデーション追加(validate_shift_input関数抽出)
- encrypt_safe/decrypt_safeで空白文字のみのテキストを拒否するよう修正
- get_input_textに入力サイズ制限(10MB)を追加
- ブルートフォースでshift=0(元テキスト)も表示するよう変更
- main.rsのVec<String>収集をenv::args().count()に簡略化
- Encrypt/DecryptのenumフィールドをCipherArgs構造体に共通化
- バージョンを1.0.3→1.0.4にバンプ

https://claude.ai/code/session_01UwAwi9MgGMEYuzf5NqHPuF
@T3pp31 T3pp31 merged commit 57410a2 into main Feb 21, 2026
1 check passed
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.

2 participants