2019-11-11 11:54:58 +00:00
|
|
|
use crate::prelude::*;
|
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;
|
2020-03-16 20:50:45 +00:00
|
|
|
use nu_parser::hir::syntax_shape::{ExpandContext, SignatureRegistry};
|
|
|
|
use nu_parser::utils::{parse_line_with_separator as parse, LineSeparatedShape};
|
|
|
|
use nu_parser::TokensIterator;
|
|
|
|
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
|
|
|
use nu_source::nom_input;
|
2019-11-11 11:54:58 +00:00
|
|
|
|
2020-03-16 20:50:45 +00:00
|
|
|
use derive_new::new;
|
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()) {
|
2020-03-16 20:50:45 +00:00
|
|
|
Ok(rows) => {
|
|
|
|
for row in rows {
|
|
|
|
match row {
|
|
|
|
Value { value: UntaggedValue::Table(list), .. } => {
|
|
|
|
for l in list {
|
|
|
|
yield ReturnSuccess::value(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => yield ReturnSuccess::value(x),
|
2019-11-11 11:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-03-16 17:32:02 +00:00
|
|
|
Err(err) => {
|
2020-03-16 20:50:45 +00:00
|
|
|
let line_one = format!("Could not parse as {}", format_name);
|
2019-11-11 11:54:58 +00:00
|
|
|
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())
|
|
|
|
}
|
2020-03-16 17:32:02 +00:00
|
|
|
|
2020-03-16 20:50:45 +00:00
|
|
|
#[derive(Debug, Clone, new)]
|
|
|
|
pub struct EmptyRegistry {
|
|
|
|
#[new(default)]
|
|
|
|
signatures: indexmap::IndexMap<String, Signature>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmptyRegistry {}
|
|
|
|
|
|
|
|
impl SignatureRegistry for EmptyRegistry {
|
|
|
|
fn has(&self, _name: &str) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fn get(&self, _name: &str) -> Option<Signature> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
fn clone_box(&self) -> Box<dyn SignatureRegistry> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_delimited_string_to_value(
|
|
|
|
s: String,
|
|
|
|
headerless: bool,
|
|
|
|
sep: char,
|
|
|
|
tag: impl Into<Tag>,
|
|
|
|
) -> Result<Vec<Value>, ShellError> {
|
|
|
|
let tag = tag.into();
|
|
|
|
|
|
|
|
let mut entries = s.lines();
|
|
|
|
|
|
|
|
let mut fields = vec![];
|
|
|
|
let mut out = vec![];
|
|
|
|
|
|
|
|
if let Some(first_entry) = entries.next() {
|
|
|
|
let tokens = match parse(&sep.to_string(), nom_input(first_entry)) {
|
|
|
|
Ok((_, tokens)) => tokens,
|
|
|
|
Err(err) => return Err(ShellError::parse_error(err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let tokens_span = tokens.span;
|
|
|
|
let source: nu_source::Text = tokens_span.slice(&first_entry).into();
|
|
|
|
|
|
|
|
if !headerless {
|
|
|
|
fields = tokens
|
|
|
|
.item
|
|
|
|
.iter()
|
|
|
|
.filter(|token| !token.is_separator())
|
|
|
|
.map(|field| field.source(&source).to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
let registry = Box::new(EmptyRegistry::new());
|
|
|
|
let ctx = ExpandContext::new(registry, &source, None);
|
|
|
|
|
|
|
|
let mut iterator = TokensIterator::new(&tokens.item, ctx, tokens_span);
|
|
|
|
let (results, tokens_identified) = iterator.expand(LineSeparatedShape);
|
|
|
|
let results = results?;
|
|
|
|
|
|
|
|
let mut row = TaggedDictBuilder::new(&tag);
|
|
|
|
|
|
|
|
if headerless {
|
|
|
|
let fallback_columns = (1..=tokens_identified)
|
|
|
|
.map(|i| format!("Column{}", i))
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
for (idx, field) in results.into_iter().enumerate() {
|
|
|
|
let key = if headerless {
|
|
|
|
&fallback_columns[idx]
|
|
|
|
} else {
|
|
|
|
&fields[idx]
|
|
|
|
};
|
|
|
|
|
|
|
|
row.insert_value(key, field.into_value(&tag));
|
2020-03-16 17:32:02 +00:00
|
|
|
}
|
2020-03-16 20:50:45 +00:00
|
|
|
|
|
|
|
out.push(row.into_value())
|
2020-03-16 17:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-16 20:50:45 +00:00
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
let tokens = match parse(&sep.to_string(), nom_input(entry)) {
|
|
|
|
Ok((_, tokens)) => tokens,
|
|
|
|
Err(err) => return Err(ShellError::parse_error(err)),
|
|
|
|
};
|
|
|
|
let tokens_span = tokens.span;
|
|
|
|
|
|
|
|
let source: nu_source::Text = tokens_span.slice(&entry).into();
|
|
|
|
let registry = Box::new(EmptyRegistry::new());
|
|
|
|
let ctx = ExpandContext::new(registry, &source, None);
|
|
|
|
|
|
|
|
let mut iterator = TokensIterator::new(&tokens.item, ctx, tokens_span);
|
|
|
|
let (results, tokens_identified) = iterator.expand(LineSeparatedShape);
|
|
|
|
let results = results?;
|
|
|
|
|
|
|
|
let mut row = TaggedDictBuilder::new(&tag);
|
|
|
|
|
|
|
|
let fallback_columns = (1..=tokens_identified)
|
|
|
|
.map(|i| format!("Column{}", i))
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
for (idx, field) in results.into_iter().enumerate() {
|
|
|
|
let key = if headerless {
|
|
|
|
&fallback_columns[idx]
|
|
|
|
} else {
|
|
|
|
match fields.get(idx) {
|
|
|
|
Some(key) => key,
|
|
|
|
None => &fallback_columns[idx],
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
row.insert_value(key, field.into_value(&tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
out.push(row.into_value())
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(out)
|
2020-03-16 17:32:02 +00:00
|
|
|
}
|