nu_command/filters/
wrap.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct Wrap;
5
6impl Command for Wrap {
7    fn name(&self) -> &str {
8        "wrap"
9    }
10
11    fn description(&self) -> &str {
12        "Wrap the value into a column."
13    }
14
15    fn signature(&self) -> nu_protocol::Signature {
16        Signature::build("wrap")
17            .input_output_types(vec![
18                (Type::List(Box::new(Type::Any)), Type::table()),
19                (Type::Range, Type::table()),
20                (Type::Any, Type::record()),
21            ])
22            .required("name", SyntaxShape::String, "The name of the column.")
23            .allow_variants_without_examples(true)
24            .category(Category::Filters)
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        let span = call.head;
35        let name: String = call.req(engine_state, stack, 0)?;
36        let metadata = input.metadata();
37
38        match input {
39            PipelineData::Empty => Ok(PipelineData::empty()),
40            PipelineData::Value(Value::Range { .. }, ..)
41            | PipelineData::Value(Value::List { .. }, ..)
42            | PipelineData::ListStream { .. } => Ok(input
43                .into_iter()
44                .map(move |x| Value::record(record! { name.clone() => x }, span))
45                .into_pipeline_data_with_metadata(span, engine_state.signals().clone(), metadata)),
46            PipelineData::ByteStream(stream, ..) => Ok(Value::record(
47                record! { name => stream.into_value()? },
48                span,
49            )
50            .into_pipeline_data_with_metadata(metadata)),
51            PipelineData::Value(input, ..) => Ok(Value::record(record! { name => input }, span)
52                .into_pipeline_data_with_metadata(metadata)),
53        }
54    }
55
56    fn examples(&self) -> Vec<Example<'_>> {
57        vec![
58            Example {
59                description: "Wrap a list into a table with a given column name",
60                example: "[ Pachisi Mahjong Catan Carcassonne ] | wrap game",
61                result: Some(Value::test_list(vec![
62                    Value::test_record(record! {
63                        "game" => Value::test_string("Pachisi"),
64                    }),
65                    Value::test_record(record! {
66                        "game" => Value::test_string("Mahjong"),
67                    }),
68                    Value::test_record(record! {
69                        "game" => Value::test_string("Catan"),
70                    }),
71                    Value::test_record(record! {
72                        "game" => Value::test_string("Carcassonne"),
73                    }),
74                ])),
75            },
76            Example {
77                description: "Wrap a range into a table with a given column name",
78                example: "4..6 | wrap num",
79                result: Some(Value::test_list(vec![
80                    Value::test_record(record! {
81                        "num" => Value::test_int(4),
82                    }),
83                    Value::test_record(record! {
84                        "num" => Value::test_int(5),
85                    }),
86                    Value::test_record(record! {
87                        "num" => Value::test_int(6),
88                    }),
89                ])),
90            },
91        ]
92    }
93}
94
95#[cfg(test)]
96mod test {
97    #[test]
98    fn test_examples() {
99        use super::Wrap;
100        use crate::test_examples;
101        test_examples(Wrap {})
102    }
103}