Skip to content

Add pattern matching - #8590

Merged
sophiajt merged 15 commits into
nushell:mainfrom
sophiajt:patterns
Mar 24, 2023
Merged

sophiajt merged 15 commits into
nushell:mainfrom
sophiajt:patterns

Conversation

@sophiajt

@sophiajt sophiajt commented Mar 24, 2023 •

Copy link
Copy Markdown
Contributor

Description

This adds match and basic pattern matching.

An example:

match $x {
  1..10 => { print "Value is between 1 and 10" }
  { foo: $bar } => { print $"Value has a 'foo' field with value ($bar)" }
  [$a, $b] => { print $"Value is a list with two items: ($a) and ($b)" }
  _ => { print "Value is none of the above" }
}

Like the recent changes to if to allow it to be used as an expression, match can also be used as an expression. This allows you to assign the result to a variable, eg) let xyz = match ...

I've also included a short-hand pattern for matching records, as I think it might help when doing a lot of record patterns: {$foo} which is equivalent to {foo: $foo}.

There are still missing components, so consider this the first step in full pattern matching support. Currently missing:

  • Patterns for strings
  • Or-patterns (like the | in Rust)
  • Patterns for tables (unclear how we want to match a table, so it'll need some design)
  • Patterns for binary values
  • And much more

User-Facing Changes

[see above]

Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace to check that all tests pass

Note
from nushell you can also use the toolkit as follows

use toolkit.nu  # or use an `env_change` hook to activate it automatically
toolkit check pr

After Submitting

If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.

@codecov

codecov Bot commented Mar 24, 2023

Copy link
Copy Markdown

Codecov Report

Merging #8590 (be69964) into main (be52f7f) will decrease coverage by 0.11%.
The diff coverage is 53.08%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8590      +/-   ##
==========================================
- Coverage   68.56%   68.45%   -0.11%     
==========================================
  Files         624      628       +4     
  Lines      100844   101551     +707     
==========================================
+ Hits        69143    69517     +374     
- Misses      31701    32034     +333     
Impacted Files Coverage Δ
crates/nu-cli/src/syntax_highlight.rs 65.57% <0.00%> (-0.59%) ⬇️
...es/nu-command/src/database/commands/into_sqlite.rs 23.62% <0.00%> (-0.11%) ⬇️
crates/nu-command/src/debug/explain.rs 8.56% <0.00%> (-0.03%) ⬇️
crates/nu-command/src/filters/find.rs 76.80% <0.00%> (-0.31%) ⬇️
crates/nu-command/src/formats/from/nuon.rs 34.68% <0.00%> (-0.91%) ⬇️
crates/nu-command/src/formats/to/json.rs 76.33% <0.00%> (ø)
crates/nu-command/src/formats/to/nuon.rs 86.42% <0.00%> (-1.90%) ⬇️
crates/nu-command/src/formats/to/text.rs 76.03% <0.00%> (-0.64%) ⬇️
crates/nu-command/src/formats/to/toml.rs 74.07% <0.00%> (-0.40%) ⬇️
crates/nu-command/src/formats/to/yaml.rs 63.00% <0.00%> (-0.64%) ⬇️
... and 16 more

... and 1 file with indirect coverage changes

@sophiajt
sophiajt merged commit 2c3aade into nushell:main Mar 24, 2023
@sophiajt
sophiajt deleted the patterns branch March 24, 2023 01:52
@fdncred

fdncred commented Mar 24, 2023

Copy link
Copy Markdown
Contributor

This match is such a wow command 😍 that we've been missing. Great add @jntrnr!!!!!

sholderbach pushed a commit that referenced this pull request Mar 26, 2023
Should close #8616.
Related to #8590.

# Description
With the new builtin `match` command introduced in the `rust` source in
#8590, there is no need to have a custom `match` in the standard
library.
This PR removes the `match` command from `std.nu` and the associated
test.

# User-Facing Changes
Users can not access `match` from `std.nu`.

# Tests + Formatting
```
nu crates/nu-utils/standard_library/tests.nu  --path crates/nu-utils/standard_library/ out+err> /dev/null; ($env.LAST_EXIT_CODE == 0)
```
gives `true`

# After Submitting
```
$nothing
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants