From 02cba084dcb18bc7cecf03c4bfee5b60bd55cedb Mon Sep 17 00:00:00 2001 From: Tom Almeida Date: Mon, 13 Jan 2020 09:16:12 +1100 Subject: [PATCH] lib/shell/flow: change match expressions to match based on regex --- src/lib/shell/flow.rs | 15 ++++++++------- tests/match.ion | 10 ++++++++++ tests/match.out | 2 ++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib/shell/flow.rs b/src/lib/shell/flow.rs index 1337fa9b..ff34e735 100644 --- a/src/lib/shell/flow.rs +++ b/src/lib/shell/flow.rs @@ -492,6 +492,7 @@ impl<'a> Shell<'a> { /// Expand an expression and run a branch based on the value of the /// expanded expression fn execute_match>(&mut self, expression: T, cases: &[Case<'a>]) -> Result { + use regex::RegexSet; // Logic for determining if the LHS of a match-case construct (the value we are // matching against) matches the RHS of a match-case construct (a value // in a case statement). For example, checking to see if the value @@ -502,13 +503,13 @@ impl<'a> Shell<'a> { let is_array = is_array(expression.as_ref()); let value = self.expand_string(expression.as_ref())?; for case in cases.iter() { - if case - .value - .as_ref() - .and_then(|v| self.expand_string(v).ok()) - .filter(|v| v.iter().all(|v| !value.contains(v))) - .is_none() - { + if value.iter().all(|value| { + case.value + .as_ref() + .and_then(|v| self.expand_string(v).ok()) + .and_then(|v| RegexSet::new(v).ok()) + .map_or(false, |regex| regex.is_match(value)) + }) { // let pattern_is_array = is_array(&value); let previous_bind = case.binding.as_ref().and_then(|bind| { if is_array { diff --git a/tests/match.ion b/tests/match.ion index 7c4ecd46..d2ae63b3 100644 --- a/tests/match.ion +++ b/tests/match.ion @@ -18,6 +18,16 @@ end analyze $out1 analyze $out2 +fn analyze_regex output + match output + case ".*application/x-gzip"; echo "Use tar -xzf" + case _ ; echo "Unknown file type" + end +end + +analyze $out1 +analyze $out2 + fn wildcard input match $input case _; echo "WILDCARD!" diff --git a/tests/match.out b/tests/match.out index f967baa4..afc241b2 100644 --- a/tests/match.out +++ b/tests/match.out @@ -6,6 +6,8 @@ Odd! Even! Use tar -xzf Unknown file type +Use tar -xzf +Unknown file type WILDCARD! WILDCARD! Almost half full (or half empty) -- GitLab