mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Create Record
type (#10103)
# Description This PR creates a new `Record` type to reduce duplicate code and possibly bugs as well. (This is an edited version of #9648.) - `Record` implements `FromIterator` and `IntoIterator` and so can be iterated over or collected into. For example, this helps with conversions to and from (hash)maps. (Also, no more `cols.iter().zip(vals)`!) - `Record` has a `push(col, val)` function to help insure that the number of columns is equal to the number of values. I caught a few potential bugs thanks to this (e.g. in the `ls` command). - Finally, this PR also adds a `record!` macro that helps simplify record creation. It is used like so: ```rust record! { "key1" => some_value, "key2" => Value::string("text", span), "key3" => Value::int(optional_int.unwrap_or(0), span), "key4" => Value::bool(config.setting, span), } ``` Since macros hinder formatting, etc., the right hand side values should be relatively short and sweet like the examples above. Where possible, prefer `record!` or `.collect()` on an iterator instead of multiple `Record::push`s, since the first two automatically set the record capacity and do less work overall. # User-Facing Changes Besides the changes in `nu-protocol` the only other breaking changes are to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use filesize::file_real_size_fast;
|
||||
use nu_glob::Pattern;
|
||||
use nu_protocol::{ShellError, Span, Value};
|
||||
use nu_protocol::{record, ShellError, Span, Value};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
@ -185,30 +185,6 @@ impl DirInfo {
|
||||
|
||||
impl From<DirInfo> for Value {
|
||||
fn from(d: DirInfo) -> Self {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("path".into());
|
||||
vals.push(Value::string(d.path.display().to_string(), d.tag));
|
||||
|
||||
cols.push("apparent".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: d.size as i64,
|
||||
span: d.tag,
|
||||
});
|
||||
|
||||
cols.push("physical".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: d.blocks as i64,
|
||||
span: d.tag,
|
||||
});
|
||||
|
||||
cols.push("directories".into());
|
||||
vals.push(value_from_vec(d.dirs, d.tag));
|
||||
|
||||
cols.push("files".into());
|
||||
vals.push(value_from_vec(d.files, d.tag));
|
||||
|
||||
// if !d.errors.is_empty() {
|
||||
// let v = d
|
||||
// .errors
|
||||
@ -223,51 +199,34 @@ impl From<DirInfo> for Value {
|
||||
// })
|
||||
// }
|
||||
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: d.tag,
|
||||
}
|
||||
Value::record(
|
||||
record! {
|
||||
"path" => Value::string(d.path.display().to_string(), d.tag),
|
||||
"apparent" => Value::filesize(d.size as i64, d.tag),
|
||||
"physical" => Value::filesize(d.blocks as i64, d.tag),
|
||||
"directories" => value_from_vec(d.dirs, d.tag),
|
||||
"files" => value_from_vec(d.files, d.tag)
|
||||
},
|
||||
d.tag,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FileInfo> for Value {
|
||||
fn from(f: FileInfo) -> Self {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("path".into());
|
||||
vals.push(Value::string(f.path.display().to_string(), f.tag));
|
||||
|
||||
cols.push("apparent".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: f.size as i64,
|
||||
span: f.tag,
|
||||
});
|
||||
|
||||
cols.push("physical".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: match f.blocks {
|
||||
Some(b) => b as i64,
|
||||
None => 0i64,
|
||||
},
|
||||
span: f.tag,
|
||||
});
|
||||
|
||||
cols.push("directories".into());
|
||||
vals.push(Value::nothing(Span::unknown()));
|
||||
|
||||
cols.push("files".into());
|
||||
vals.push(Value::nothing(Span::unknown()));
|
||||
|
||||
// cols.push("errors".into());
|
||||
// vals.push(Value::nothing(Span::unknown()));
|
||||
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: f.tag,
|
||||
}
|
||||
Value::record(
|
||||
record! {
|
||||
"path" => Value::string(f.path.display().to_string(), f.tag),
|
||||
"apparent" => Value::filesize(f.size as i64, f.tag),
|
||||
"physical" => Value::filesize(f.blocks.unwrap_or(0) as i64, f.tag),
|
||||
"directories" => Value::nothing(Span::unknown()),
|
||||
"files" => Value::nothing(Span::unknown()),
|
||||
},
|
||||
f.tag,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user