Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
redox-os
ion
Commits
02cba084
Unverified
Commit
02cba084
authored
Jan 13, 2020
by
Tom Almeida
Browse files
lib/shell/flow: change match expressions to match based on regex
parent
843f59ef
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/lib/shell/flow.rs
View file @
02cba084
...
...
@@ -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
<
T
:
AsRef
<
str
>>
(
&
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
{
...
...
tests/match.ion
View file @
02cba084
...
...
@@ -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!"
...
...
tests/match.out
View file @
02cba084
...
...
@@ -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)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment