Refactoring and more split-by flexibility.

This commit is contained in:
Andrés N. Robalino 2020-06-17 12:34:26 -05:00
parent 778e497903
commit 9f54d238ba
2 changed files with 57 additions and 4 deletions

View file

@ -112,17 +112,17 @@ pub fn suggestions(tried: Tagged<&str>, for_value: &Value) -> ShellError {
possible_matches.sort();
if !possible_matches.is_empty() {
return ShellError::labeled_error(
ShellError::labeled_error(
"Unknown column",
format!("did you mean '{}'?", possible_matches[0].1),
tried.tag(),
);
)
} else {
return ShellError::labeled_error(
ShellError::labeled_error(
"Unknown column",
"row does not contain this column",
tried.tag(),
);
)
}
}

View file

@ -0,0 +1,53 @@
use nu_errors::ShellError;
use nu_protocol::{SpannedTypeName, TaggedDictBuilder, UntaggedValue, Value};
use nu_source::Tag;
use crate::utils::data::group;
#[allow(clippy::type_complexity)]
pub fn split(
value: &Value,
splitter: &Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
tag: impl Into<Tag>,
) -> Result<Value, ShellError> {
let tag = tag.into();
let mut splits = indexmap::IndexMap::new();
for (column, value) in value.row_entries() {
if !&value.is_table() {
return Err(ShellError::type_error(
"a table value",
value.spanned_type_name(),
));
}
match group(&value, splitter, &tag) {
Ok(grouped) => {
for (split_label, subset) in grouped.row_entries() {
let s = splits
.entry(split_label.clone())
.or_insert(indexmap::IndexMap::new());
if !&subset.is_table() {
return Err(ShellError::type_error(
"a table value",
subset.spanned_type_name(),
));
}
s.insert(column.clone(), subset.clone());
}
}
Err(err) => return Err(err),
}
}
let mut out = TaggedDictBuilder::new(&tag);
for (k, v) in splits.into_iter() {
out.insert_untagged(k, UntaggedValue::row(v));
}
Ok(out.into_value())
}