mirror of
https://github.com/nushell/nushell
synced 2024-12-30 23:08:56 +00:00
Refactoring and more split-by flexibility.
This commit is contained in:
parent
778e497903
commit
9f54d238ba
2 changed files with 57 additions and 4 deletions
|
@ -112,17 +112,17 @@ pub fn suggestions(tried: Tagged<&str>, for_value: &Value) -> ShellError {
|
||||||
possible_matches.sort();
|
possible_matches.sort();
|
||||||
|
|
||||||
if !possible_matches.is_empty() {
|
if !possible_matches.is_empty() {
|
||||||
return ShellError::labeled_error(
|
ShellError::labeled_error(
|
||||||
"Unknown column",
|
"Unknown column",
|
||||||
format!("did you mean '{}'?", possible_matches[0].1),
|
format!("did you mean '{}'?", possible_matches[0].1),
|
||||||
tried.tag(),
|
tried.tag(),
|
||||||
);
|
)
|
||||||
} else {
|
} else {
|
||||||
return ShellError::labeled_error(
|
ShellError::labeled_error(
|
||||||
"Unknown column",
|
"Unknown column",
|
||||||
"row does not contain this column",
|
"row does not contain this column",
|
||||||
tried.tag(),
|
tried.tag(),
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
53
crates/nu-cli/src/utils/data/split.rs
Normal file
53
crates/nu-cli/src/utils/data/split.rs
Normal 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())
|
||||||
|
}
|
Loading…
Reference in a new issue