nu_command/filters/
is_empty.rs

1use crate::filters::empty::empty;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct IsEmpty;
6
7impl Command for IsEmpty {
8    fn name(&self) -> &str {
9        "is-empty"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("is-empty")
14            .input_output_types(vec![(Type::Any, Type::Bool)])
15            .rest(
16                "rest",
17                SyntaxShape::CellPath,
18                "The names of the columns to check emptiness.",
19            )
20            .category(Category::Filters)
21    }
22
23    fn description(&self) -> &str {
24        "Check for empty values."
25    }
26
27    fn run(
28        &self,
29        engine_state: &EngineState,
30        stack: &mut Stack,
31        call: &Call,
32        input: PipelineData,
33    ) -> Result<PipelineData, ShellError> {
34        empty(engine_state, stack, call, input, false)
35    }
36
37    fn examples(&self) -> Vec<Example> {
38        vec![
39            Example {
40                description: "Check if a string is empty",
41                example: "'' | is-empty",
42                result: Some(Value::test_bool(true)),
43            },
44            Example {
45                description: "Check if a list is empty",
46                example: "[] | is-empty",
47                result: Some(Value::test_bool(true)),
48            },
49            Example {
50                // TODO: revisit empty cell path semantics for a record.
51                description: "Check if more than one column are empty",
52                example: "[[meal size]; [arepa small] [taco '']] | is-empty meal size",
53                result: Some(Value::test_bool(false)),
54            },
55        ]
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_examples() {
65        use crate::test_examples;
66
67        test_examples(IsEmpty {})
68    }
69}