nushell/crates/nu-cmd-lang/src/core_commands
JT 2c3aade057
Add pattern matching (#8590)
# 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
> ```bash
> 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](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-03-24 14:52:01 +13:00
..
overlay Revert "Allow NU_LIBS_DIR and friends to be const" (#8501) 2023-03-17 09:33:24 -05:00
alias.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
break_.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
commandline.rs fix: fix commandline when called with no arguments (#8207) 2023-03-16 17:45:35 -05:00
const_.rs Switch let/let-env family to init with math expressions (#8545) 2023-03-23 09:14:10 +13:00
continue_.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
def_env.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
def.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
describe.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
do_.rs Restrict closure expression to be something like {|| ...} (#8290) 2023-03-17 07:36:28 -05:00
echo.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
error_make.rs FIX: do not allow *start > end* in error make spans (#8570) 2023-03-23 20:31:06 +01:00
export_alias.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
export_def_env.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
export_def.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
export_extern.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
export_use.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
export.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
extern_.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
for_.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
help_aliases.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
help_commands.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
help_externs.rs Added help externs command (#8403) 2023-03-15 18:40:27 +01:00
help_modules.rs Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
help_operators.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
help.rs Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
hide_env.rs Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
hide.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
if_.rs Switch let/let-env family to init with math expressions (#8545) 2023-03-23 09:14:10 +13:00
ignore.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
let_.rs Switch let/let-env family to init with math expressions (#8545) 2023-03-23 09:14:10 +13:00
loop_.rs Cratification: Break out nu_cmd_lang into a separate crate (#8181) 2023-02-24 09:54:42 -06:00
match_.rs Add pattern matching (#8590) 2023-03-24 14:52:01 +13:00
mod.rs Add pattern matching (#8590) 2023-03-24 14:52:01 +13:00
module.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
mut_.rs Switch let/let-env family to init with math expressions (#8545) 2023-03-23 09:14:10 +13:00
register.rs Clarify how register works (#8583) 2023-03-23 13:02:22 -05:00
return_.rs Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
try_.rs Restrict closure expression to be something like {|| ...} (#8290) 2023-03-17 07:36:28 -05:00
use_.rs Revert "Allow NU_LIBS_DIR and friends to be const" (#8501) 2023-03-17 09:33:24 -05:00
version.rs Fix the SQLite feature name in version (#8381) 2023-03-09 20:49:29 -08:00
while_.rs Switch let/let-env family to init with math expressions (#8545) 2023-03-23 09:14:10 +13:00