aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/io
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-07-20 05:55:46 +0900
committernsfisis <nsfisis@gmail.com>2026-07-20 05:55:46 +0900
commit7f14217abbba2f89ae5845d63c8f97b606354f45 (patch)
treea000f7297f8aff509b74ce25ff871b334bcfc191 /crates/shirabe/src/io
parentb151ceaa798a7d2e9b55ffbc4e4b10ea8f0a59a8 (diff)
downloadphp-shirabe-7f14217abbba2f89ae5845d63c8f97b606354f45.tar.gz
php-shirabe-7f14217abbba2f89ae5845d63c8f97b606354f45.tar.zst
php-shirabe-7f14217abbba2f89ae5845d63c8f97b606354f45.zip
fix(io): widen IOInterface::select choices to PhpMixed for assoc arrays
PHP's IOInterface::select accepts an associative choices array whose keys are the selectable values, but the port narrowed it to Vec<String>, making key-based selection unrepresentable. Accept PhpMixed (List or Array) like PHP's array $choices; ConsoleIO already branched on both shapes internally. Also mirror PHP in the single-select array_search fallback for numeric-keyed arrays. All call sites keep their previous list-based behavior. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/io')
-rw-r--r--crates/shirabe/src/io/buffer_io.rs2
-rw-r--r--crates/shirabe/src/io/console_io.rs7
-rw-r--r--crates/shirabe/src/io/io_interface.rs7
-rw-r--r--crates/shirabe/src/io/null_io.rs2
4 files changed, 12 insertions, 6 deletions
diff --git a/crates/shirabe/src/io/buffer_io.rs b/crates/shirabe/src/io/buffer_io.rs
index 0aaa5e07..f00ebe25 100644
--- a/crates/shirabe/src/io/buffer_io.rs
+++ b/crates/shirabe/src/io/buffer_io.rs
@@ -208,7 +208,7 @@ impl crate::io::IOInterfaceImmutable for BufferIO {
fn select(
&self,
question: String,
- choices: Vec<String>,
+ choices: PhpMixed,
default: PhpMixed,
attempts: PhpMixed,
error_message: String,
diff --git a/crates/shirabe/src/io/console_io.rs b/crates/shirabe/src/io/console_io.rs
index fe796d9e..911e7242 100644
--- a/crates/shirabe/src/io/console_io.rs
+++ b/crates/shirabe/src/io/console_io.rs
@@ -538,13 +538,12 @@ impl IOInterfaceImmutable for ConsoleIO {
fn select(
&self,
question: String,
- choices: Vec<String>,
+ choices: PhpMixed,
default: PhpMixed,
attempts: PhpMixed,
error_message: String,
multiselect: bool,
) -> PhpMixed {
- let choices: PhpMixed = PhpMixed::List(choices.into_iter().map(PhpMixed::String).collect());
let sanitized_question = Self::sanitize(PhpMixed::String(question), true)
.as_string()
.unwrap_or("")
@@ -604,6 +603,10 @@ impl IOInterfaceImmutable for ConsoleIO {
.enumerate()
.filter_map(|(i, v)| v.as_string().map(|s| (i.to_string(), s.to_string())))
.collect(),
+ PhpMixed::Array(a) => a
+ .iter()
+ .filter_map(|(k, v)| v.as_string().map(|s| (k.clone(), s.to_string())))
+ .collect(),
_ => IndexMap::new(),
};
return PhpMixed::String(array_search(&result_str, &haystack).unwrap_or_default());
diff --git a/crates/shirabe/src/io/io_interface.rs b/crates/shirabe/src/io/io_interface.rs
index 96bc1980..33a2f017 100644
--- a/crates/shirabe/src/io/io_interface.rs
+++ b/crates/shirabe/src/io/io_interface.rs
@@ -106,10 +106,13 @@ pub trait IOInterfaceImmutable: std::fmt::Debug {
fn ask_and_hide_answer(&self, question: String) -> Option<String>;
+ /// PHP `array $choices` may be a list (`PhpMixed::List`) or an associative
+ /// array (`PhpMixed::Array`); list choices resolve to their index, while
+ /// associative choices resolve to their key.
fn select(
&self,
question: String,
- choices: Vec<String>,
+ choices: PhpMixed,
default: PhpMixed,
attempts: PhpMixed,
error_message: String,
@@ -301,7 +304,7 @@ impl IOInterfaceImmutable for std::rc::Rc<std::cell::RefCell<dyn IOInterface>> {
fn select(
&self,
question: String,
- choices: Vec<String>,
+ choices: PhpMixed,
default: PhpMixed,
attempts: PhpMixed,
error_message: String,
diff --git a/crates/shirabe/src/io/null_io.rs b/crates/shirabe/src/io/null_io.rs
index 60970247..71a81408 100644
--- a/crates/shirabe/src/io/null_io.rs
+++ b/crates/shirabe/src/io/null_io.rs
@@ -90,7 +90,7 @@ impl IOInterfaceImmutable for NullIO {
fn select(
&self,
_question: String,
- _choices: Vec<String>,
+ _choices: PhpMixed,
default: PhpMixed,
_attempts: PhpMixed,
_error_message: String,