1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use crate::prelude::*;
use nu_engine::{UnevaluatedCallInfo, WholeStreamCommand};
use nu_errors::ShellError;
use nu_protocol::{
    hir::ExternalRedirection, Primitive, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tagged;
use std::path::{Path, PathBuf};

pub struct Save;

macro_rules! process_unknown {
    ($scope:tt, $input:ident, $name_tag:ident) => {{
        if $input.len() > 0 {
            match $input[0] {
                Value {
                    value: UntaggedValue::Primitive(Primitive::Binary(_)),
                    ..
                } => process_binary!($scope, $input, $name_tag),
                _ => process_string!($scope, $input, $name_tag),
            }
        } else {
            process_string!($scope, $input, $name_tag)
        }
    }};
}

macro_rules! process_string {
    ($scope:tt, $input:ident, $name_tag:ident) => {{
        let mut result_string = String::new();
        for res in $input {
            match res {
                Value {
                    value: UntaggedValue::Primitive(Primitive::String(s)),
                    ..
                } => {
                    result_string.push_str(&s);
                }
                _ => {
                    break $scope Err(ShellError::labeled_error(
                        "Save requires string data",
                        "consider converting data to string (see `help commands`)",
                        $name_tag,
                    ));
                }
            }
        }
        Ok(result_string.into_bytes())
    }};
}

macro_rules! process_binary {
    ($scope:tt, $input:ident, $name_tag:ident) => {{
        let mut result_binary: Vec<u8> = Vec::new();
        for res in $input {
            match res {
                Value {
                    value: UntaggedValue::Primitive(Primitive::Binary(b)),
                    ..
                } => {
                    for u in b.into_iter() {
                        result_binary.push(u);
                    }
                }
                _ => {
                    break $scope Err(ShellError::labeled_error(
                        "Save could not successfully save",
                        "unexpected data during binary save",
                        $name_tag,
                    ));
                }
            }
        }
        Ok(result_binary)
    }};
}

macro_rules! process_string_return_success {
    ($scope:tt, $result_vec:ident, $name_tag:ident) => {{
        let mut result_string = String::new();
        for res in $result_vec {
            match res {
                Value {
                    value: UntaggedValue::Primitive(Primitive::String(s)),
                    ..
                } => {
                    result_string.push_str(&s);
                }
                _ => {
                    break $scope Err(ShellError::labeled_error(
                        "Save could not successfully save",
                        "unexpected data during text save",
                        $name_tag,
                    ));
                }
            }
        }
        Ok(result_string.into_bytes())
    }};
}

macro_rules! process_binary_return_success {
    ($scope:tt, $result_vec:ident, $name_tag:ident) => {{
        let mut result_binary: Vec<u8> = Vec::new();
        for res in $result_vec {
            match res {
                Value {
                    value: UntaggedValue::Primitive(Primitive::Binary(b)),
                    ..
                } => {
                    for u in b.into_iter() {
                        result_binary.push(u);
                    }
                }
                _ => {
                    break $scope Err(ShellError::labeled_error(
                        "Save could not successfully save",
                        "unexpected data during binary save",
                        $name_tag,
                    ));
                }
            }
        }
        Ok(result_binary)
    }};
}

impl WholeStreamCommand for Save {
    fn name(&self) -> &str {
        "save"
    }

    fn signature(&self) -> Signature {
        Signature::build("save")
            .optional(
                "path",
                SyntaxShape::FilePath,
                "the path to save contents to",
            )
            .switch(
                "raw",
                "treat values as-is rather than auto-converting based on file extension",
                Some('r'),
            )
    }

    fn usage(&self) -> &str {
        "Save the contents of the pipeline to a file."
    }

    fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
        save(args)
    }
}

fn save(args: CommandArgs) -> Result<OutputStream, ShellError> {
    let shell_manager = args.shell_manager();
    let mut full_path = PathBuf::from(shell_manager.path());
    let name_tag = args.call_info.name_tag.clone();
    let name = args.call_info.name_tag.clone();
    let context = args.context.clone();
    let scope = args.scope().clone();

    let head = args.call_info.args.head.clone();

    let path: Option<Tagged<PathBuf>> = args.opt(0)?;
    let save_raw = args.has_flag("raw");

    let input: Vec<Value> = args.input.collect();
    if path.is_none() {
        let mut should_return_file_path_error = true;

        // If there is no filename, check the metadata for the anchor filename
        if !input.is_empty() {
            let anchor = input[0].tag.anchor();

            if let Some(AnchorLocation::File(file)) = anchor {
                should_return_file_path_error = false;
                full_path.push(Path::new(&file));
            }
        }

        if should_return_file_path_error {
            return Err(ShellError::labeled_error(
                "Save requires a filepath",
                "needs path",
                name_tag,
            ));
        }
    } else if let Some(file) = path {
        full_path.push(file.item());
    }

    // TODO use label_break_value once it is stable:
    // https://github.com/rust-lang/rust/issues/48594
    #[allow(clippy::never_loop)]
    let content: Result<Vec<u8>, ShellError> = 'scope: loop {
        break if !save_raw {
            if let Some(extension) = full_path.extension() {
                let command_name = format!("to {}", extension.to_string_lossy());
                if let Some(converter) = scope.get_command(&command_name) {
                    let new_args = CommandArgs {
                        context,
                        call_info: UnevaluatedCallInfo {
                            args: nu_protocol::hir::Call {
                                head,
                                positional: None,
                                named: None,
                                span: Span::unknown(),
                                external_redirection: ExternalRedirection::Stdout,
                            },
                            name_tag: name_tag.clone(),
                        },
                        input: InputStream::from_stream(input.into_iter()),
                    };
                    let mut result = converter.run(new_args)?;
                    let result_vec: Vec<Value> = result.drain_vec();
                    if converter.is_binary() {
                        process_binary_return_success!('scope, result_vec, name_tag)
                    } else {
                        process_string_return_success!('scope, result_vec, name_tag)
                    }
                } else {
                    process_unknown!('scope, input, name_tag)
                }
            } else {
                process_unknown!('scope, input, name_tag)
            }
        } else {
            Ok(string_from(&input).into_bytes())
        };
    };

    shell_manager.save(&full_path, &content?, name.span)
}

fn string_from(input: &[Value]) -> String {
    let mut save_data = String::new();

    if !input.is_empty() {
        let mut first = true;
        for i in input.iter() {
            if !first {
                save_data.push('\n');
            } else {
                first = false;
            }
            if let Ok(data) = &i.as_string() {
                save_data.push_str(data);
            }
        }
    }

    save_data
}

#[cfg(test)]
mod tests {
    use super::Save;
    use super::ShellError;

    #[test]
    fn examples_work_as_expected() -> Result<(), ShellError> {
        use crate::examples::test as test_examples;

        test_examples(Save {})
    }
}