nu_cmd_lang/core_commands/
match_.rs1use nu_engine::command_prelude::*;
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct Match;
6
7impl Command for Match {
8 fn name(&self) -> &str {
9 "match"
10 }
11
12 fn description(&self) -> &str {
13 "Conditionally run a block on a matched value."
14 }
15
16 fn signature(&self) -> nu_protocol::Signature {
17 Signature::build("match")
18 .input_output_types(vec![(Type::Any, Type::Any)])
19 .required("value", SyntaxShape::Any, "Value to check.")
20 .required(
21 "match_block",
22 SyntaxShape::MatchBlock,
23 "Block to run if check succeeds.",
24 )
25 .category(Category::Core)
26 }
27
28 fn extra_description(&self) -> &str {
29 "This command is a parser keyword. For details, check:
30 https://www.nushell.sh/book/thinking_in_nu.html"
31 }
32
33 fn command_type(&self) -> CommandType {
34 CommandType::Keyword
35 }
36
37 fn run(
38 &self,
39 _engine_state: &EngineState,
40 _stack: &mut Stack,
41 _call: &Call,
42 _input: PipelineData,
43 ) -> Result<PipelineData, ShellError> {
44 eprintln!(
47 "Tried to execute 'run' for the 'match' command: this code path should never be reached in IR mode"
48 );
49 unreachable!()
50 }
51
52 fn examples(&self) -> Vec<Example<'_>> {
53 vec![
54 Example {
55 description: "Match on a value.",
56 example: "match 3 { 1 => 'one', 2 => 'two', 3 => 'three' }",
57 result: Some(Value::test_string("three")),
58 },
59 Example {
60 description: "Match against alternative values.",
61 example: "match 'three' { 1 | 'one' => '-', 2 | 'two' => '--', 3 | 'three' => '---' }",
62 result: Some(Value::test_string("---")),
63 },
64 Example {
65 description: "Match on a value in range.",
66 example: "match 3 { 1..10 => 'yes!' }",
67 result: Some(Value::test_string("yes!")),
68 },
69 Example {
70 description: "Match on a constant expression.",
71 example: "match 42 { (40 + 2) => 'yes!' }",
72 result: Some(Value::test_string("yes!")),
73 },
74 Example {
75 description: "Match on a field in a record.",
76 example: "match {a: 100} { {a: $my_value} => { $my_value } }",
77 result: Some(Value::test_int(100)),
78 },
79 Example {
80 description: "Match with a catch-all.",
81 example: "match 3 { 1 => { 'yes!' }, _ => { 'no!' } }",
82 result: Some(Value::test_string("no!")),
83 },
84 Example {
85 description: "Match against a list.",
86 example: "match [1, 2, 3] { [$a, $b, $c] => { $a + $b + $c }, _ => 0 }",
87 result: Some(Value::test_int(6)),
88 },
89 Example {
90 description: "Match against pipeline input.",
91 example: "{a: {b: 3}} | match $in {{a: { $b }} => ($b + 10) }",
92 result: Some(Value::test_int(13)),
93 },
94 Example {
95 description: "Match with a guard.",
96 example: "match [1 2 3] {
97 [$x, ..$y] if $x == 1 => { 'good list' },
98 _ => { 'not a very good list' }
99 }
100 ",
101 result: Some(Value::test_string("good list")),
102 },
103 ]
104 }
105}
106
107#[cfg(test)]
108mod test {
109 use super::*;
110
111 #[test]
112 fn test_examples() -> nu_test_support::Result {
113 nu_test_support::test().examples(Match)
114 }
115}