aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe-external-packages/src')
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs29
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs22
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs63
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/question/confirmation_question.rs54
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/question/mod.rs6
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/question/question.rs93
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs41
7 files changed, 251 insertions, 57 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs
index 9eb8fff..1fc90bd 100644
--- a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs
@@ -15,8 +15,8 @@ use crate::symfony::console::output::console_output_interface::ConsoleOutputInte
use crate::symfony::console::output::console_section_output::ConsoleSectionOutput;
use crate::symfony::console::output::output_interface;
use crate::symfony::console::output::output_interface::OutputInterface;
-use crate::symfony::console::question::choice_question::ChoiceQuestion;
-use crate::symfony::console::question::question::Question;
+use crate::symfony::console::question::ChoiceQuestion;
+use crate::symfony::console::question::QuestionInterface;
use crate::symfony::console::terminal::Terminal;
use crate::symfony::string::s;
use shirabe_php_shim::AsAny;
@@ -48,7 +48,7 @@ impl QuestionHelper {
&mut self,
input: &mut dyn InputInterface,
output: Rc<RefCell<dyn OutputInterface>>,
- question: &Question,
+ question: &impl QuestionInterface,
) -> anyhow::Result<Result<PhpMixed, MissingInputException>> {
let mut output = output;
let error_output = {
@@ -115,7 +115,7 @@ impl QuestionHelper {
fn do_ask(
&self,
output: Rc<RefCell<dyn OutputInterface>>,
- question: &Question,
+ question: &impl QuestionInterface,
) -> anyhow::Result<Result<PhpMixed, MissingInputException>> {
self.write_prompt(Rc::clone(&output), question);
@@ -220,7 +220,7 @@ impl QuestionHelper {
}
/// @return mixed
- fn get_default_answer(&self, question: &Question) -> PhpMixed {
+ fn get_default_answer(&self, question: &impl QuestionInterface) -> PhpMixed {
let default = question.get_default();
if matches!(default, PhpMixed::Null) {
@@ -230,7 +230,7 @@ impl QuestionHelper {
if let Some(validator) = question.get_validator() {
// call_user_func($question->getValidator(), $default)
return validator(Some(default)).unwrap();
- } else if let Some(choice_question) = question_as_choice_question(question) {
+ } else if let Some(choice_question) = question.as_choice() {
let choices = choice_question.get_choices();
if !choice_question.is_multiselect() {
@@ -262,11 +262,11 @@ impl QuestionHelper {
pub(crate) fn write_prompt(
&self,
output: Rc<RefCell<dyn OutputInterface>>,
- question: &Question,
+ question: &impl QuestionInterface,
) {
let mut message = question.get_question().to_string();
- if let Some(choice_question) = question_as_choice_question(question) {
+ if let Some(choice_question) = question.as_choice() {
let mut lines = vec![question.get_question().to_string()];
lines.extend(self.format_choice_question_choices(choice_question, "info"));
output
@@ -339,7 +339,7 @@ impl QuestionHelper {
fn autocomplete(
&self,
output: Rc<RefCell<dyn OutputInterface>>,
- question: &Question,
+ question: &impl QuestionInterface,
input_stream: &shirabe_php_shim::PhpResource,
autocomplete: &dyn Fn(&str) -> Vec<PhpMixed>,
) -> String {
@@ -518,7 +518,7 @@ impl QuestionHelper {
let mut temp_ret = ret.clone();
- if let Some(choice_question) = question_as_choice_question(question)
+ if let Some(choice_question) = question.as_choice()
&& choice_question.is_multiselect()
{
temp_ret = self.most_recently_entered_value(&full_choice);
@@ -680,7 +680,7 @@ impl QuestionHelper {
&self,
interviewer: &dyn Fn() -> anyhow::Result<Result<PhpMixed, MissingInputException>>,
output: Rc<RefCell<dyn OutputInterface>>,
- question: &Question,
+ question: &impl QuestionInterface,
) -> anyhow::Result<Result<PhpMixed, MissingInputException>> {
let mut error: Option<shirabe_php_shim::Exception> = None;
let mut attempts = question.get_max_attempts();
@@ -752,7 +752,7 @@ impl QuestionHelper {
fn read_input(
&self,
input_stream: &shirabe_php_shim::PhpResource,
- question: &Question,
+ question: &impl QuestionInterface,
) -> PhpMixed {
if !question.is_multiline() {
let cp = self.set_io_codepage();
@@ -869,11 +869,6 @@ impl QuestionHelper {
}
}
-/// Downcasts a Question to a ChoiceQuestion if applicable.
-fn question_as_choice_question(question: &Question) -> Option<&ChoiceQuestion> {
- question.as_any().downcast_ref::<ChoiceQuestion>()
-}
-
// PHP `__FILE__` magic constant. The shim's `file()` is PHP's file() function,
// not the magic constant, and there is no `__FILE__` shim yet (see report).
fn magic_file() -> String {
diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs
index c1d8455..9940896 100644
--- a/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs
@@ -4,9 +4,7 @@ use crate::symfony::console::formatter::output_formatter::OutputFormatter;
use crate::symfony::console::helper::question_helper::QuestionHelper;
use crate::symfony::console::output::output_interface;
use crate::symfony::console::output::output_interface::OutputInterface;
-use crate::symfony::console::question::choice_question::ChoiceQuestion;
-use crate::symfony::console::question::confirmation_question::ConfirmationQuestion;
-use crate::symfony::console::question::question::Question;
+use crate::symfony::console::question::QuestionInterface;
use crate::symfony::console::style::symfony_style::SymfonyStyle;
use shirabe_php_shim::AsAny;
use shirabe_php_shim::PhpMixed;
@@ -28,7 +26,7 @@ impl SymfonyQuestionHelper {
pub(crate) fn write_prompt(
&self,
output: Rc<RefCell<dyn OutputInterface>>,
- question: &Question,
+ question: &impl QuestionInterface,
) {
let mut text = OutputFormatter::escape_trailing_backslash(question.get_question());
let default = question.get_default();
@@ -43,11 +41,7 @@ impl SymfonyQuestionHelper {
// switch (true)
if matches!(default, PhpMixed::Null) {
text = format!(" <info>{}</info>:", PhpMixed::String(text));
- } else if question
- .as_any()
- .downcast_ref::<ConfirmationQuestion>()
- .is_some()
- {
+ } else if question.as_confirmation().is_some() {
text = format!(
" <info>{} (yes/no)</info> [<comment>{}</comment>]:",
PhpMixed::String(text),
@@ -60,11 +54,7 @@ impl SymfonyQuestionHelper {
.to_string(),
),
);
- } else if let Some(choice_question) = question
- .as_any()
- .downcast_ref::<ChoiceQuestion>()
- .filter(|q| q.is_multiselect())
- {
+ } else if let Some(choice_question) = question.as_choice().filter(|q| q.is_multiselect()) {
let choices = choice_question.get_choices();
let default_parts = shirabe_php_shim::explode(",", &default.to_string());
@@ -83,7 +73,7 @@ impl SymfonyQuestionHelper {
PhpMixed::String(text),
PhpMixed::String(OutputFormatter::escape(&resolved.join(", ")).unwrap()),
);
- } else if let Some(choice_question) = question.as_any().downcast_ref::<ChoiceQuestion>() {
+ } else if let Some(choice_question) = question.as_choice() {
let choices = choice_question.get_choices();
text = format!(
" <info>{}</info> [<comment>{}</comment>]:",
@@ -113,7 +103,7 @@ impl SymfonyQuestionHelper {
let mut prompt = " > ".to_string();
- if let Some(choice_question) = question.as_any().downcast_ref::<ChoiceQuestion>() {
+ if let Some(choice_question) = question.as_choice() {
output.borrow().writeln(
&self
.inner
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<i64>,
+ ) -> 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<Vec<PhpMixed>> {
+ self.inner.get_autocompleter_values()
+ }
+
+ fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option<Vec<PhpMixed>>)> {
+ self.inner.get_autocompleter_callback()
+ }
+
+ fn get_validator(
+ &self,
+ ) -> Option<&(dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>)> {
+ self.inner.get_validator()
+ }
+
+ fn get_max_attempts(&self) -> Option<i64> {
+ 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<String, PhpMixed>) -> IndexMap<String, String> {
diff --git a/crates/shirabe-external-packages/src/symfony/console/question/confirmation_question.rs b/crates/shirabe-external-packages/src/symfony/console/question/confirmation_question.rs
index 5076d84..74039d0 100644
--- a/crates/shirabe-external-packages/src/symfony/console/question/confirmation_question.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/question/confirmation_question.rs
@@ -1,6 +1,8 @@
//! ref: composer/vendor/symfony/console/Question/ConfirmationQuestion.php
+use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::question::Question;
+use crate::symfony::console::question::QuestionInterface;
use shirabe_php_shim::PhpMixed;
/// Represents a yes/no question.
@@ -57,3 +59,55 @@ impl ConfirmationQuestion {
})
}
}
+
+impl QuestionInterface for ConfirmationQuestion {
+ 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<Vec<PhpMixed>> {
+ self.inner.get_autocompleter_values()
+ }
+
+ fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option<Vec<PhpMixed>>)> {
+ self.inner.get_autocompleter_callback()
+ }
+
+ fn get_validator(
+ &self,
+ ) -> Option<&(dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>)> {
+ self.inner.get_validator()
+ }
+
+ fn get_max_attempts(&self) -> Option<i64> {
+ 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_confirmation(&self) -> Option<&ConfirmationQuestion> {
+ Some(self)
+ }
+}
diff --git a/crates/shirabe-external-packages/src/symfony/console/question/mod.rs b/crates/shirabe-external-packages/src/symfony/console/question/mod.rs
index 06e0c2c..06d03b4 100644
--- a/crates/shirabe-external-packages/src/symfony/console/question/mod.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/question/mod.rs
@@ -1,6 +1,6 @@
-pub mod choice_question;
-pub mod confirmation_question;
-pub mod question;
+mod choice_question;
+mod confirmation_question;
+mod question;
pub use choice_question::*;
pub use confirmation_question::*;
diff --git a/crates/shirabe-external-packages/src/symfony/console/question/question.rs b/crates/shirabe-external-packages/src/symfony/console/question/question.rs
index 9ec3aef..c93a71e 100644
--- a/crates/shirabe-external-packages/src/symfony/console/question/question.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/question/question.rs
@@ -2,8 +2,53 @@
use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::exception::logic_exception::LogicException;
+use crate::symfony::console::question::choice_question::ChoiceQuestion;
+use crate::symfony::console::question::confirmation_question::ConfirmationQuestion;
use shirabe_php_shim::PhpMixed;
+/// Polymorphic boundary for the Symfony Console Question hierarchy.
+///
+/// PHP has no `QuestionInterface`; `Question` is a concrete class extended by
+/// `ChoiceQuestion`/`ConfirmationQuestion`. Modelling those subclasses as
+/// `inner: Question` composition loses subtype identity, so consumers that take
+/// a `Question` and run `instanceof` checks are expressed here as a trait whose
+/// `as_choice`/`as_confirmation` downcasts stand in for `instanceof`.
+pub trait QuestionInterface: std::fmt::Debug {
+ fn get_question(&self) -> &str;
+
+ fn get_default(&self) -> PhpMixed;
+
+ fn is_multiline(&self) -> bool;
+
+ fn is_hidden(&self) -> bool;
+
+ fn is_hidden_fallback(&self) -> bool;
+
+ fn get_autocompleter_values(&self) -> Option<Vec<PhpMixed>>;
+
+ fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option<Vec<PhpMixed>>)>;
+
+ fn get_validator(
+ &self,
+ ) -> Option<&(dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>)>;
+
+ fn get_max_attempts(&self) -> Option<i64>;
+
+ fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)>;
+
+ fn is_trimmable(&self) -> bool;
+
+ /// Models `$question instanceof ChoiceQuestion`.
+ fn as_choice(&self) -> Option<&ChoiceQuestion> {
+ None
+ }
+
+ /// Models `$question instanceof ConfirmationQuestion`.
+ fn as_confirmation(&self) -> Option<&ConfirmationQuestion> {
+ None
+ }
+}
+
/// Represents a Question.
pub struct Question {
question: String,
@@ -275,3 +320,51 @@ impl Question {
self
}
}
+
+impl QuestionInterface for Question {
+ fn get_question(&self) -> &str {
+ self.get_question()
+ }
+
+ fn get_default(&self) -> PhpMixed {
+ self.get_default()
+ }
+
+ fn is_multiline(&self) -> bool {
+ self.is_multiline()
+ }
+
+ fn is_hidden(&self) -> bool {
+ self.is_hidden()
+ }
+
+ fn is_hidden_fallback(&self) -> bool {
+ self.is_hidden_fallback()
+ }
+
+ fn get_autocompleter_values(&self) -> Option<Vec<PhpMixed>> {
+ self.get_autocompleter_values()
+ }
+
+ fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option<Vec<PhpMixed>>)> {
+ self.get_autocompleter_callback()
+ }
+
+ fn get_validator(
+ &self,
+ ) -> Option<&(dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>)> {
+ self.get_validator()
+ }
+
+ fn get_max_attempts(&self) -> Option<i64> {
+ self.get_max_attempts()
+ }
+
+ fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)> {
+ self.get_normalizer()
+ }
+
+ fn is_trimmable(&self) -> bool {
+ self.is_trimmable()
+ }
+}
diff --git a/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs b/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs
index 4b945b0..f445877 100644
--- a/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs
@@ -18,6 +18,7 @@ use crate::symfony::console::output::output_interface::OUTPUT_NORMAL;
use crate::symfony::console::question::ChoiceQuestion;
use crate::symfony::console::question::ConfirmationQuestion;
use crate::symfony::console::question::Question;
+use crate::symfony::console::question::QuestionInterface;
use crate::symfony::console::style::output_style::OutputStyle;
use crate::symfony::console::style::style_interface::StyleInterface;
use crate::symfony::console::terminal::Terminal;
@@ -211,7 +212,7 @@ impl SymfonyStyle {
todo!()
}
- pub fn ask_question(&mut self, question: Question) -> PhpMixed {
+ pub fn ask_question(&mut self, question: &impl QuestionInterface) -> PhpMixed {
if self.input.borrow().is_interactive() {
self.auto_prepend_block();
}
@@ -227,7 +228,7 @@ impl SymfonyStyle {
self.question_helper
.as_mut()
.unwrap()
- .ask(&mut *input, self.output.clone(), &question)
+ .ask(&mut *input, self.output.clone(), question)
};
// PHP `askQuestion` returns the answer directly; exceptions propagate. Phase B
// collapses the double `Result` by panicking on either error.
@@ -669,7 +670,7 @@ impl StyleInterface for SymfonyStyle {
);
question.set_validator(Self::adapt_validator(validator));
- self.ask_question(question)
+ self.ask_question(&question)
}
/// {@inheritdoc}
@@ -683,23 +684,18 @@ impl StyleInterface for SymfonyStyle {
question.set_hidden(true);
question.set_validator(Self::adapt_validator(validator));
- self.ask_question(question)
+ self.ask_question(&question)
}
/// {@inheritdoc}
fn confirm(&mut self, question: &str, default: bool) -> bool {
- // PHP: return $this->askQuestion(new ConfirmationQuestion($question, $default));
- // ConfirmationQuestion extends Question, but the ported type embeds a private
- // `inner: Question` and cannot be passed to `ask_question(Question)`. The
- // Question class hierarchy needs a trait/enum to dispatch polymorphically.
- let _confirmation_question =
- ConfirmationQuestion::new(question.to_string(), default, "/^y/i".to_string());
- let answer = self.ask_question(todo!());
- // PHP returns the bool answer from ConfirmationQuestion.
- match answer {
- PhpMixed::Bool(b) => b,
- _ => todo!(),
- }
+ let answer = self.ask_question(&ConfirmationQuestion::new(
+ question.to_string(),
+ default,
+ "/^y/i".to_string(),
+ ));
+
+ shirabe_php_shim::boolval(&answer)
}
/// {@inheritdoc}
@@ -713,21 +709,24 @@ impl StyleInterface for SymfonyStyle {
let values =
shirabe_php_shim::array_flip(&PhpMixed::List(choices.iter().cloned().collect()));
// $default = $values[$default] ?? $default;
- let _ = values;
- Some(default)
+ let resolved = match &values {
+ PhpMixed::Array(map) => map.get(&default.to_string()).cloned(),
+ _ => None,
+ };
+ Some(resolved.unwrap_or(default))
} else {
None
};
// PHP: return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
- // ChoiceQuestion extends Question; see `confirm` for the polymorphism note.
let choices_map: indexmap::IndexMap<String, PhpMixed> = choices
.into_iter()
.enumerate()
.map(|(i, c)| (i.to_string(), c))
.collect();
- let _choice_question = ChoiceQuestion::new(question.to_string(), choices_map, default);
- self.ask_question(todo!())
+ let choice_question = ChoiceQuestion::new(question.to_string(), choices_map, default)
+ .expect("choice() always provides at least one choice");
+ self.ask_question(&choice_question)
}
/// {@inheritdoc}