From bbb2f9454b6580fcbd337ced61b5a1888d52ce8d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 5 Jun 2026 01:50:16 +0900 Subject: 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 --- crates/shirabe/src/command/self_update_command.rs | 32 +++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'crates/shirabe/src/command/self_update_command.rs') 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 https://composer.github.io/pubkeys.html to find the latest keys", ); - // TODO(phase-b): closure captures none; PHP throws inside the closure on bad input - let validator: Box PhpMixed> = - Box::new(|value: PhpMixed| -> PhpMixed { + let validator: std::rc::Rc anyhow::Result> = + std::rc::Rc::new(|value: PhpMixed| -> anyhow::Result { 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(); -- cgit v1.3.1