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/io/console_io.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/io/console_io.rs')
| -rw-r--r-- | crates/shirabe/src/io/console_io.rs | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/crates/shirabe/src/io/console_io.rs b/crates/shirabe/src/io/console_io.rs index b69b68b..44e5609 100644 --- a/crates/shirabe/src/io/console_io.rs +++ b/crates/shirabe/src/io/console_io.rs @@ -487,10 +487,10 @@ impl IOInterfaceImmutable for ConsoleIO { fn ask_and_validate( &self, question: String, - validator: Box<dyn Fn(PhpMixed) -> PhpMixed>, + validator: Box<dyn Fn(PhpMixed) -> anyhow::Result<PhpMixed>>, attempts: Option<i64>, default: PhpMixed, - ) -> PhpMixed { + ) -> anyhow::Result<PhpMixed> { let _helper = self.helper_set.get("question"); let sanitized_question = Self::sanitize(PhpMixed::String(question), true) .as_string() @@ -502,13 +502,10 @@ impl IOInterfaceImmutable for ConsoleIO { Some(default) }; let mut question = Question::new(&sanitized_question, sanitized_default); - // TODO(phase-b): IOInterface validator type is Box<dyn Fn(PhpMixed) -> PhpMixed> - // but Question::set_validator expects Option<Box<dyn Fn(Option<PhpMixed>) -> Result<PhpMixed>>>. - // Bridge the signatures by adapting the input/output types. + // Question::set_validator takes Fn(Option<PhpMixed>) -> Result<PhpMixed>; adapt the + // None answer to PHP's null and forward the validator's Result unchanged. let adapted: Box<dyn Fn(Option<PhpMixed>) -> anyhow::Result<PhpMixed>> = - Box::new(move |answer: Option<PhpMixed>| { - Ok(validator(answer.unwrap_or(PhpMixed::Null))) - }); + Box::new(move |answer: Option<PhpMixed>| validator(answer.unwrap_or(PhpMixed::Null))); question.set_validator(Some(adapted)); question.set_max_attempts(attempts); |
