From 145b4c0a7fadf6dfd8c03b22c735695037930b0a Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Aug 2026 11:08:57 +0900 Subject: 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 --- crates/shirabe/src/console/input.rs | 85 +++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 4 deletions(-) (limited to 'crates/shirabe/src/console/input.rs') 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`. +/// +/// 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>, +>; + +/// PHP: the `list|\Closure(...)` union taken by the suggestedValues parameter of the +/// `Composer\Console\Input\InputArgument` / `InputOption` backport. +/// +/// The closure returns `Vec` rather than `list`: 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), + 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> { + 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), + Option(std::rc::Rc), } impl InputDefinitionItem { @@ -26,12 +103,12 @@ impl InputDefinitionItem { impl From for InputDefinitionItem { fn from(value: input_argument::InputArgument) -> Self { - Self::Argument(value) + Self::Argument(std::rc::Rc::new(value)) } } impl From for InputDefinitionItem { fn from(value: input_option::InputOption) -> Self { - Self::Option(value) + Self::Option(std::rc::Rc::new(value)) } } -- cgit v1.3.1