From 27c961ee7bee5c775e1902e59f74a9871d31321d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 24 Jun 2026 00:49:51 +0900 Subject: feat(question): model Question hierarchy via QuestionInterface trait Introduce a QuestionInterface trait that the base Question and all its subclasses (ChoiceQuestion, ConfirmationQuestion, StrictConfirmationQuestion) implement, with as_choice/as_confirmation downcasts standing in for PHP's instanceof. Consumers (QuestionHelper, SymfonyQuestionHelper, SymfonyStyle, ConsoleIO) now take a QuestionInterface boundary generically. This fixes the instanceof emulation, which previously went through as_any().downcast_ref on a concrete &Question and always returned None, and unblocks the select/confirm/choice paths that were left as todo!() because a polymorphic ChoiceQuestion/ConfirmationQuestion could not be passed to ask. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../symfony/console/question/choice_question.rs | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs') diff --git a/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs b/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs index 24d434d..b862eb8 100644 --- a/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs +++ b/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs @@ -3,6 +3,7 @@ use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException; use crate::symfony::console::exception::logic_exception::LogicException; use crate::symfony::console::question::Question; +use crate::symfony::console::question::QuestionInterface; use indexmap::IndexMap; use shirabe_php_shim::PhpMixed; @@ -83,6 +84,16 @@ impl ChoiceQuestion { self } + /// Inherited from Question. Sets the maximum number of attempts. + pub fn set_max_attempts( + &mut self, + attempts: Option, + ) -> Result<&mut Self, InvalidArgumentException> { + self.inner.set_max_attempts(attempts)?; + + Ok(self) + } + /// Sets the error message for invalid values. /// /// The error message has a string placeholder (%s) for the invalid value. @@ -229,6 +240,58 @@ impl ChoiceQuestion { } } +impl QuestionInterface for ChoiceQuestion { + fn get_question(&self) -> &str { + self.inner.get_question() + } + + fn get_default(&self) -> PhpMixed { + self.inner.get_default() + } + + fn is_multiline(&self) -> bool { + self.inner.is_multiline() + } + + fn is_hidden(&self) -> bool { + self.inner.is_hidden() + } + + fn is_hidden_fallback(&self) -> bool { + self.inner.is_hidden_fallback() + } + + fn get_autocompleter_values(&self) -> Option> { + self.inner.get_autocompleter_values() + } + + fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option>)> { + self.inner.get_autocompleter_callback() + } + + fn get_validator( + &self, + ) -> Option<&(dyn Fn(Option) -> Result)> { + self.inner.get_validator() + } + + fn get_max_attempts(&self) -> Option { + self.inner.get_max_attempts() + } + + fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)> { + self.inner.get_normalizer() + } + + fn is_trimmable(&self) -> bool { + self.inner.is_trimmable() + } + + fn as_choice(&self) -> Option<&ChoiceQuestion> { + Some(self) + } +} + /// array_search operates over the choice values as strings; this projects the /// choices map's values into the string-keyed form the shim expects. fn choices_as_str(choices: &IndexMap) -> IndexMap { -- cgit v1.3.1