diff options
Diffstat (limited to 'crates/shirabe')
| -rw-r--r-- | crates/shirabe/src/command/exec_command.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/command/package_discovery_trait.rs | 4 | ||||
| -rw-r--r-- | crates/shirabe/src/command/run_script_command.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/command/update_command.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/io/buffer_io.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/src/io/console_io.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/io/io_interface.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/io/null_io.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/tests/common/io_mock.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/tests/common/io_stub.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/tests/io/console_io_test.rs | 5 | ||||
| -rw-r--r-- | crates/shirabe/tests/io/null_io_test.rs | 5 |
12 files changed, 43 insertions, 17 deletions
diff --git a/crates/shirabe/src/command/exec_command.rs b/crates/shirabe/src/command/exec_command.rs index 3eaee5a9..057c9eb2 100644 --- a/crates/shirabe/src/command/exec_command.rs +++ b/crates/shirabe/src/command/exec_command.rs @@ -118,7 +118,12 @@ impl Command for ExecCommand { let io = self.get_io(); let binary = io.select( "Binary to run: ".to_string(), - binaries.clone(), + PhpMixed::List( + binaries + .iter() + .map(|b| PhpMixed::String(b.clone())) + .collect(), + ), PhpMixed::String(String::new()), PhpMixed::Int(1), "Invalid binary name \"%s\"".to_string(), diff --git a/crates/shirabe/src/command/package_discovery_trait.rs b/crates/shirabe/src/command/package_discovery_trait.rs index 830f1996..2ab1d9d5 100644 --- a/crates/shirabe/src/command/package_discovery_trait.rs +++ b/crates/shirabe/src/command/package_discovery_trait.rs @@ -689,7 +689,9 @@ pub trait PackageDiscoveryTrait: BaseCommand { "<error>Could not find package {}.</error>\nPick one of these or leave empty to abort:", name, ), - similar.to_vec(), + PhpMixed::List( + similar.iter().map(|s| PhpMixed::String(s.clone())).collect(), + ), PhpMixed::Bool(false), PhpMixed::Int(1), "No package named \"%s\" is installed.".to_string(), diff --git a/crates/shirabe/src/command/run_script_command.rs b/crates/shirabe/src/command/run_script_command.rs index c256eea2..26e73d6d 100644 --- a/crates/shirabe/src/command/run_script_command.rs +++ b/crates/shirabe/src/command/run_script_command.rs @@ -209,7 +209,12 @@ impl Command for RunScriptCommand { let io = self.get_io(); let script = io.select( "Script to run: ".to_string(), - options.keys().cloned().collect(), + PhpMixed::List( + options + .keys() + .map(|k| PhpMixed::String(k.clone())) + .collect(), + ), PhpMixed::String(String::new()), PhpMixed::Int(1), "Invalid script name \"%s\"".to_string(), diff --git a/crates/shirabe/src/command/update_command.rs b/crates/shirabe/src/command/update_command.rs index b47c51d0..a20d288f 100644 --- a/crates/shirabe/src/command/update_command.rs +++ b/crates/shirabe/src/command/update_command.rs @@ -705,10 +705,12 @@ impl UpdateCommand { let select_result = io.select( "Select packages: (Select more than one value separated by comma) ".to_string(), - autocompleter_values - .keys() - .cloned() - .collect::<Vec<String>>(), + PhpMixed::List( + autocompleter_values + .keys() + .map(|k| PhpMixed::String(k.clone())) + .collect(), + ), PhpMixed::Bool(false), PhpMixed::Int(1), "No package named \"%s\" is installed.".to_string(), 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, diff --git a/crates/shirabe/tests/common/io_mock.rs b/crates/shirabe/tests/common/io_mock.rs index 47f4b66b..a658c5bc 100644 --- a/crates/shirabe/tests/common/io_mock.rs +++ b/crates/shirabe/tests/common/io_mock.rs @@ -258,7 +258,7 @@ impl IOInterfaceImmutable for IOMock { fn select( &self, question: String, - choices: Vec<String>, + choices: PhpMixed, default: PhpMixed, attempts: PhpMixed, error_message: String, diff --git a/crates/shirabe/tests/common/io_stub.rs b/crates/shirabe/tests/common/io_stub.rs index d582447c..b897305e 100644 --- a/crates/shirabe/tests/common/io_stub.rs +++ b/crates/shirabe/tests/common/io_stub.rs @@ -246,7 +246,7 @@ impl IOInterfaceImmutable for IOStub { fn select( &self, _question: String, - _choices: Vec<String>, + _choices: PhpMixed, default: PhpMixed, _attempts: PhpMixed, _error_message: String, diff --git a/crates/shirabe/tests/io/console_io_test.rs b/crates/shirabe/tests/io/console_io_test.rs index a924f0c7..03307e51 100644 --- a/crates/shirabe/tests/io/console_io_test.rs +++ b/crates/shirabe/tests/io/console_io_test.rs @@ -236,7 +236,10 @@ fn test_select() { let (console_io, _output) = make_console_io_with_answer("item2\n"); let result = console_io.select( "Select item".to_string(), - vec!["item1".to_string(), "item2".to_string()], + PhpMixed::List(vec![ + PhpMixed::String("item1".to_string()), + PhpMixed::String("item2".to_string()), + ]), PhpMixed::String("item1".to_string()), PhpMixed::Bool(false), "Error message".to_string(), diff --git a/crates/shirabe/tests/io/null_io_test.rs b/crates/shirabe/tests/io/null_io_test.rs index 75678b90..1627e91f 100644 --- a/crates/shirabe/tests/io/null_io_test.rs +++ b/crates/shirabe/tests/io/null_io_test.rs @@ -79,7 +79,10 @@ fn test_select() { PhpMixed::String("1".to_string()), io.select( "question".to_string(), - vec!["item1".to_string(), "item2".to_string()], + PhpMixed::List(vec![ + PhpMixed::String("item1".to_string()), + PhpMixed::String("item2".to_string()), + ]), PhpMixed::String("1".to_string()), PhpMixed::Int(2), "foo".to_string(), |
