2019-11-11 11:54:58 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::ReaderBuilder;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, TaggedDictBuilder, UntaggedValue, Value};
|
2019-11-11 11:54:58 +00:00
|
|
|
|
2019-11-19 15:13:10 +00:00
|
|
|
fn from_delimited_string_to_value(
|
2019-11-11 11:54:58 +00:00
|
|
|
s: String,
|
|
|
|
headerless: bool,
|
|
|
|
separator: char,
|
|
|
|
tag: impl Into<Tag>,
|
2019-11-21 14:33:14 +00:00
|
|
|
) -> Result<Value, csv::Error> {
|
2019-11-11 11:54:58 +00:00
|
|
|
let mut reader = ReaderBuilder::new()
|
|
|
|
.has_headers(!headerless)
|
|
|
|
.delimiter(separator as u8)
|
|
|
|
.from_reader(s.as_bytes());
|
|
|
|
let tag = tag.into();
|
|
|
|
|
|
|
|
let headers = if headerless {
|
|
|
|
(1..=reader.headers()?.len())
|
|
|
|
.map(|i| format!("Column{}", i))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
} else {
|
|
|
|
reader.headers()?.iter().map(String::from).collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut rows = vec![];
|
|
|
|
for row in reader.records() {
|
|
|
|
let mut tagged_row = TaggedDictBuilder::new(&tag);
|
|
|
|
for (value, header) in row?.iter().zip(headers.iter()) {
|
2019-11-21 14:33:14 +00:00
|
|
|
tagged_row.insert_value(
|
2019-11-11 11:54:58 +00:00
|
|
|
header,
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(value))).into_value(&tag),
|
2019-11-11 11:54:58 +00:00
|
|
|
)
|
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
rows.push(tagged_row.into_value());
|
2019-11-11 11:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
Ok(UntaggedValue::Table(rows).into_value(&tag))
|
2019-11-11 11:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 15:13:10 +00:00
|
|
|
pub fn from_delimited_data(
|
2019-11-11 11:54:58 +00:00
|
|
|
headerless: bool,
|
|
|
|
sep: char,
|
|
|
|
format_name: &'static str,
|
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let name_tag = name;
|
|
|
|
|
|
|
|
let stream = async_stream! {
|
2020-03-06 16:06:39 +00:00
|
|
|
let concat_string = input.collect_string(name_tag.clone()).await?;
|
2019-11-11 11:54:58 +00:00
|
|
|
|
2020-03-06 16:06:39 +00:00
|
|
|
match from_delimited_string_to_value(concat_string.item, headerless, sep, name_tag.clone()) {
|
2019-11-11 11:54:58 +00:00
|
|
|
Ok(x) => match x {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value { value: UntaggedValue::Table(list), .. } => {
|
2019-11-11 11:54:58 +00:00
|
|
|
for l in list {
|
|
|
|
yield ReturnSuccess::value(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => yield ReturnSuccess::value(x),
|
|
|
|
},
|
2020-03-06 16:06:39 +00:00
|
|
|
Err(_) => {
|
2019-11-11 11:54:58 +00:00
|
|
|
let line_one = format!("Could not parse as {}", format_name);
|
|
|
|
let line_two = format!("input cannot be parsed as {}", format_name);
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
line_one,
|
|
|
|
line_two,
|
|
|
|
name_tag.clone(),
|
|
|
|
"value originates from here",
|
2020-03-06 16:06:39 +00:00
|
|
|
concat_string.tag,
|
2019-11-11 11:54:58 +00:00
|
|
|
))
|
|
|
|
} ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|