diff options
Diffstat (limited to 'crates/shirabe/src/io')
| -rw-r--r-- | crates/shirabe/src/io/buffer_io.rs | 4 | ||||
| -rw-r--r-- | crates/shirabe/src/io/console_io.rs | 13 | ||||
| -rw-r--r-- | crates/shirabe/src/io/io_interface.rs | 8 | ||||
| -rw-r--r-- | crates/shirabe/src/io/null_io.rs | 6 |
4 files changed, 14 insertions, 17 deletions
diff --git a/crates/shirabe/src/io/buffer_io.rs b/crates/shirabe/src/io/buffer_io.rs index 291b64b..836fd95 100644 --- a/crates/shirabe/src/io/buffer_io.rs +++ b/crates/shirabe/src/io/buffer_io.rs @@ -175,10 +175,10 @@ impl crate::io::IOInterfaceImmutable for BufferIO { 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> { self.inner .ask_and_validate(question, validator, attempts, default) } 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); diff --git a/crates/shirabe/src/io/io_interface.rs b/crates/shirabe/src/io/io_interface.rs index e48f9ce..e33cea2 100644 --- a/crates/shirabe/src/io/io_interface.rs +++ b/crates/shirabe/src/io/io_interface.rs @@ -101,10 +101,10 @@ pub trait IOInterfaceImmutable: std::fmt::Debug { 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>; fn ask_and_hide_answer(&self, question: String) -> Option<String>; @@ -279,10 +279,10 @@ impl IOInterfaceImmutable for Rc<RefCell<dyn IOInterface>> { 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> { self.borrow() .ask_and_validate(question, validator, attempts, default) } diff --git a/crates/shirabe/src/io/null_io.rs b/crates/shirabe/src/io/null_io.rs index 85a5fa5..0a83bff 100644 --- a/crates/shirabe/src/io/null_io.rs +++ b/crates/shirabe/src/io/null_io.rs @@ -70,11 +70,11 @@ impl IOInterfaceImmutable for NullIO { 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 { - default + ) -> anyhow::Result<PhpMixed> { + Ok(default) } fn ask_and_hide_answer(&self, _question: String) -> Option<String> { |
