From ec7ff5960d290b8d2426f36125a8cb98c2d551f5 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Mon, 8 Jun 2020 00:48:10 -0400 Subject: [PATCH] Remove async_stream! from some commands (#1951) * Remove async_stream! from open.rs * Ran rustfmt * Fix Clippy warning * Removed async_stream! from evaluate_by.rs * Removed async_stream! from exit.rs * Removed async_stream! from from_eml.rs * Removed async_stream! from group_by_date.rs * Removed async_stream! from group_by.rs * Removed async_stream! from map_max.rs * Removed async_stream! from to_sqlite.rs * Removed async_stream! from to_md.rs * Removed async_stream! from to_html.rs --- crates/nu-cli/src/commands/evaluate_by.rs | 45 +++--- crates/nu-cli/src/commands/exit.rs | 21 +-- crates/nu-cli/src/commands/from_eml.rs | 64 ++++---- crates/nu-cli/src/commands/group_by.rs | 39 +++-- crates/nu-cli/src/commands/group_by_date.rs | 75 +++++---- crates/nu-cli/src/commands/map_max_by.rs | 43 +++--- crates/nu-cli/src/commands/open.rs | 46 +++--- crates/nu-cli/src/commands/to_html.rs | 160 ++++++++++---------- crates/nu-cli/src/commands/to_md.rs | 75 ++++----- crates/nu-cli/src/commands/to_sqlite.rs | 37 +++-- 10 files changed, 306 insertions(+), 299 deletions(-) diff --git a/crates/nu-cli/src/commands/evaluate_by.rs b/crates/nu-cli/src/commands/evaluate_by.rs index 12e4d72b59..6db406724f 100644 --- a/crates/nu-cli/src/commands/evaluate_by.rs +++ b/crates/nu-cli/src/commands/evaluate_by.rs @@ -37,42 +37,37 @@ impl WholeStreamCommand for EvaluateBy { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - evaluate_by(args, registry) + evaluate_by(args, registry).await } } -pub fn evaluate_by( +pub async fn evaluate_by( args: CommandArgs, registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let name = args.call_info.name_tag.clone(); - let (EvaluateByArgs { evaluate_with }, mut input) = args.process(®istry).await?; - let values: Vec = input.collect().await; + let name = args.call_info.name_tag.clone(); + let (EvaluateByArgs { evaluate_with }, mut input) = args.process(®istry).await?; + let values: Vec = input.collect().await; - if values.is_empty() { - yield Err(ShellError::labeled_error( - "Expected table from pipeline", - "requires a table input", - name - )) + if values.is_empty() { + Err(ShellError::labeled_error( + "Expected table from pipeline", + "requires a table input", + name, + )) + } else { + let evaluate_with = if let Some(evaluator) = evaluate_with { + Some(evaluator.item().clone()) } else { + None + }; - let evaluate_with = if let Some(evaluator) = evaluate_with { - Some(evaluator.item().clone()) - } else { - None - }; - - match evaluate(&values[0], evaluate_with, name) { - Ok(evaluated) => yield ReturnSuccess::value(evaluated), - Err(err) => yield Err(err) - } + match evaluate(&values[0], evaluate_with, name) { + Ok(evaluated) => Ok(OutputStream::one(ReturnSuccess::value(evaluated))), + Err(err) => Err(err), } - }; - - Ok(stream.to_output_stream()) + } } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/exit.rs b/crates/nu-cli/src/commands/exit.rs index 1463dec3be..1d451addc0 100644 --- a/crates/nu-cli/src/commands/exit.rs +++ b/crates/nu-cli/src/commands/exit.rs @@ -25,7 +25,7 @@ impl WholeStreamCommand for Exit { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - exit(args, registry) + exit(args, registry).await } fn examples(&self) -> Vec { @@ -44,19 +44,20 @@ impl WholeStreamCommand for Exit { } } -pub fn exit(args: CommandArgs, registry: &CommandRegistry) -> Result { +pub async fn exit( + args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let args = args.evaluate_once(®istry).await?; + let args = args.evaluate_once(®istry).await?; - if args.call_info.args.has("now") { - yield Ok(ReturnSuccess::Action(CommandAction::Exit)); - } else { - yield Ok(ReturnSuccess::Action(CommandAction::LeaveShell)); - } + let command_action = if args.call_info.args.has("now") { + CommandAction::Exit + } else { + CommandAction::LeaveShell }; - Ok(stream.to_output_stream()) + Ok(OutputStream::one(ReturnSuccess::action(command_action))) } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/from_eml.rs b/crates/nu-cli/src/commands/from_eml.rs index 2b479472ce..a896f291f7 100644 --- a/crates/nu-cli/src/commands/from_eml.rs +++ b/crates/nu-cli/src/commands/from_eml.rs @@ -40,7 +40,7 @@ impl WholeStreamCommand for FromEML { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - from_eml(args, registry) + from_eml(args, registry).await } } @@ -77,46 +77,54 @@ fn headerfieldvalue_to_value(tag: &Tag, value: &HeaderFieldValue) -> UntaggedVal } } -fn from_eml(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn from_eml( + args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let tag = args.call_info.name_tag.clone(); let registry = registry.clone(); - let stream = async_stream! { - let (eml_args, mut input): (FromEMLArgs, _) = args.process(®istry).await?; - let value = input.collect_string(tag.clone()).await?; + let (eml_args, input): (FromEMLArgs, _) = args.process(®istry).await?; + let value = input.collect_string(tag.clone()).await?; - let body_preview = eml_args.preview_body.map(|b| b.item).unwrap_or(DEFAULT_BODY_PREVIEW); + let body_preview = eml_args + .preview_body + .map(|b| b.item) + .unwrap_or(DEFAULT_BODY_PREVIEW); - let eml = EmlParser::from_string(value.item) + let eml = EmlParser::from_string(value.item) .with_body_preview(body_preview) .parse() - .map_err(|_| ShellError::labeled_error("Could not parse .eml file", "could not parse .eml file", &tag))?; + .map_err(|_| { + ShellError::labeled_error( + "Could not parse .eml file", + "could not parse .eml file", + &tag, + ) + })?; - let mut dict = TaggedDictBuilder::new(&tag); + let mut dict = TaggedDictBuilder::new(&tag); - if let Some(subj) = eml.subject { - dict.insert_untagged("Subject", UntaggedValue::string(subj)); - } + if let Some(subj) = eml.subject { + dict.insert_untagged("Subject", UntaggedValue::string(subj)); + } - if let Some(from) = eml.from { - dict.insert_untagged("From", headerfieldvalue_to_value(&tag, &from)); - } + if let Some(from) = eml.from { + dict.insert_untagged("From", headerfieldvalue_to_value(&tag, &from)); + } - if let Some(to) = eml.to { - dict.insert_untagged("To", headerfieldvalue_to_value(&tag, &to)); - } + if let Some(to) = eml.to { + dict.insert_untagged("To", headerfieldvalue_to_value(&tag, &to)); + } - for HeaderField{ name, value } in eml.headers.iter() { - dict.insert_untagged(name, headerfieldvalue_to_value(&tag, &value)); - } + for HeaderField { name, value } in eml.headers.iter() { + dict.insert_untagged(name, headerfieldvalue_to_value(&tag, &value)); + } - if let Some(body) = eml.body { - dict.insert_untagged("Body", UntaggedValue::string(body)); - } + if let Some(body) = eml.body { + dict.insert_untagged("Body", UntaggedValue::string(body)); + } - yield ReturnSuccess::value(dict.into_value()); - }; - - Ok(stream.to_output_stream()) + Ok(OutputStream::one(ReturnSuccess::value(dict.into_value()))) } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/group_by.rs b/crates/nu-cli/src/commands/group_by.rs index b26830655c..4b63ed8233 100644 --- a/crates/nu-cli/src/commands/group_by.rs +++ b/crates/nu-cli/src/commands/group_by.rs @@ -35,7 +35,7 @@ impl WholeStreamCommand for GroupBy { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - group_by(args, registry) + group_by(args, registry).await } fn examples(&self) -> Vec { @@ -71,30 +71,27 @@ impl WholeStreamCommand for GroupBy { } } -pub fn group_by(args: CommandArgs, registry: &CommandRegistry) -> Result { +pub async fn group_by( + args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let registry = registry.clone(); let name = args.call_info.name_tag.clone(); - let stream = async_stream! { - let (GroupByArgs { column_name }, mut input) = args.process(®istry).await?; - let values: Vec = input.collect().await; - - if values.is_empty() { - yield Err(ShellError::labeled_error( - "Expected table from pipeline", - "requires a table input", - name - )) - } else { - - match crate::utils::data::group(column_name, &values, None, &name) { - Ok(grouped) => yield ReturnSuccess::value(grouped), - Err(err) => yield Err(err), - } + let (GroupByArgs { column_name }, input) = args.process(®istry).await?; + let values: Vec = input.collect().await; + if values.is_empty() { + Err(ShellError::labeled_error( + "Expected table from pipeline", + "requires a table input", + name, + )) + } else { + match crate::utils::data::group(column_name, &values, None, &name) { + Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), + Err(err) => Err(err), } - }; - - Ok(stream.to_output_stream()) + } } pub fn group( diff --git a/crates/nu-cli/src/commands/group_by_date.rs b/crates/nu-cli/src/commands/group_by_date.rs index b36ab63dac..f361deb207 100644 --- a/crates/nu-cli/src/commands/group_by_date.rs +++ b/crates/nu-cli/src/commands/group_by_date.rs @@ -42,7 +42,7 @@ impl WholeStreamCommand for GroupByDate { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - group_by_date(args, registry) + group_by_date(args, registry).await } fn examples(&self) -> Vec { @@ -58,50 +58,59 @@ enum Grouper { ByDate(Option), } -pub fn group_by_date( +pub async fn group_by_date( args: CommandArgs, registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); let name = args.call_info.name_tag.clone(); - let stream = async_stream! { - let (GroupByDateArgs { column_name, format }, mut input) = args.process(®istry).await?; - let values: Vec = input.collect().await; + let ( + GroupByDateArgs { + column_name, + format, + }, + input, + ) = args.process(®istry).await?; + let values: Vec = input.collect().await; - if values.is_empty() { - yield Err(ShellError::labeled_error( - "Expected table from pipeline", - "requires a table input", - name - )) + if values.is_empty() { + Err(ShellError::labeled_error( + "Expected table from pipeline", + "requires a table input", + name, + )) + } else { + let grouper = if let Some(Tagged { item: fmt, tag: _ }) = format { + Grouper::ByDate(Some(fmt)) } else { + Grouper::ByDate(None) + }; - let grouper = if let Some(Tagged { item: fmt, tag }) = format { - Grouper::ByDate(Some(fmt)) - } else { - Grouper::ByDate(None) - }; - - match grouper { - Grouper::ByDate(None) => { - match crate::utils::data::group(column_name, &values, Some(Box::new(|row: &Value| row.format("%Y-%b-%d"))), &name) { - Ok(grouped) => yield ReturnSuccess::value(grouped), - Err(err) => yield Err(err), - } + match grouper { + Grouper::ByDate(None) => { + match crate::utils::data::group( + column_name, + &values, + Some(Box::new(|row: &Value| row.format("%Y-%b-%d"))), + &name, + ) { + Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), + Err(err) => Err(err), } - Grouper::ByDate(Some(fmt)) => { - match crate::utils::data::group(column_name, &values, Some(Box::new(move |row: &Value| { - row.format(&fmt) - })), &name) { - Ok(grouped) => yield ReturnSuccess::value(grouped), - Err(err) => yield Err(err), - } + } + Grouper::ByDate(Some(fmt)) => { + match crate::utils::data::group( + column_name, + &values, + Some(Box::new(move |row: &Value| row.format(&fmt))), + &name, + ) { + Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), + Err(err) => Err(err), } } } - }; - - Ok(stream.to_output_stream()) + } } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/map_max_by.rs b/crates/nu-cli/src/commands/map_max_by.rs index 836041ab09..a8664ab724 100644 --- a/crates/nu-cli/src/commands/map_max_by.rs +++ b/crates/nu-cli/src/commands/map_max_by.rs @@ -38,42 +38,37 @@ impl WholeStreamCommand for MapMaxBy { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - map_max_by(args, registry) + map_max_by(args, registry).await } } -pub fn map_max_by( +pub async fn map_max_by( args: CommandArgs, registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); let name = args.call_info.name_tag.clone(); - let stream = async_stream! { - let (MapMaxByArgs { column_name }, mut input) = args.process(®istry).await?; - let values: Vec = input.collect().await; + let (MapMaxByArgs { column_name }, mut input) = args.process(®istry).await?; + let values: Vec = input.collect().await; - if values.is_empty() { - yield Err(ShellError::labeled_error( - "Expected table from pipeline", - "requires a table input", - name - )) + if values.is_empty() { + Err(ShellError::labeled_error( + "Expected table from pipeline", + "requires a table input", + name, + )) + } else { + let map_by_column = if let Some(column_to_map) = column_name { + Some(column_to_map.item().clone()) } else { + None + }; - let map_by_column = if let Some(column_to_map) = column_name { - Some(column_to_map.item().clone()) - } else { - None - }; - - match map_max(&values[0], map_by_column, name) { - Ok(table_maxed) => yield ReturnSuccess::value(table_maxed), - Err(err) => yield Err(err) - } + match map_max(&values[0], map_by_column, name) { + Ok(table_maxed) => Ok(OutputStream::one(ReturnSuccess::value(table_maxed))), + Err(err) => Err(err), } - }; - - Ok(stream.to_output_stream()) + } } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/open.rs b/crates/nu-cli/src/commands/open.rs index 0be878b33f..ce7e52f2ab 100644 --- a/crates/nu-cli/src/commands/open.rs +++ b/crates/nu-cli/src/commands/open.rs @@ -42,7 +42,7 @@ impl WholeStreamCommand for Open { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - open(args, registry) + open(args, registry).await } fn examples(&self) -> Vec { @@ -54,39 +54,33 @@ impl WholeStreamCommand for Open { } } -fn open(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn open(args: CommandArgs, registry: &CommandRegistry) -> Result { let cwd = PathBuf::from(args.shell_manager.path()); let full_path = cwd; let registry = registry.clone(); - let stream = async_stream! { - let (OpenArgs { path, raw }, _) = args.process(®istry).await?; - let result = fetch(&full_path, &path.item, path.tag.span).await; + let (OpenArgs { path, raw }, _) = args.process(®istry).await?; + let result = fetch(&full_path, &path.item, path.tag.span).await; - if let Err(e) = result { - yield Err(e); - return; - } - let (file_extension, contents, contents_tag) = result?; + let (file_extension, contents, contents_tag) = result?; - let file_extension = if raw.item { - None - } else { - // If the extension could not be determined via mimetype, try to use the path - // extension. Some file types do not declare their mimetypes (such as bson files). - file_extension.or(path.extension().map(|x| x.to_string_lossy().to_string())) - }; - - let tagged_contents = contents.into_value(&contents_tag); - - if let Some(extension) = file_extension { - yield Ok(ReturnSuccess::Action(CommandAction::AutoConvert(tagged_contents, extension))) - } else { - yield ReturnSuccess::value(tagged_contents); - } + let file_extension = if raw.item { + None + } else { + // If the extension could not be determined via mimetype, try to use the path + // extension. Some file types do not declare their mimetypes (such as bson files). + file_extension.or_else(|| path.extension().map(|x| x.to_string_lossy().to_string())) }; - Ok(stream.to_output_stream()) + let tagged_contents = contents.into_value(&contents_tag); + + if let Some(extension) = file_extension { + Ok(OutputStream::one(ReturnSuccess::action( + CommandAction::AutoConvert(tagged_contents, extension), + ))) + } else { + Ok(OutputStream::one(ReturnSuccess::value(tagged_contents))) + } } pub async fn fetch( diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index df460ceb80..c6de43506d 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -27,98 +27,106 @@ impl WholeStreamCommand for ToHTML { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - to_html(args, registry) + to_html(args, registry).await } } -fn to_html(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn to_html( + args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let args = args.evaluate_once(®istry).await?; - let name_tag = args.name_tag(); - let input: Vec = args.input.collect().await; - let headers = nu_protocol::merge_descriptors(&input); - let mut output_string = "".to_string(); + let args = args.evaluate_once(®istry).await?; + let name_tag = args.name_tag(); + let input: Vec = args.input.collect().await; + let headers = nu_protocol::merge_descriptors(&input); + let mut output_string = "".to_string(); - if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { - output_string.push_str(""); + if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { + output_string.push_str("
"); - output_string.push_str(""); - for header in &headers { - output_string.push_str(""); - } - output_string.push_str(""); + output_string.push_str(""); + for header in &headers { + output_string.push_str(""); } + output_string.push_str(""); + } - for row in input { - match row.value { - UntaggedValue::Primitive(Primitive::Binary(b)) => { - // This might be a bit much, but it's fun :) - match row.tag.anchor { - Some(AnchorLocation::Url(f)) | - Some(AnchorLocation::File(f)) => { - let extension = f.split('.').last().map(String::from); - match extension { - Some(s) if ["png", "jpg", "bmp", "gif", "tiff", "jpeg"].contains(&s.to_lowercase().as_str()) => { - output_string.push_str(""); - } - _ => {} + for row in input { + match row.value { + UntaggedValue::Primitive(Primitive::Binary(b)) => { + // This might be a bit much, but it's fun :) + match row.tag.anchor { + Some(AnchorLocation::Url(f)) | Some(AnchorLocation::File(f)) => { + let extension = f.split('.').last().map(String::from); + match extension { + Some(s) + if ["png", "jpg", "bmp", "gif", "tiff", "jpeg"] + .contains(&s.to_lowercase().as_str()) => + { + output_string.push_str(""); } + _ => {} } - _ => {} } - } - UntaggedValue::Primitive(Primitive::String(ref b)) => { - // This might be a bit much, but it's fun :) - match row.tag.anchor { - Some(AnchorLocation::Url(f)) | - Some(AnchorLocation::File(f)) => { - let extension = f.split('.').last().map(String::from); - match extension { - Some(s) if s.to_lowercase() == "svg" => { - output_string.push_str(""); - continue; - } - _ => {} - } - } - _ => {} - } - output_string.push_str(&(htmlescape::encode_minimal(&format_leaf(&row.value).plain_string(100_000)).replace("\n", "
"))); - } - UntaggedValue::Row(row) => { - output_string.push_str(""); - for header in &headers { - let data = row.get_data(header); - output_string.push_str(""); - } - output_string.push_str(""); - } - p => { - output_string.push_str(&(htmlescape::encode_minimal(&format_leaf(&p).plain_string(100_000)).replace("\n", "
"))); + _ => {} } } + UntaggedValue::Primitive(Primitive::String(ref b)) => { + // This might be a bit much, but it's fun :) + match row.tag.anchor { + Some(AnchorLocation::Url(f)) | Some(AnchorLocation::File(f)) => { + let extension = f.split('.').last().map(String::from); + match extension { + Some(s) if s.to_lowercase() == "svg" => { + output_string.push_str(""); + continue; + } + _ => {} + } + } + _ => {} + } + output_string.push_str( + &(htmlescape::encode_minimal(&format_leaf(&row.value).plain_string(100_000)) + .replace("\n", "
")), + ); + } + UntaggedValue::Row(row) => { + output_string.push_str(""); + for header in &headers { + let data = row.get_data(header); + output_string.push_str(""); + } + output_string.push_str(""); + } + p => { + output_string.push_str( + &(htmlescape::encode_minimal(&format_leaf(&p).plain_string(100_000)) + .replace("\n", "
")), + ); + } } + } - if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { - output_string.push_str("
"); - output_string.push_str(&htmlescape::encode_minimal(&header)); - output_string.push_str("
"); + output_string.push_str(&htmlescape::encode_minimal(&header)); + output_string.push_str("
"); - output_string.push_str(&format_leaf(data.borrow()).plain_string(100_000)); - output_string.push_str("
"); + output_string.push_str(&format_leaf(data.borrow()).plain_string(100_000)); + output_string.push_str("
"); - } - output_string.push_str(""); + if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { + output_string.push_str(""); + } + output_string.push_str(""); - yield ReturnSuccess::value(UntaggedValue::string(output_string).into_value(name_tag)); - }; - - Ok(stream.to_output_stream()) + Ok(OutputStream::one(ReturnSuccess::value( + UntaggedValue::string(output_string).into_value(name_tag), + ))) } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/to_md.rs b/crates/nu-cli/src/commands/to_md.rs index 81e400a639..587390abc3 100644 --- a/crates/nu-cli/src/commands/to_md.rs +++ b/crates/nu-cli/src/commands/to_md.rs @@ -26,55 +26,58 @@ impl WholeStreamCommand for ToMarkdown { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - to_html(args, registry) + to_html(args, registry).await } } -fn to_html(args: CommandArgs, registry: &CommandRegistry) -> Result { +async fn to_html( + args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let args = args.evaluate_once(®istry).await?; - let name_tag = args.name_tag(); - let input: Vec = args.input.collect().await; - let headers = nu_protocol::merge_descriptors(&input); - let mut output_string = String::new(); + let args = args.evaluate_once(®istry).await?; + let name_tag = args.name_tag(); + let input: Vec = args.input.collect().await; + let headers = nu_protocol::merge_descriptors(&input); + let mut output_string = String::new(); - if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { + if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { + output_string.push_str("|"); + for header in &headers { + output_string.push_str(&htmlescape::encode_minimal(&header)); output_string.push_str("|"); - for header in &headers { - output_string.push_str(&htmlescape::encode_minimal(&header)); - output_string.push_str("|"); - } - output_string.push_str("\n|"); - for _ in &headers { - output_string.push_str("-"); - output_string.push_str("|"); - } - output_string.push_str("\n"); } + output_string.push_str("\n|"); + for _ in &headers { + output_string.push_str("-"); + output_string.push_str("|"); + } + output_string.push_str("\n"); + } - for row in input { - match row.value { - UntaggedValue::Row(row) => { + for row in input { + match row.value { + UntaggedValue::Row(row) => { + output_string.push_str("|"); + for header in &headers { + let data = row.get_data(header); + output_string.push_str(&format_leaf(data.borrow()).plain_string(100_000)); output_string.push_str("|"); - for header in &headers { - let data = row.get_data(header); - output_string.push_str(&format_leaf(data.borrow()).plain_string(100_000)); - output_string.push_str("|"); - } - output_string.push_str("\n"); - } - p => { - output_string.push_str(&(htmlescape::encode_minimal(&format_leaf(&p).plain_string(100_000)))); - output_string.push_str("\n"); } + output_string.push_str("\n"); + } + p => { + output_string.push_str( + &(htmlescape::encode_minimal(&format_leaf(&p).plain_string(100_000))), + ); + output_string.push_str("\n"); } } + } - yield ReturnSuccess::value(UntaggedValue::string(output_string).into_value(name_tag)); - }; - - Ok(stream.to_output_stream()) + Ok(OutputStream::one(ReturnSuccess::value( + UntaggedValue::string(output_string).into_value(name_tag), + ))) } #[cfg(test)] diff --git a/crates/nu-cli/src/commands/to_sqlite.rs b/crates/nu-cli/src/commands/to_sqlite.rs index 012661a74d..9e6805c5fa 100644 --- a/crates/nu-cli/src/commands/to_sqlite.rs +++ b/crates/nu-cli/src/commands/to_sqlite.rs @@ -27,7 +27,7 @@ impl WholeStreamCommand for ToSQLite { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - to_sqlite(args, registry) + to_sqlite(args, registry).await } fn is_binary(&self) -> bool { @@ -56,7 +56,7 @@ impl WholeStreamCommand for ToDB { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - to_sqlite(args, registry) + to_sqlite(args, registry).await } fn is_binary(&self) -> bool { @@ -203,26 +203,23 @@ fn sqlite_input_stream_to_bytes(values: Vec) -> Result Result { +async fn to_sqlite( + args: CommandArgs, + registry: &CommandRegistry, +) -> Result { let registry = registry.clone(); - let stream = async_stream! { - let args = args.evaluate_once(®istry).await?; - let name_tag = args.name_tag(); - let input: Vec = args.input.collect().await; + let args = args.evaluate_once(®istry).await?; + let name_tag = args.name_tag(); + let input: Vec = args.input.collect().await; - match sqlite_input_stream_to_bytes(input) { - Ok(out) => yield ReturnSuccess::value(out), - _ => { - yield Err(ShellError::labeled_error( - "Expected a table with SQLite-compatible structure from pipeline", - "requires SQLite-compatible input", - name_tag, - )) - }, - } - }; - - Ok(stream.to_output_stream()) + match sqlite_input_stream_to_bytes(input) { + Ok(out) => Ok(OutputStream::one(ReturnSuccess::value(out))), + _ => Err(ShellError::labeled_error( + "Expected a table with SQLite-compatible structure from pipeline", + "requires SQLite-compatible input", + name_tag, + )), + } } #[cfg(test)]