mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 21:37:54 +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:
@ -3,7 +3,7 @@ use crate::{
|
||||
parse_nustyle, NuStyle,
|
||||
};
|
||||
use nu_ansi_term::Style;
|
||||
use nu_protocol::Value;
|
||||
use nu_protocol::{Record, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn lookup_ansi_color_style(s: &str) -> Style {
|
||||
@ -32,7 +32,7 @@ pub fn get_color_map(colors: &HashMap<String, Value>) -> HashMap<String, Style>
|
||||
fn parse_map_entry(hm: &mut HashMap<String, Style>, key: &str, value: &Value) {
|
||||
let value = match value {
|
||||
Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
|
||||
Value::Record { cols, vals, .. } => get_style_from_value(cols, vals).map(parse_nustyle),
|
||||
Value::Record { val, .. } => get_style_from_value(val).map(parse_nustyle),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(value) = value {
|
||||
@ -40,10 +40,10 @@ fn parse_map_entry(hm: &mut HashMap<String, Style>, key: &str, value: &Value) {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_style_from_value(cols: &[String], vals: &[Value]) -> Option<NuStyle> {
|
||||
fn get_style_from_value(record: &Record) -> Option<NuStyle> {
|
||||
let mut was_set = false;
|
||||
let mut style = NuStyle::from(Style::default());
|
||||
for (col, val) in cols.iter().zip(vals) {
|
||||
for (col, val) in record {
|
||||
match col.as_str() {
|
||||
"bg" => {
|
||||
if let Value::String { val, .. } = val {
|
||||
@ -120,48 +120,54 @@ mod tests {
|
||||
#[test]
|
||||
fn test_get_style_from_value() {
|
||||
// Test case 1: all values are valid
|
||||
let cols = vec!["bg".to_string(), "fg".to_string(), "attr".to_string()];
|
||||
let vals = vec![
|
||||
Value::String {
|
||||
val: "red".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "blue".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "bold".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
];
|
||||
let record = Record {
|
||||
cols: vec!["bg".to_string(), "fg".to_string(), "attr".to_string()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "red".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "blue".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "bold".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
],
|
||||
};
|
||||
let expected_style = NuStyle {
|
||||
bg: Some("red".to_string()),
|
||||
fg: Some("blue".to_string()),
|
||||
attr: Some("bold".to_string()),
|
||||
};
|
||||
assert_eq!(get_style_from_value(&cols, &vals), Some(expected_style));
|
||||
assert_eq!(get_style_from_value(&record), Some(expected_style));
|
||||
|
||||
// Test case 2: no values are valid
|
||||
let cols = vec!["invalid".to_string()];
|
||||
let vals = vec![Value::nothing(Span::unknown())];
|
||||
assert_eq!(get_style_from_value(&cols, &vals), None);
|
||||
let record = Record {
|
||||
cols: vec!["invalid".to_string()],
|
||||
vals: vec![Value::nothing(Span::unknown())],
|
||||
};
|
||||
assert_eq!(get_style_from_value(&record), None);
|
||||
|
||||
// Test case 3: some values are valid
|
||||
let cols = vec!["bg".to_string(), "invalid".to_string()];
|
||||
let vals = vec![
|
||||
Value::String {
|
||||
val: "green".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::nothing(Span::unknown()),
|
||||
];
|
||||
let record = Record {
|
||||
cols: vec!["bg".to_string(), "invalid".to_string()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "green".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::nothing(Span::unknown()),
|
||||
],
|
||||
};
|
||||
let expected_style = NuStyle {
|
||||
bg: Some("green".to_string()),
|
||||
fg: None,
|
||||
attr: None,
|
||||
};
|
||||
assert_eq!(get_style_from_value(&cols, &vals), Some(expected_style));
|
||||
assert_eq!(get_style_from_value(&record), Some(expected_style));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -100,8 +100,8 @@ pub fn color_record_to_nustyle(value: &Value) -> Style {
|
||||
let mut bg = None;
|
||||
let mut attr = None;
|
||||
let v = value.as_record();
|
||||
if let Ok((cols, inner_vals)) = v {
|
||||
for (k, v) in cols.iter().zip(inner_vals) {
|
||||
if let Ok(record) = v {
|
||||
for (k, v) in record {
|
||||
// Because config already type-checked the color_config records, this doesn't bother giving errors
|
||||
// if there are unrecognised keys or bad values.
|
||||
if let Ok(v) = v.as_string() {
|
||||
|
Reference in New Issue
Block a user