Skip to main content

nu_command/filters/
reject.rs

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