nu_command/filters/
reject.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::{DeprecationEntry, DeprecationType, ReportMode, ast::PathMember, casing::Casing};
3use std::{cmp::Reverse, collections::HashSet};
4
5#[derive(Clone)]
6pub struct Reject;
7
8impl Command for Reject {
9    fn name(&self) -> &str {
10        "reject"
11    }
12
13    fn signature(&self) -> Signature {
14        Signature::build("reject")
15            .input_output_types(vec![
16                (Type::record(), Type::record()),
17                (Type::table(), Type::table()),
18                (Type::list(Type::Any), Type::list(Type::Any)),
19            ])
20            .switch("optional", "make all cell path members optional", Some('o'))
21            .switch(
22                "ignore-errors",
23                "ignore missing data (make all cell path members optional) (deprecated)",
24                Some('i'),
25            )
26            .rest(
27                "rest",
28                SyntaxShape::CellPath,
29                "The names of columns to remove from the table.",
30            )
31            .category(Category::Filters)
32    }
33
34    fn description(&self) -> &str {
35        "Remove the given columns or rows from the table. Opposite of `select`."
36    }
37
38    fn extra_description(&self) -> &str {
39        "To remove a quantity of rows or columns, use `skip`, `drop`, or `drop column`."
40    }
41
42    fn search_terms(&self) -> Vec<&str> {
43        vec!["drop", "key"]
44    }
45
46    fn run(
47        &self,
48        engine_state: &EngineState,
49        stack: &mut Stack,
50        call: &Call,
51        input: PipelineData,
52    ) -> Result<PipelineData, ShellError> {
53        let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
54        let mut new_columns: Vec<CellPath> = vec![];
55        for col_val in columns {
56            let col_span = &col_val.span();
57            match col_val {
58                Value::CellPath { val, .. } => {
59                    new_columns.push(val);
60                }
61                Value::String { val, .. } => {
62                    let cv = CellPath {
63                        members: vec![PathMember::String {
64                            val: val.clone(),
65                            span: *col_span,
66                            optional: false,
67                            casing: Casing::Sensitive,
68                        }],
69                    };
70                    new_columns.push(cv.clone());
71                }
72                Value::Int { val, .. } => {
73                    let cv = CellPath {
74                        members: vec![PathMember::Int {
75                            val: val as usize,
76                            span: *col_span,
77                            optional: false,
78                        }],
79                    };
80                    new_columns.push(cv.clone());
81                }
82                x => {
83                    return Err(ShellError::CantConvert {
84                        to_type: "cell path".into(),
85                        from_type: x.get_type().to_string(),
86                        span: x.span(),
87                        help: None,
88                    });
89                }
90            }
91        }
92        let span = call.head;
93
94        let optional = call.has_flag(engine_state, stack, "optional")?
95            || call.has_flag(engine_state, stack, "ignore-errors")?;
96        if optional {
97            for cell_path in &mut new_columns {
98                cell_path.make_optional();
99            }
100        }
101
102        reject(engine_state, span, input, new_columns)
103    }
104
105    fn deprecation_info(&self) -> Vec<DeprecationEntry> {
106        vec![DeprecationEntry {
107            ty: DeprecationType::Flag("ignore-errors".into()),
108            report_mode: ReportMode::FirstUse,
109            since: Some("0.106.0".into()),
110            expected_removal: None,
111            help: Some(
112                "This flag has been renamed to `--optional (-o)` to better reflect its behavior."
113                    .into(),
114            ),
115        }]
116    }
117
118    fn examples(&self) -> Vec<Example> {
119        vec![
120            Example {
121                description: "Reject a column in the `ls` table",
122                example: "ls | reject modified",
123                result: None,
124            },
125            Example {
126                description: "Reject a column in a table",
127                example: "[[a, b]; [1, 2]] | reject a",
128                result: Some(Value::test_list(vec![Value::test_record(record! {
129                    "b" => Value::test_int(2),
130                })])),
131            },
132            Example {
133                description: "Reject a row in a table",
134                example: "[[a, b]; [1, 2] [3, 4]] | reject 1",
135                result: Some(Value::test_list(vec![Value::test_record(record! {
136                    "a" =>  Value::test_int(1),
137                    "b" =>  Value::test_int(2),
138                })])),
139            },
140            Example {
141                description: "Reject the specified field in a record",
142                example: "{a: 1, b: 2} | reject a",
143                result: Some(Value::test_record(record! {
144                    "b" => Value::test_int(2),
145                })),
146            },
147            Example {
148                description: "Reject a nested field in a record",
149                example: "{a: {b: 3, c: 5}} | reject a.b",
150                result: Some(Value::test_record(record! {
151                    "a" => Value::test_record(record! {
152                        "c" => Value::test_int(5),
153                    }),
154                })),
155            },
156            Example {
157                description: "Reject multiple rows",
158                example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | reject 0 2",
159                result: None,
160            },
161            Example {
162                description: "Reject multiple columns",
163                example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject type size",
164                result: Some(Value::test_list(vec![
165                    Value::test_record(record! { "name" => Value::test_string("Cargo.toml") }),
166                    Value::test_record(record! { "name" => Value::test_string("Cargo.lock") }),
167                ])),
168            },
169            Example {
170                description: "Reject multiple columns by spreading a list",
171                example: "let cols = [type size]; [[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject ...$cols",
172                result: Some(Value::test_list(vec![
173                    Value::test_record(record! { "name" => Value::test_string("Cargo.toml") }),
174                    Value::test_record(record! { "name" => Value::test_string("Cargo.lock") }),
175                ])),
176            },
177            Example {
178                description: "Reject item in list",
179                example: "[1 2 3] | reject 1",
180                result: Some(Value::test_list(vec![
181                    Value::test_int(1),
182                    Value::test_int(3),
183                ])),
184            },
185        ]
186    }
187}
188
189fn reject(
190    engine_state: &EngineState,
191    span: Span,
192    input: PipelineData,
193    cell_paths: Vec<CellPath>,
194) -> Result<PipelineData, ShellError> {
195    let mut unique_rows: HashSet<usize> = HashSet::new();
196    let metadata = input.metadata();
197    let mut new_columns = vec![];
198    let mut new_rows = vec![];
199    for column in cell_paths {
200        let CellPath { ref members } = column;
201        match members.first() {
202            Some(PathMember::Int { val, span, .. }) => {
203                if members.len() > 1 {
204                    return Err(ShellError::GenericError {
205                        error: "Reject only allows row numbers for rows".into(),
206                        msg: "extra after row number".into(),
207                        span: Some(*span),
208                        help: None,
209                        inner: vec![],
210                    });
211                }
212                if !unique_rows.contains(val) {
213                    unique_rows.insert(*val);
214                    new_rows.push(column);
215                }
216            }
217            _ => {
218                if !new_columns.contains(&column) {
219                    new_columns.push(column)
220                }
221            }
222        };
223    }
224    new_rows.sort_unstable_by_key(|k| {
225        Reverse({
226            match k.members[0] {
227                PathMember::Int { val, .. } => val,
228                PathMember::String { .. } => usize::MIN,
229            }
230        })
231    });
232
233    new_columns.append(&mut new_rows);
234
235    let has_integer_path_member = new_columns.iter().any(|path| {
236        path.members
237            .iter()
238            .any(|member| matches!(member, PathMember::Int { .. }))
239    });
240
241    match input {
242        PipelineData::ListStream(stream, ..) if !has_integer_path_member => {
243            let result = stream
244                .into_iter()
245                .map(move |mut value| {
246                    let span = value.span();
247
248                    for cell_path in new_columns.iter() {
249                        if let Err(error) = value.remove_data_at_cell_path(&cell_path.members) {
250                            return Value::error(error, span);
251                        }
252                    }
253
254                    value
255                })
256                .into_pipeline_data(span, engine_state.signals().clone());
257
258            Ok(result)
259        }
260
261        input => {
262            let mut val = input.into_value(span)?;
263
264            for cell_path in new_columns {
265                val.remove_data_at_cell_path(&cell_path.members)?;
266            }
267
268            Ok(val.into_pipeline_data_with_metadata(metadata))
269        }
270    }
271}
272
273#[cfg(test)]
274mod test {
275    #[test]
276    fn test_examples() {
277        use super::Reject;
278        use crate::test_examples;
279        test_examples(Reject {})
280    }
281}