aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/console/input.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-02 11:08:57 +0900
committernsfisis <nsfisis@gmail.com>2026-08-02 11:08:57 +0900
commit145b4c0a7fadf6dfd8c03b22c735695037930b0a (patch)
treecac7c4e265b2f6aeaefdf0431e2c22e54b26b8ef /crates/shirabe/src/console/input.rs
parent2592062434eeb5fe814dbca953d5fd23458bc135 (diff)
downloadphp-shirabe-145b4c0a7fadf6dfd8c03b22c735695037930b0a.tar.gz
php-shirabe-145b4c0a7fadf6dfd8c03b22c735695037930b0a.tar.zst
php-shirabe-145b4c0a7fadf6dfd8c03b22c735695037930b0a.zip
feat(console-input): port the suggested-values backport onto InputArgument/InputOption
Composer backports symfony/console 6.1's $suggestedValues parameter in Composer\Console\Input\{InputArgument,InputOption}; the Rust newtypes had dropped it. PHP closures are bound to the command ($this), but a command cannot capture a handle to itself while configure() runs inside new(), so the closure receives the bound command as an explicit `this` argument at call time instead. - add SuggestedValues (list | this-taking closure) and wire it through InputArgument::new5 / InputOption::new6 and their complete() methods - track Composer-typed definition entries by name in BaseCommandData side maps, standing in for PHP's instanceof checks (set_definition converts entries to the Symfony types for storage) - add base_command_complete, the BaseCommand::complete dispatch shared by every Composer command - introduce BaseCommand::base_command_data and make command_data a default method on top of it Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/console/input.rs')
-rw-r--r--crates/shirabe/src/console/input.rs85
1 files changed, 81 insertions, 4 deletions
diff --git a/crates/shirabe/src/console/input.rs b/crates/shirabe/src/console/input.rs
index 9141f0cb..9661c7ad 100644
--- a/crates/shirabe/src/console/input.rs
+++ b/crates/shirabe/src/console/input.rs
@@ -4,9 +4,86 @@ pub mod input_option;
pub use input_argument::*;
pub use input_option::*;
+use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput;
+use shirabe_external_packages::symfony::console::completion::completion_suggestions::{
+ CompletionSuggestions, StringOrSuggestion,
+};
+
+/// PHP: `\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion>`.
+///
+/// PHP closures are bound to the command instance (`$this`); commands cannot capture a handle
+/// to themselves while `configure()` runs inside `new()`, so the bound command is passed in as
+/// `this` at call time instead.
+pub type SuggestedValuesClosure = Box<
+ dyn Fn(
+ &dyn crate::command::BaseCommand,
+ &CompletionInput,
+ &mut CompletionSuggestions,
+ ) -> anyhow::Result<Vec<String>>,
+>;
+
+/// PHP: the `list<string>|\Closure(...)` union taken by the suggestedValues parameter of the
+/// `Composer\Console\Input\InputArgument` / `InputOption` backport.
+///
+/// The closure returns `Vec<String>` rather than `list<string|Suggestion>`: every Composer
+/// closure returns plain strings, and `complete` lifts them into suggestions. PHP's runtime
+/// "Closure must return an array" LogicException is statically guaranteed by the type.
+pub enum SuggestedValues {
+ List(Vec<String>),
+ Closure(SuggestedValuesClosure),
+}
+
+impl std::fmt::Debug for SuggestedValues {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ SuggestedValues::List(values) => f.debug_tuple("List").field(values).finish(),
+ SuggestedValues::Closure(_) => f.write_str("Closure(..)"),
+ }
+ }
+}
+
+impl SuggestedValues {
+ /// Whether PHP's `[] !== $suggestedValues` is false, i.e. no suggestions were declared.
+ pub(crate) fn is_empty(&self) -> bool {
+ matches!(self, SuggestedValues::List(values) if values.is_empty())
+ }
+
+ /// The shared body of the `InputArgument::complete` / `InputOption::complete` backport.
+ pub(crate) fn complete(
+ &self,
+ this: &dyn crate::command::BaseCommand,
+ input: &CompletionInput,
+ suggestions: &mut CompletionSuggestions,
+ ) -> anyhow::Result<()> {
+ let values = match self {
+ SuggestedValues::List(values) => values.clone(),
+ SuggestedValues::Closure(closure) => closure(this, input, suggestions)?,
+ };
+ if !values.is_empty() {
+ suggestions
+ .suggest_values(values.into_iter().map(StringOrSuggestion::String).collect());
+ }
+ Ok(())
+ }
+
+ /// PHP: `$this->suggestX()($input)` — invoking a suggestion closure directly. Calling this
+ /// on a List is a programming error (PHP would fatal on `$array()`).
+ pub fn call(
+ &self,
+ this: &dyn crate::command::BaseCommand,
+ input: &CompletionInput,
+ suggestions: &mut CompletionSuggestions,
+ ) -> anyhow::Result<Vec<String>> {
+ match self {
+ SuggestedValues::Closure(closure) => closure(this, input, suggestions),
+ SuggestedValues::List(_) => panic!("SuggestedValues::call on a non-closure"),
+ }
+ }
+}
+
pub enum InputDefinitionItem {
- Argument(input_argument::InputArgument),
- Option(input_option::InputOption),
+ Argument(std::rc::Rc<input_argument::InputArgument>),
+ Option(std::rc::Rc<input_option::InputOption>),
}
impl InputDefinitionItem {
@@ -26,12 +103,12 @@ impl InputDefinitionItem {
impl From<input_argument::InputArgument> for InputDefinitionItem {
fn from(value: input_argument::InputArgument) -> Self {
- Self::Argument(value)
+ Self::Argument(std::rc::Rc::new(value))
}
}
impl From<input_option::InputOption> for InputDefinitionItem {
fn from(value: input_option::InputOption) -> Self {
- Self::Option(value)
+ Self::Option(std::rc::Rc::new(value))
}
}