Use Record's public API in a bunch of places (#10927)

# Description
Since #10841 the goal is to remove the implementation details of
`Record` outside of core operations.

To this end use Record iterators and map-like accessors in a bunch of
places. In this PR I try to collect the boring cases where I don't
expect any dramatic performance impacts or don't have doubts about the
correctness afterwards

- Use checked record construction in `nu_plugin_example`
- Use `Record::into_iter` in `columns`
- Use `Record` iterators in `headers` cmd
- Use explicit record iterators in `split-by`
- Use `Record::into_iter` in variable completions
- Use `Record::values` iterator in `into sqlite`
- Use `Record::iter_mut` for-loop in `default`
- Change `nu_engine::nonexistent_column` to use iterator
- Use `Record::columns` iter in `nu-cmd-base`
- Use `Record::get_index` in `nu-command/network/http`
- Use `Record.insert()` in `merge`
- Refactor `move` to use encapsulated record API
- Use `Record.insert()` in `explore`
- Use proper `Record` API in `explore`
- Remove defensiveness around record in `explore`
- Use encapsulated record API in more `nu-command`s

# User-Facing Changes
None intentional

# Tests + Formatting
(-)
This commit is contained in:
Stefan Holderbach
2023-11-08 14:24:00 +01:00
committed by GitHub
parent b03ef56bcb
commit edbf3aaccb
24 changed files with 70 additions and 108 deletions

View File

@ -165,7 +165,7 @@ fn help_frame_data(
let (cols, mut vals) = help_manual_data(manual, aliases);
let vals = vals.remove(0);
Value::record(Record { cols, vals }, NuSpan::unknown())
Value::record(Record::from_raw_cols_vals(cols, vals), NuSpan::unknown())
})
.collect();
let commands = Value::list(commands, NuSpan::unknown());

View File

@ -90,7 +90,10 @@ fn collect_external_stream(
pub fn collect_input(value: Value) -> (Vec<String>, Vec<Vec<Value>>) {
let span = value.span();
match value {
Value::Record { val: record, .. } => (record.cols, vec![record.vals]),
Value::Record { val: record, .. } => {
let (key, val) = record.into_iter().unzip();
(key, vec![val])
}
Value::List { vals, .. } => {
let mut columns = get_columns(&vals);
let data = convert_records_to_dataset(&columns, vals);

View File

@ -1041,21 +1041,9 @@ fn set_config(hm: &mut HashMap<String, Value>, path: &[&str], value: Value) -> b
match val {
Value::Record { val: record, .. } => {
if path.len() == 2 {
if record.cols.len() != record.vals.len() {
return false;
}
let key = path[1];
let key = &path[1];
let pos = record.cols.iter().position(|v| v == key);
match pos {
Some(i) => {
record.vals[i] = value;
}
None => {
record.push(*key, value);
}
}
record.insert(key, value);
} else {
let mut hm2: HashMap<String, Value> = HashMap::new();
for (k, v) in record.iter() {

View File

@ -708,10 +708,7 @@ fn build_table_as_list(v: &RecordView) -> Value {
.cloned()
.map(|vals| {
Value::record(
Record {
cols: headers.clone(),
vals,
},
Record::from_raw_cols_vals(headers.clone(), vals),
NuSpan::unknown(),
)
})
@ -726,7 +723,7 @@ fn build_table_as_record(v: &RecordView) -> Value {
let cols = layer.columns.to_vec();
let vals = layer.records.get(0).map_or(Vec::new(), |row| row.clone());
Value::record(Record { cols, vals }, NuSpan::unknown())
Value::record(Record::from_raw_cols_vals(cols, vals), NuSpan::unknown())
}
fn report_cursor_position(mode: UIMode, cursor: XYCursor) -> String {