aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-console
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe-symfony-console')
-rw-r--r--crates/shirabe-symfony-console/src/command/complete_command.rs23
-rw-r--r--crates/shirabe-symfony-console/src/command/help_command.rs10
-rw-r--r--crates/shirabe-symfony-console/src/command/list_command.rs15
-rw-r--r--crates/shirabe-symfony-console/src/completion/completion_input.rs2
-rw-r--r--crates/shirabe-symfony-console/src/input/argv_input.rs3
-rw-r--r--crates/shirabe-symfony-console/src/input/array_input.rs3
-rw-r--r--crates/shirabe-symfony-console/src/input/input.rs14
-rw-r--r--crates/shirabe-symfony-console/src/input/input_interface.rs3
-rw-r--r--crates/shirabe-symfony-console/src/input/input_option.rs87
-rw-r--r--crates/shirabe-symfony-console/src/input/string_input.rs3
10 files changed, 139 insertions, 24 deletions
diff --git a/crates/shirabe-symfony-console/src/command/complete_command.rs b/crates/shirabe-symfony-console/src/command/complete_command.rs
index dc418178..432888b6 100644
--- a/crates/shirabe-symfony-console/src/command/complete_command.rs
+++ b/crates/shirabe-symfony-console/src/command/complete_command.rs
@@ -78,19 +78,26 @@ impl CompleteCommand {
input: &dyn InputInterface,
) -> anyhow::Result<CompletionInput> {
let current_index = input.get_option("current")?;
- if !current_index.to_bool() || !shirabe_php_shim::ctype_digit(&current_index.to_string()) {
+ if !current_index.to_bool()
+ || !shirabe_php_shim::ctype_digit(current_index.as_string().unwrap_or_default())
+ {
anyhow::bail!(shirabe_php_shim::RuntimeException::new(
"The \"--current\" option must be set and it must be an integer.".to_string()
));
}
- let tokens: Vec<String> = match input.get_option("input")?.as_list() {
- Some(list) => list.iter().map(|v| v.to_string()).collect(),
- None => Vec::new(),
- };
+ let tokens: Vec<String> = input
+ .get_option("input")?
+ .as_array()
+ .map(<[String]>::to_vec)
+ .unwrap_or_default();
let mut completion_input = CompletionInput::from_tokens(
tokens,
- current_index.to_string().parse::<i64>().unwrap_or(0),
+ current_index
+ .as_string()
+ .unwrap_or_default()
+ .parse::<i64>()
+ .unwrap_or(0),
)?;
// try { $completionInput->bind(...); } catch (ExceptionInterface $e) {}
@@ -254,13 +261,13 @@ impl Command for CompleteCommand {
let completion_output = self
.completion_outputs
- .get(&shell.to_string())
+ .get(shell.as_string().unwrap_or_default())
.cloned()
.unwrap_or(PhpMixed::Bool(false));
if !completion_output.to_bool() {
anyhow::bail!(shirabe_php_shim::RuntimeException::new(format!(
"Shell completion is not supported for your shell: \"{}\" (supported: \"{}\").",
- shell,
+ shell.as_string().unwrap_or_default(),
self.completion_outputs
.keys()
.cloned()
diff --git a/crates/shirabe-symfony-console/src/command/help_command.rs b/crates/shirabe-symfony-console/src/command/help_command.rs
index 4f1447cb..2081e0e1 100644
--- a/crates/shirabe-symfony-console/src/command/help_command.rs
+++ b/crates/shirabe-symfony-console/src/command/help_command.rs
@@ -149,8 +149,14 @@ impl Command for HelpCommand {
let mut helper = DescriptorHelper::new();
let object = DescribableObject::Command(self.command.borrow().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")?);
+ options.insert(
+ "format".to_string(),
+ input.borrow().get_option("format")?.into(),
+ );
+ options.insert(
+ "raw_text".to_string(),
+ input.borrow().get_option("raw")?.into(),
+ );
helper.describe2(output.clone(), object, options)?;
*self.command.borrow_mut() = None;
diff --git a/crates/shirabe-symfony-console/src/command/list_command.rs b/crates/shirabe-symfony-console/src/command/list_command.rs
index ead84d44..6074b49a 100644
--- a/crates/shirabe-symfony-console/src/command/list_command.rs
+++ b/crates/shirabe-symfony-console/src/command/list_command.rs
@@ -147,13 +147,22 @@ impl Command for ListCommand {
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")?);
+ options.insert(
+ "format".to_string(),
+ input.borrow().get_option("format")?.into(),
+ );
+ options.insert(
+ "raw_text".to_string(),
+ input.borrow().get_option("raw")?.into(),
+ );
options.insert(
"namespace".to_string(),
input.borrow().get_argument("namespace")?,
);
- options.insert("short".to_string(), input.borrow().get_option("short")?);
+ options.insert(
+ "short".to_string(),
+ input.borrow().get_option("short")?.into(),
+ );
helper.describe2(output.clone(), object, options)?;
Ok(0)
diff --git a/crates/shirabe-symfony-console/src/completion/completion_input.rs b/crates/shirabe-symfony-console/src/completion/completion_input.rs
index e7f1e0a2..c667ef60 100644
--- a/crates/shirabe-symfony-console/src/completion/completion_input.rs
+++ b/crates/shirabe-symfony-console/src/completion/completion_input.rs
@@ -350,7 +350,7 @@ impl crate::input::InputInterface for CompletionInput {
crate::input::InputInterface::get_options(&self.inner)
}
- fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
+ fn get_option(&self, name: &str) -> anyhow::Result<crate::input::InputOptionValue> {
crate::input::InputInterface::get_option(&self.inner, name)
}
diff --git a/crates/shirabe-symfony-console/src/input/argv_input.rs b/crates/shirabe-symfony-console/src/input/argv_input.rs
index 94e5299d..6138c195 100644
--- a/crates/shirabe-symfony-console/src/input/argv_input.rs
+++ b/crates/shirabe-symfony-console/src/input/argv_input.rs
@@ -4,6 +4,7 @@ use crate::exception::RuntimeException;
use crate::input::Input;
use crate::input::InputDefinition;
use crate::input::InputInterface;
+use crate::input::InputOptionValue;
use crate::input::StreamableInputInterface;
use indexmap::IndexMap;
use shirabe_php_shim::{PhpMixed, php_regex, preg_match};
@@ -593,7 +594,7 @@ impl InputInterface for ArgvInput {
self.inner.get_options()
}
- fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
+ fn get_option(&self, name: &str) -> anyhow::Result<InputOptionValue> {
self.inner.get_option(name)
}
diff --git a/crates/shirabe-symfony-console/src/input/array_input.rs b/crates/shirabe-symfony-console/src/input/array_input.rs
index a032432b..c9bc9dce 100644
--- a/crates/shirabe-symfony-console/src/input/array_input.rs
+++ b/crates/shirabe-symfony-console/src/input/array_input.rs
@@ -5,6 +5,7 @@ use crate::exception::InvalidOptionException;
use crate::input::Input;
use crate::input::InputDefinition;
use crate::input::InputInterface;
+use crate::input::InputOptionValue;
use crate::input::StreamableInputInterface;
use indexmap::IndexMap;
use shirabe_php_shim::PhpMixed;
@@ -336,7 +337,7 @@ impl InputInterface for ArrayInput {
self.inner.get_options()
}
- fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
+ fn get_option(&self, name: &str) -> anyhow::Result<InputOptionValue> {
self.inner.get_option(name)
}
diff --git a/crates/shirabe-symfony-console/src/input/input.rs b/crates/shirabe-symfony-console/src/input/input.rs
index 88bb9f94..795e9c81 100644
--- a/crates/shirabe-symfony-console/src/input/input.rs
+++ b/crates/shirabe-symfony-console/src/input/input.rs
@@ -3,6 +3,7 @@
use crate::exception::InvalidArgumentException;
use crate::exception::RuntimeException;
use crate::input::InputDefinition;
+use crate::input::InputOptionValue;
use indexmap::IndexMap;
use shirabe_php_shim::{PhpMixed, PhpResource, php_regex, preg_is_match};
@@ -156,14 +157,14 @@ impl Input {
)
}
- pub fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
+ pub fn get_option(&self, name: &str) -> anyhow::Result<InputOptionValue> {
if self.definition.has_negation(name) {
let value = self.get_option(&self.definition.negation_to_name(name)?)?;
- if matches!(value, PhpMixed::Null) {
+ if value.is_null() {
return Ok(value);
}
- return Ok(PhpMixed::Bool(!value.as_bool().unwrap_or(false)));
+ return Ok(InputOptionValue::Bool(!value.to_bool()));
}
if !self.definition.has_option(name) {
@@ -174,10 +175,11 @@ impl Input {
.into());
}
- Ok(if self.options.contains_key(name) {
- self.options[name].clone()
+ Ok(if let Some(value) = self.options.get(name) {
+ InputOptionValue::from_php_mixed(value)
} else {
- self.definition.get_option(name)?.get_default().clone()
+ let option = self.definition.get_option(name)?;
+ InputOptionValue::from_php_mixed(option.get_default())
})
}
diff --git a/crates/shirabe-symfony-console/src/input/input_interface.rs b/crates/shirabe-symfony-console/src/input/input_interface.rs
index 4491ca8c..3c3ec2d5 100644
--- a/crates/shirabe-symfony-console/src/input/input_interface.rs
+++ b/crates/shirabe-symfony-console/src/input/input_interface.rs
@@ -1,6 +1,7 @@
//! ref: composer/vendor/symfony/console/Input/InputInterface.php
use crate::input::InputDefinition;
+use crate::input::InputOptionValue;
use crate::input::StreamableInputInterface;
use shirabe_php_shim::PhpMixed;
@@ -33,7 +34,7 @@ pub trait InputInterface: std::fmt::Debug + shirabe_php_shim::AsAny {
fn get_options(&self) -> indexmap::IndexMap<String, PhpMixed>;
- fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed>;
+ fn get_option(&self, name: &str) -> anyhow::Result<InputOptionValue>;
fn set_option(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()>;
diff --git a/crates/shirabe-symfony-console/src/input/input_option.rs b/crates/shirabe-symfony-console/src/input/input_option.rs
index 3b24d914..d37e18ae 100644
--- a/crates/shirabe-symfony-console/src/input/input_option.rs
+++ b/crates/shirabe-symfony-console/src/input/input_option.rs
@@ -191,3 +191,90 @@ impl InputOption {
&& option.is_value_optional() == self.is_value_optional()
}
}
+
+/// The `bool|string|string[]|null` domain of a parsed option value, as returned by
+/// [`InputInterface::get_option`](crate::input::InputInterface::get_option).
+#[derive(Debug, Clone, PartialEq)]
+pub enum InputOptionValue {
+ Null,
+ Bool(bool),
+ String(String),
+ Array(Vec<String>),
+}
+
+impl InputOptionValue {
+ /// Narrows a raw option value to this domain.
+ ///
+ /// TODO(type-model): `Input` keeps parsed options and `InputOption` defaults as `PhpMixed`, so
+ /// a value outside this domain — an int, a float, or an array holding one — can only be
+ /// rejected here.
+ pub(crate) fn from_php_mixed(value: &PhpMixed) -> Self {
+ match value {
+ PhpMixed::Null => Self::Null,
+ PhpMixed::Bool(b) => Self::Bool(*b),
+ PhpMixed::String(s) => Self::String(s.clone()),
+ PhpMixed::List(_) | PhpMixed::Array(_) => Self::Array(
+ value
+ .values()
+ .into_iter()
+ .map(|item| match item {
+ PhpMixed::String(s) => s.clone(),
+ other => panic!("an option array holds {:?}, not a string", other),
+ })
+ .collect(),
+ ),
+ other => panic!(
+ "an option holds {:?}, not a bool, string, array or null",
+ other
+ ),
+ }
+ }
+
+ pub fn is_null(&self) -> bool {
+ matches!(self, Self::Null)
+ }
+
+ pub fn as_bool(&self) -> Option<bool> {
+ match self {
+ Self::Bool(b) => Some(*b),
+ _ => None,
+ }
+ }
+
+ pub fn as_string(&self) -> Option<&str> {
+ match self {
+ Self::String(s) => Some(s.as_str()),
+ _ => None,
+ }
+ }
+
+ pub fn as_array(&self) -> Option<&[String]> {
+ match self {
+ Self::Array(items) => Some(items),
+ _ => None,
+ }
+ }
+
+ /// PHP loose boolean cast `(bool) $value`.
+ pub fn to_bool(&self) -> bool {
+ match self {
+ Self::Null => false,
+ Self::Bool(b) => *b,
+ Self::String(s) => !s.is_empty() && s != "0",
+ Self::Array(items) => !items.is_empty(),
+ }
+ }
+}
+
+impl From<InputOptionValue> for PhpMixed {
+ fn from(value: InputOptionValue) -> Self {
+ match value {
+ InputOptionValue::Null => PhpMixed::Null,
+ InputOptionValue::Bool(b) => PhpMixed::Bool(b),
+ InputOptionValue::String(s) => PhpMixed::String(s),
+ InputOptionValue::Array(items) => {
+ PhpMixed::List(items.into_iter().map(PhpMixed::String).collect())
+ }
+ }
+ }
+}
diff --git a/crates/shirabe-symfony-console/src/input/string_input.rs b/crates/shirabe-symfony-console/src/input/string_input.rs
index bdd21577..9fcb3fc8 100644
--- a/crates/shirabe-symfony-console/src/input/string_input.rs
+++ b/crates/shirabe-symfony-console/src/input/string_input.rs
@@ -4,6 +4,7 @@ use crate::exception::InvalidArgumentException;
use crate::input::ArgvInput;
use crate::input::InputDefinition;
use crate::input::InputInterface;
+use crate::input::InputOptionValue;
use crate::input::StreamableInputInterface;
use indexmap::IndexMap;
use shirabe_php_shim::{PhpMixed, php_regex, preg_match};
@@ -177,7 +178,7 @@ impl InputInterface for StringInput {
InputInterface::get_options(&self.inner)
}
- fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
+ fn get_option(&self, name: &str) -> anyhow::Result<InputOptionValue> {
self.inner.get_option(name)
}