From 6051927c8fa32cfffa102d2a170c5a6cf747a1b9 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 9 Aug 2026 11:19:03 +0900 Subject: refactor(symfony-console): extract symfony/console into the shirabe-symfony-console crate Move `Symfony\Component\Console` out of shirabe-external-packages and into its own crate, so the path is `shirabe_symfony_console::application::Application` instead of `shirabe_external_packages::symfony::console::application::Application`. The `delegate_to_inner!` and `delegate_command_trait_impls_to_inner!` macros move with it. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/helper/table_cell.rs | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 crates/shirabe-symfony-console/src/helper/table_cell.rs (limited to 'crates/shirabe-symfony-console/src/helper/table_cell.rs') diff --git a/crates/shirabe-symfony-console/src/helper/table_cell.rs b/crates/shirabe-symfony-console/src/helper/table_cell.rs new file mode 100644 index 00000000..100872ca --- /dev/null +++ b/crates/shirabe-symfony-console/src/helper/table_cell.rs @@ -0,0 +1,99 @@ +//! ref: composer/vendor/symfony/console/Helper/TableCell.php + +use crate::exception::invalid_argument_exception::InvalidArgumentException; +use crate::helper::table_cell_style::TableCellStyle; +use indexmap::IndexMap; + +/// A `TableCell` option value: an integer span, a `TableCellStyle`, or null. +#[derive(Debug, Clone)] +pub enum TableCellOption { + Int(i64), + Style(std::rc::Rc), + Null, +} + +#[derive(Debug, Clone)] +pub struct TableCell { + pub(crate) value: String, + options: IndexMap, +} + +impl TableCell { + pub fn new( + value: &str, + options: IndexMap, + ) -> Result { + let mut this_options: IndexMap = IndexMap::new(); + this_options.insert("rowspan".to_string(), TableCellOption::Int(1)); + this_options.insert("colspan".to_string(), TableCellOption::Int(1)); + this_options.insert("style".to_string(), TableCellOption::Null); + + // check option names + let diff: Vec = options + .keys() + .filter(|key| !this_options.contains_key(*key)) + .cloned() + .collect(); + if !diff.is_empty() { + return Err(InvalidArgumentException::new(format!( + "The TableCell does not support the following options: '{}'.", + diff.join("', '"), + ))); + } + + if let Some(style) = options.get("style") + && !matches!(style, TableCellOption::Style(_)) + && !matches!(style, TableCellOption::Null) + { + return Err(InvalidArgumentException::new( + "The style option must be an instance of \"TableCellStyle\".".to_string(), + )); + } + + for (key, option) in options { + this_options.insert(key, option); + } + + Ok(Self { + value: value.to_string(), + options: this_options, + }) + } + + /// Two-argument constructor (`__construct(string $value, array $options)`). + /// + /// The options used by the Table helper are internally controlled, so a malformed-option + /// error here would be a programming bug rather than a recoverable condition. + pub fn new2(value: &str, options: IndexMap) -> Self { + Self::new(value, options).expect("TableCell options built internally are always valid") + } + + /// Gets number of colspan. + pub fn get_colspan(&self) -> i64 { + match self.options["colspan"] { + TableCellOption::Int(colspan) => colspan, + _ => 0, + } + } + + /// Gets number of rowspan. + pub fn get_rowspan(&self) -> i64 { + match self.options["rowspan"] { + TableCellOption::Int(rowspan) => rowspan, + _ => 0, + } + } + + pub fn get_style(&self) -> Option> { + match &self.options["style"] { + TableCellOption::Style(style) => Some(style.clone()), + _ => None, + } + } +} + +impl std::fmt::Display for TableCell { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.value) + } +} -- cgit v1.3.1-4-g156e