nu_command/debug/
metadata_set.rs1use nu_engine::command_prelude::*;
2use nu_protocol::DataSource;
3
4#[derive(Clone)]
5pub struct MetadataSet;
6
7impl Command for MetadataSet {
8 fn name(&self) -> &str {
9 "metadata set"
10 }
11
12 fn description(&self) -> &str {
13 "Set the metadata for items in the stream."
14 }
15
16 fn signature(&self) -> nu_protocol::Signature {
17 Signature::build("metadata set")
18 .input_output_types(vec![(Type::Any, Type::Any)])
19 .switch(
20 "datasource-ls",
21 "Assign the DataSource::Ls metadata to the input",
22 Some('l'),
23 )
24 .named(
25 "datasource-filepath",
26 SyntaxShape::Filepath,
27 "Assign the DataSource::FilePath metadata to the input",
28 Some('f'),
29 )
30 .named(
31 "content-type",
32 SyntaxShape::String,
33 "Assign content type metadata to the input",
34 Some('c'),
35 )
36 .allow_variants_without_examples(true)
37 .category(Category::Debug)
38 }
39
40 fn run(
41 &self,
42 engine_state: &EngineState,
43 stack: &mut Stack,
44 call: &Call,
45 mut input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 let head = call.head;
48 let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
49 let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
50 let content_type: Option<String> = call.get_flag(engine_state, stack, "content-type")?;
51
52 let mut metadata = match &mut input {
53 PipelineData::Value(_, metadata)
54 | PipelineData::ListStream(_, metadata)
55 | PipelineData::ByteStream(_, metadata) => metadata.take().unwrap_or_default(),
56 PipelineData::Empty => return Err(ShellError::PipelineEmpty { dst_span: head }),
57 };
58
59 if let Some(content_type) = content_type {
60 metadata.content_type = Some(content_type);
61 }
62
63 match (ds_fp, ds_ls) {
64 (Some(path), false) => metadata.data_source = DataSource::FilePath(path.into()),
65 (None, true) => metadata.data_source = DataSource::Ls,
66 (Some(_), true) => {
67 return Err(ShellError::IncompatibleParameters {
68 left_message: "cannot use `--datasource-filepath`".into(),
69 left_span: call
70 .get_flag_span(stack, "datasource-filepath")
71 .expect("has flag"),
72 right_message: "with `--datasource-ls`".into(),
73 right_span: call
74 .get_flag_span(stack, "datasource-ls")
75 .expect("has flag"),
76 });
77 }
78 (None, false) => (),
79 }
80
81 Ok(input.set_metadata(Some(metadata)))
82 }
83
84 fn examples(&self) -> Vec<Example> {
85 vec![
86 Example {
87 description: "Set the metadata of a table literal",
88 example: "[[name color]; [Cargo.lock '#ff0000'] [Cargo.toml '#00ff00'] [README.md '#0000ff']] | metadata set --datasource-ls",
89 result: None,
90 },
91 Example {
92 description: "Set the metadata of a file path",
93 example: "'crates' | metadata set --datasource-filepath $'(pwd)/crates'",
94 result: None,
95 },
96 Example {
97 description: "Set the metadata of a file path",
98 example: "'crates' | metadata set --content-type text/plain | metadata | get content_type",
99 result: Some(Value::test_string("text/plain")),
100 },
101 ]
102 }
103}
104
105#[cfg(test)]
106mod test {
107 use crate::{Metadata, test_examples_with_commands};
108
109 use super::*;
110
111 #[test]
112 fn test_examples() {
113 test_examples_with_commands(MetadataSet {}, &[&Metadata {}])
114 }
115}