diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-05 01:50:16 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-05 01:50:16 +0900 |
| commit | bbb2f9454b6580fcbd337ced61b5a1888d52ce8d (patch) | |
| tree | 45563449df57f63ae4a4d010a3e8e6007518c35b /crates/shirabe/src/command/self_update_command.rs | |
| parent | adf14510b00929aaee85ccb8dedf9164878a0164 (diff) | |
| download | php-shirabe-bbb2f9454b6580fcbd337ced61b5a1888d52ce8d.tar.gz php-shirabe-bbb2f9454b6580fcbd337ced61b5a1888d52ce8d.tar.zst php-shirabe-bbb2f9454b6580fcbd337ced61b5a1888d52ce8d.zip | |
refactor(io): model ask_and_validate validators with anyhow::Result
PHP's askAndValidate throws when a validator rejects input. Change the
IOInterface validator callback and return type to anyhow::Result so the
call sites can return Err instead of panic, faithfully modeling the throw
semantics already supported by the Question layer.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/command/self_update_command.rs')
| -rw-r--r-- | crates/shirabe/src/command/self_update_command.rs | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/crates/shirabe/src/command/self_update_command.rs b/crates/shirabe/src/command/self_update_command.rs index 9b960f0..63e6a0c 100644 --- a/crates/shirabe/src/command/self_update_command.rs +++ b/crates/shirabe/src/command/self_update_command.rs @@ -672,9 +672,8 @@ RGv89BPD+2DLnJysngsvVaUCAwEAAQ==\n\ "Open <info>https://composer.github.io/pubkeys.html</info> to find the latest keys", ); - // TODO(phase-b): closure captures none; PHP throws inside the closure on bad input - let validator: Box<dyn Fn(PhpMixed) -> PhpMixed> = - Box::new(|value: PhpMixed| -> PhpMixed { + let validator: std::rc::Rc<dyn Fn(PhpMixed) -> anyhow::Result<PhpMixed>> = + std::rc::Rc::new(|value: PhpMixed| -> anyhow::Result<PhpMixed> { let value_str = value.as_string().unwrap_or("").to_string(); if !Preg::is_match( r"{^-----BEGIN PUBLIC KEY-----$}", @@ -682,11 +681,17 @@ RGv89BPD+2DLnJysngsvVaUCAwEAAQ==\n\ ) .unwrap_or(false) { - // TODO(phase-b): closure cannot throw - panic!("{}", "Invalid input"); + return Err(UnexpectedValueException { + message: "Invalid input".to_string(), + code: 0, + } + .into()); } - PhpMixed::String(format!("{}\n", shirabe_php_shim::trim(&value_str, None))) + Ok(PhpMixed::String(format!( + "{}\n", + shirabe_php_shim::trim(&value_str, None) + ))) }); let mut dev_key = String::new(); @@ -708,10 +713,13 @@ RGv89BPD+2DLnJysngsvVaUCAwEAAQ==\n\ dev_key = io .ask_and_validate( "Enter Dev / Snapshot Public Key (including lines with -----): ".to_string(), - Box::new(|v: PhpMixed| v), + { + let validator = validator.clone(); + Box::new(move |v: PhpMixed| validator(v)) + }, None, PhpMixed::Null, - ) + )? .as_string() .unwrap_or("") .to_string(); @@ -727,7 +735,6 @@ RGv89BPD+2DLnJysngsvVaUCAwEAAQ==\n\ } } } - let _ = &validator; let key_path = format!( "{}/keys.dev.pub", config.get("home").as_string().unwrap_or("") @@ -757,10 +764,13 @@ RGv89BPD+2DLnJysngsvVaUCAwEAAQ==\n\ tags_key = io .ask_and_validate( "Enter Tags Public Key (including lines with -----): ".to_string(), - Box::new(|v: PhpMixed| v), + { + let validator = validator.clone(); + Box::new(move |v: PhpMixed| validator(v)) + }, None, PhpMixed::Null, - ) + )? .as_string() .unwrap_or("") .to_string(); |
