diff --git a/crates/nu-cli/src/commands/mv.rs b/crates/nu-cli/src/commands/mv.rs index 7386bc82bb..03b3dbe70e 100644 --- a/crates/nu-cli/src/commands/mv.rs +++ b/crates/nu-cli/src/commands/mv.rs @@ -43,7 +43,7 @@ impl WholeStreamCommand for Move { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - mv(args, registry) + mv(args, registry).await } fn examples(&self) -> Vec { @@ -67,20 +67,13 @@ impl WholeStreamCommand for Move { } } -fn mv(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn mv(args: CommandArgs, registry: &CommandRegistry) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let name = args.call_info.name_tag.clone(); - let shell_manager = args.shell_manager.clone(); - let (args, _) = args.process(®istry).await?; - let mut result = shell_manager.mv(args, name)?; + let name = args.call_info.name_tag.clone(); + let shell_manager = args.shell_manager.clone(); + let (args, _) = args.process(®istry).await?; - while let Some(item) = result.next().await { - yield item; - } - }; - - Ok(stream.to_output_stream()) + shell_manager.mv(args, name) } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/reject.rs b/crates/nu-cli/src/commands/reject.rs index e94a6f6115..a2858a6735 100644 --- a/crates/nu-cli/src/commands/reject.rs +++ b/crates/nu-cli/src/commands/reject.rs @@ -31,7 +31,7 @@ impl WholeStreamCommand for Reject { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - reject(args, registry) + reject(args, registry).await } fn examples(&self) -> Vec { @@ -43,28 +43,24 @@ impl WholeStreamCommand for Reject { } } -fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let name = args.call_info.name_tag.clone(); - let (RejectArgs { rest: fields }, mut input) = args.process(®istry).await?; - if fields.is_empty() { - yield Err(ShellError::labeled_error( - "Reject requires fields", - "needs parameter", - name, - )); - return; - } + let name = args.call_info.name_tag.clone(); + let (RejectArgs { rest: fields }, input) = args.process(®istry).await?; - let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); + if fields.is_empty() { + return Err(ShellError::labeled_error( + "Reject requires fields", + "needs parameter", + name, + )); + } - while let Some(item) = input.next().await { - yield ReturnSuccess::value(reject_fields(&item, &fields, &item.tag)); - } - }; + let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); - Ok(stream.to_output_stream()) + Ok(input + .map(move |item| ReturnSuccess::value(reject_fields(&item, &fields, &item.tag))) + .to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/save.rs b/crates/nu-cli/src/commands/save.rs index a0ef140c32..0b520eef03 100644 --- a/crates/nu-cli/src/commands/save.rs +++ b/crates/nu-cli/src/commands/save.rs @@ -154,11 +154,14 @@ impl WholeStreamCommand for Save { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - save(args, registry) + save(args, registry).await } } -fn save(raw_args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn save( + raw_args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let mut full_path = PathBuf::from(raw_args.shell_manager.path()); let name_tag = raw_args.call_info.name_tag.clone(); let name = raw_args.call_info.name_tag.clone(); @@ -169,101 +172,96 @@ fn save(raw_args: CommandArgs, registry: &CommandRegistry) -> Result = input.collect().await; - if path.is_none() { - // If there is no filename, check the metadata for the anchor filename - if input.len() > 0 { - let anchor = input[0].tag.anchor(); - match anchor { - Some(path) => match path { - AnchorLocation::File(file) => { - full_path.push(Path::new(&file)); - } - _ => { - yield Err(ShellError::labeled_error( - "Save requires a filepath", - "needs path", - name_tag.clone(), - )); - } - }, - None => { - yield Err(ShellError::labeled_error( - "Save requires a filepath", - "needs path", - name_tag.clone(), - )); - } + let head = raw_args.call_info.args.head.clone(); + let ( + SaveArgs { + path, + raw: save_raw, + }, + input, + ) = raw_args.process(®istry).await?; + let input: Vec = input.collect().await; + 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(path) = anchor { + if let AnchorLocation::File(file) = path { + should_return_file_path_error = false; + full_path.push(Path::new(&file)); } - } else { - yield Err(ShellError::labeled_error( - "Save requires a filepath", - "needs path", - name_tag.clone(), - )); - } - } 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 - let content : Result, 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) = registry.get_command(&command_name) { - let new_args = RawCommandArgs { - host, - ctrl_c, - current_errors, - shell_manager, - call_info: UnevaluatedCallInfo { - args: nu_protocol::hir::Call { - head, - positional: None, - named: None, - span: Span::unknown(), - is_last: false, - }, - name_tag: name_tag.clone(), - scope, - } - }; - let mut result = converter.run(new_args.with_input(input), ®istry).await; - let result_vec: Vec> = result.drain_vec().await; - if converter.is_binary() { - process_binary_return_success!('scope, result_vec, name_tag) - } else { - process_string_return_success!('scope, result_vec, name_tag) - } + if should_return_file_path_error { + return Err(ShellError::labeled_error( + "Save requires a filepath", + "needs path", + name_tag.clone(), + )); + } + } 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, 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) = registry.get_command(&command_name) { + let new_args = RawCommandArgs { + host, + ctrl_c, + current_errors, + shell_manager, + call_info: UnevaluatedCallInfo { + args: nu_protocol::hir::Call { + head, + positional: None, + named: None, + span: Span::unknown(), + is_last: false, + }, + name_tag: name_tag.clone(), + scope, + }, + }; + let mut result = converter.run(new_args.with_input(input), ®istry).await; + let result_vec: Vec> = + result.drain_vec().await; + if converter.is_binary() { + process_binary_return_success!('scope, result_vec, name_tag) } else { - process_unknown!('scope, input, name_tag) + process_string_return_success!('scope, result_vec, name_tag) } } else { process_unknown!('scope, input, name_tag) } } else { - Ok(string_from(&input).into_bytes()) - }; + process_unknown!('scope, input, name_tag) + } + } else { + Ok(string_from(&input).into_bytes()) }; - - match content { - Ok(save_data) => match std::fs::write(full_path, save_data) { - Ok(o) => o, - Err(e) => yield Err(ShellError::labeled_error(e.to_string(), "IO error while saving", name)), - }, - Err(e) => yield Err(e), - } - }; - Ok(OutputStream::new(stream)) + match content { + Ok(save_data) => match std::fs::write(full_path, save_data) { + Ok(_) => Ok(OutputStream::empty()), + Err(e) => Err(ShellError::labeled_error( + e.to_string(), + "IO error while saving", + name, + )), + }, + Err(e) => Err(e), + } } fn string_from(input: &[Value]) -> String { diff --git a/crates/nu-cli/src/commands/touch.rs b/crates/nu-cli/src/commands/touch.rs index aeab415601..0bd7fe549d 100644 --- a/crates/nu-cli/src/commands/touch.rs +++ b/crates/nu-cli/src/commands/touch.rs @@ -33,7 +33,7 @@ impl WholeStreamCommand for Touch { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - touch(args, registry) + touch(args, registry).await } fn examples(&self) -> Vec { @@ -45,21 +45,18 @@ impl WholeStreamCommand for Touch { } } -fn touch(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn touch(args: CommandArgs, registry: &CommandRegistry) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let (TouchArgs { target }, _) = args.process(®istry).await?; - match OpenOptions::new().write(true).create(true).open(&target) { - Ok(_) => {}, - Err(err) => yield Err(ShellError::labeled_error( - "File Error", - err.to_string(), - &target.tag, - )), - } - }; + let (TouchArgs { target }, _) = args.process(®istry).await?; - Ok(stream.to_output_stream()) + match OpenOptions::new().write(true).create(true).open(&target) { + Ok(_) => Ok(OutputStream::empty()), + Err(err) => Err(ShellError::labeled_error( + "File Error", + err.to_string(), + &target.tag, + )), + } } #[cfg(test)]