diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-19 23:54:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-20 00:01:37 +0900 |
| commit | e5fcde172b38d9a71292d0b4dd7f5d347da2e748 (patch) | |
| tree | c1091a1d94efb1c3ac1cda7c59a18ce53d74313b /crates/shirabe-external-packages/src | |
| parent | adfa7c6b295521a6f8fdf4084993a80a8030e49b (diff) | |
| download | php-shirabe-e5fcde172b38d9a71292d0b4dd7f5d347da2e748.tar.gz php-shirabe-e5fcde172b38d9a71292d0b4dd7f5d347da2e748.tar.zst php-shirabe-e5fcde172b38d9a71292d0b4dd7f5d347da2e748.zip | |
feat(command): implement --help output
Diffstat (limited to 'crates/shirabe-external-packages/src')
10 files changed, 94 insertions, 85 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs index 321b640..d98152f 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs @@ -6,6 +6,7 @@ use crate::symfony::console::completion::completion_suggestions::{ CompletionSuggestions, StringOrSuggestion, }; use crate::symfony::console::descriptor::application_description::ApplicationDescription; +use crate::symfony::console::descriptor::descriptor_interface::DescribableObject; use crate::symfony::console::helper::descriptor_helper::DescriptorHelper; use crate::symfony::console::input::input_argument::InputArgument; use crate::symfony::console::input::input_definition::DefinitionItem; @@ -137,15 +138,12 @@ impl Command for HelpCommand { self.command = Some(application.borrow_mut().find(&command_name)?); } - let helper = DescriptorHelper::new(); - // TODO: DescriptorHelper::describe2 takes the described object as Option<PhpMixed>, - // but PhpMixed cannot hold a Command. The Command/Application object mixing for - // describe needs a dedicated type (Phase C). - let object: Option<PhpMixed> = todo!(); + let mut helper = DescriptorHelper::new(); + let object = DescribableObject::Command(self.command.clone().unwrap()); let mut options = indexmap::IndexMap::new(); options.insert("format".to_string(), input.borrow().get_option("format")?); options.insert("raw_text".to_string(), input.borrow().get_option("raw")?); - let _ = helper.describe2(&mut *output.borrow_mut(), object, options); + helper.describe2(output.clone(), object, options)?; self.command = None; diff --git a/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs index 9edebe4..bfaece7 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs @@ -6,6 +6,7 @@ use crate::symfony::console::completion::completion_suggestions::{ CompletionSuggestions, StringOrSuggestion, }; use crate::symfony::console::descriptor::application_description::ApplicationDescription; +use crate::symfony::console::descriptor::descriptor_interface::DescribableObject; use crate::symfony::console::helper::descriptor_helper::DescriptorHelper; use crate::symfony::console::input::input_argument::InputArgument; use crate::symfony::console::input::input_definition::DefinitionItem; @@ -136,11 +137,8 @@ impl Command for ListCommand { input: Rc<RefCell<dyn InputInterface>>, output: Rc<RefCell<dyn OutputInterface>>, ) -> anyhow::Result<i64> { - let helper = DescriptorHelper::new(); - // TODO: DescriptorHelper::describe2 takes the described object as Option<PhpMixed>, - // but PhpMixed cannot hold an Application. The Command/Application object mixing for - // describe needs a dedicated type (Phase C). - let object: Option<PhpMixed> = todo!(); + let mut helper = DescriptorHelper::new(); + let object = DescribableObject::Application(self.get_application().unwrap()); let mut options = indexmap::IndexMap::new(); options.insert("format".to_string(), input.borrow().get_option("format")?); options.insert("raw_text".to_string(), input.borrow().get_option("raw")?); @@ -149,7 +147,7 @@ impl Command for ListCommand { input.borrow().get_argument("namespace")?, ); options.insert("short".to_string(), input.borrow().get_option("short")?); - let _ = helper.describe2(&mut *output.borrow_mut(), object, options); + helper.describe2(output.clone(), object, options)?; Ok(0) } diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs index 43b1094..38824a6 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs @@ -2,8 +2,9 @@ use crate::symfony::console::application::Application; use crate::symfony::console::command::command::Command; -use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface; -use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException; +use crate::symfony::console::descriptor::descriptor_interface::{ + DescribableObject, DescriptorInterface, +}; use crate::symfony::console::input::input_argument::InputArgument; use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_option::InputOption; @@ -20,52 +21,29 @@ pub trait Descriptor: DescriptorInterface { fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()> { self.set_output(output); - // PHP dispatches via `$object instanceof ...`. The concrete runtime type of - // `object` must be recovered to route to the correct describe* method. - match true { - // case $object instanceof InputArgument: - _ if todo!("$object instanceof InputArgument") => { - let argument: InputArgument = todo!("downcast object to InputArgument"); + // PHP dispatches via `$object instanceof ...`; the explicit `DescribableObject` enum makes + // that dispatch a `match`. + match object { + DescribableObject::InputArgument(argument) => { self.describe_input_argument(&argument, options)?; } - // case $object instanceof InputOption: - _ if todo!("$object instanceof InputOption") => { - let option: InputOption = todo!("downcast object to InputOption"); + DescribableObject::InputOption(option) => { self.describe_input_option(&option, options)?; } - // case $object instanceof InputDefinition: - _ if todo!("$object instanceof InputDefinition") => { - let definition: InputDefinition = todo!("downcast object to InputDefinition"); + DescribableObject::InputDefinition(definition) => { self.describe_input_definition(&definition, options)?; } - // case $object instanceof Command: - _ if todo!("$object instanceof Command") => { - let mut command: Box<dyn Command> = todo!("downcast object to Command"); - self.describe_command(command.as_mut(), options)?; + DescribableObject::Command(command) => { + self.describe_command(&mut *command.borrow_mut(), options)?; } - // case $object instanceof Application: - _ if todo!("$object instanceof Application") => { - let application: std::rc::Rc<std::cell::RefCell<dyn Application>> = - todo!("downcast object to Application"); + DescribableObject::Application(application) => { self.describe_application(application, options)?; } - _ => { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!( - "Object of type \"{}\" is not describable.", - shirabe_php_shim::get_debug_type(&object) - ), - code: 0, - }) - .into(), - ); - } } Ok(()) diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs index be39ec3..be0e9ff 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs @@ -1,15 +1,29 @@ //! ref: composer/vendor/symfony/console/Descriptor/DescriptorInterface.php +use crate::symfony::console::application::Application; +use crate::symfony::console::command::command::Command; +use crate::symfony::console::input::input_argument::InputArgument; +use crate::symfony::console::input::input_definition::InputDefinition; +use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::OutputInterface; use indexmap::IndexMap; use shirabe_php_shim::PhpMixed; +/// The set of objects the descriptors know how to describe. +pub enum DescribableObject { + InputArgument(InputArgument), + InputOption(InputOption), + InputDefinition(InputDefinition), + Command(std::rc::Rc<std::cell::RefCell<dyn Command>>), + Application(std::rc::Rc<std::cell::RefCell<dyn Application>>), +} + /// Descriptor interface. pub trait DescriptorInterface { fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()>; } diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs index 4793370..9068a0a 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs @@ -5,7 +5,9 @@ use crate::symfony::console::application::Application; use crate::symfony::console::command::command::Command; use crate::symfony::console::descriptor::application_description::ApplicationDescription; use crate::symfony::console::descriptor::descriptor::Descriptor; -use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface; +use crate::symfony::console::descriptor::descriptor_interface::{ + DescribableObject, DescriptorInterface, +}; use crate::symfony::console::input::input_argument::InputArgument; use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_option::InputOption; @@ -370,7 +372,7 @@ impl DescriptorInterface for JsonDescriptor { fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()> { Descriptor::describe(self, output, object, options) diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs index 698cc51..49d958f 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs @@ -5,7 +5,9 @@ use crate::symfony::console::application::Application; use crate::symfony::console::command::command::Command; use crate::symfony::console::descriptor::application_description::ApplicationDescription; use crate::symfony::console::descriptor::descriptor::Descriptor; -use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface; +use crate::symfony::console::descriptor::descriptor_interface::{ + DescribableObject, DescriptorInterface, +}; use crate::symfony::console::helper::helper::Helper; use crate::symfony::console::input::input_argument::InputArgument; use crate::symfony::console::input::input_definition::InputDefinition; @@ -26,7 +28,7 @@ impl MarkdownDescriptor { pub fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()> { let decorated = output.borrow().is_decorated(); @@ -312,7 +314,7 @@ impl DescriptorInterface for MarkdownDescriptor { fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()> { MarkdownDescriptor::describe(self, output, object, options) diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs index 915e19e..6590384 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs @@ -5,7 +5,9 @@ use crate::symfony::console::application::Application; use crate::symfony::console::command::command::Command; use crate::symfony::console::descriptor::application_description::ApplicationDescription; use crate::symfony::console::descriptor::descriptor::Descriptor; -use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface; +use crate::symfony::console::descriptor::descriptor_interface::{ + DescribableObject, DescriptorInterface, +}; use crate::symfony::console::formatter::output_formatter::OutputFormatter; use crate::symfony::console::helper::helper::Helper; use crate::symfony::console::input::input_argument::InputArgument; @@ -549,7 +551,7 @@ impl DescriptorInterface for TextDescriptor { fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()> { Descriptor::describe(self, output, object, options) diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs index 4c65893..063f279 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs @@ -4,7 +4,9 @@ use crate::symfony::console::application::Application; use crate::symfony::console::command::command::Command; use crate::symfony::console::descriptor::application_description::ApplicationDescription; use crate::symfony::console::descriptor::descriptor::Descriptor; -use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface; +use crate::symfony::console::descriptor::descriptor_interface::{ + DescribableObject, DescriptorInterface, +}; use crate::symfony::console::input::input_argument::InputArgument; use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_option::InputOption; @@ -372,7 +374,7 @@ impl DescriptorInterface for XmlDescriptor { fn describe( &mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - object: PhpMixed, + object: DescribableObject, options: IndexMap<String, PhpMixed>, ) -> anyhow::Result<()> { Descriptor::describe(self, output, object, options) diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs index a0e1b2b..521284b 100644 --- a/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs +++ b/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs @@ -1,6 +1,8 @@ //! ref: composer/vendor/symfony/console/Helper/DescriptorHelper.php -use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface; +use crate::symfony::console::descriptor::descriptor_interface::{ + DescribableObject, DescriptorInterface, +}; use crate::symfony::console::descriptor::json_descriptor::JsonDescriptor; use crate::symfony::console::descriptor::markdown_descriptor::MarkdownDescriptor; use crate::symfony::console::descriptor::text_descriptor::TextDescriptor; @@ -39,16 +41,10 @@ impl DescriptorHelper { inner: Helper::default(), descriptors: IndexMap::new(), }; - // The XML/JSON/Markdown descriptors do not yet expose a constructor - // (no `Default`/`new`); their construction is deferred until those - // descriptor types provide one. - let xml: Box<dyn DescriptorInterface> = todo!(); - let json: Box<dyn DescriptorInterface> = todo!(); - let md: Box<dyn DescriptorInterface> = todo!(); this.register("txt", Box::new(TextDescriptor::default())) - .register("xml", xml) - .register("json", json) - .register("md", md); + .register("xml", Box::new(XmlDescriptor::default())) + .register("json", Box::new(JsonDescriptor::default())) + .register("md", Box::new(MarkdownDescriptor::default())); this } @@ -60,11 +56,11 @@ impl DescriptorHelper { /// /// @throws InvalidArgumentException when the given format is not supported pub fn describe2( - &self, - output: &dyn OutputInterface, - object: Option<shirabe_php_shim::PhpMixed>, + &mut self, + output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, + object: DescribableObject, options: IndexMap<String, shirabe_php_shim::PhpMixed>, - ) -> Result<(), InvalidArgumentException> { + ) -> anyhow::Result<()> { let mut merged: IndexMap<String, shirabe_php_shim::PhpMixed> = IndexMap::new(); merged.insert( "raw_text".to_string(), @@ -85,23 +81,20 @@ impl DescriptorHelper { }; if !self.descriptors.contains_key(&format) { - return Err(InvalidArgumentException( - shirabe_php_shim::InvalidArgumentException { + return Err( + InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { message: format!( "Unsupported format \"{}\".", shirabe_php_shim::PhpMixed::String(format.clone()), ), code: 0, - }, - )); + }) + .into(), + ); } - let _ = (&self.descriptors[&format], output, object, options); - // `DescriptorInterface::describe` takes an owned - // `Rc<RefCell<dyn OutputInterface>>` and `&mut self`, while the helper - // only holds a shared `&dyn OutputInterface` and `&self`. Reconciling - // this ownership/mutability gap is a Phase C decision. - todo!("dispatch to DescriptorInterface::describe (Phase C ownership)"); + let descriptor = self.descriptors.get_mut(&format).unwrap(); + descriptor.describe(output, object, options) } /// Registers a descriptor. diff --git a/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs b/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs index 396dcf3..8917f20 100644 --- a/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs +++ b/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs @@ -6,12 +6,32 @@ pub struct UnicodeString { } impl UnicodeString { - pub fn new(_string: &str) -> Self { - todo!() + // TODO(phase-c): the real constructor runs `normalizer_normalize` (Unicode NFC normalization), + // which has no Rust std equivalent and would need a dedicated normalization implementation. + // Normalization is skipped here, which is only correct for already-NFC input such as ASCII. + pub fn new(string: &str) -> Self { + Self { + string: string.to_string(), + } } - pub fn width(&self, _ignore_unsupported_encoding: bool) -> i64 { - todo!() + // TODO(phase-c): ASCII-only provisional implementation. The faithful `width()` uses `wcswidth` + // with the Unicode width tables to treat wide characters as width 2 and skip zero-width / + // combining characters; here every character counts as width 1, correct only for ASCII. The + // ANSI/control-character stripping driven by `ignore_ansi_decoration` is likewise not handled. + pub fn width(&self, _ignore_ansi_decoration: bool) -> i64 { + let s = self.string.replace(['\x00', '\x05', '\x07'], ""); + let s = s.replace("\r\n", "\n").replace('\r', "\n"); + + let mut width: i64 = 0; + for line in s.split('\n') { + let line_width = line.chars().count() as i64; + if line_width > width { + width = line_width; + } + } + + width } pub fn length(&self) -> i64 { |
