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:
Ian Manske
2023-08-24 19:50:29 +00:00
committed by GitHub
parent 030e749fe7
commit 8da27a1a09
195 changed files with 4211 additions and 6245 deletions

View File

@ -6,8 +6,9 @@ use nu_protocol::{
Operator, PathMember, PipelineElement, Redirection,
},
engine::{EngineState, ProfilingConfig, Stack},
DataSource, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, PipelineMetadata,
Range, ShellError, Span, Spanned, Unit, Value, VarId, ENV_VARIABLE_ID,
record, DataSource, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
PipelineMetadata, Range, Record, ShellError, Span, Spanned, Unit, Value, VarId,
ENV_VARIABLE_ID,
};
use std::time::Instant;
use std::{collections::HashMap, path::PathBuf};
@ -588,12 +589,12 @@ pub fn eval_expression(
})
}
Expr::Record(fields) => {
let mut cols = vec![];
let mut vals = vec![];
let mut record = Record::new();
for (col, val) in fields {
// avoid duplicate cols.
let col_name = eval_expression(engine_state, stack, col)?.as_string()?;
let pos = cols.iter().position(|c| c == &col_name);
let pos = record.cols.iter().position(|c| c == &col_name);
match pos {
Some(index) => {
return Err(ShellError::ColumnDefinedTwice {
@ -602,17 +603,12 @@ pub fn eval_expression(
})
}
None => {
cols.push(col_name);
vals.push(eval_expression(engine_state, stack, val)?);
record.push(col_name, eval_expression(engine_state, stack, val)?);
}
}
}
Ok(Value::Record {
cols,
vals,
span: expr.span,
})
Ok(Value::record(record, expr.span))
}
Expr::Table(headers, vals) => {
let mut output_headers = vec![];
@ -626,11 +622,13 @@ pub fn eval_expression(
for expr in val {
row.push(eval_expression(engine_state, stack, expr)?);
}
output_rows.push(Value::Record {
cols: output_headers.clone(),
vals: row,
span: expr.span,
});
output_rows.push(Value::record(
Record {
cols: output_headers.clone(),
vals: row,
},
expr.span,
));
}
Ok(Value::List {
vals: output_rows,
@ -1241,201 +1239,154 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
}
}
let mut cols = vec![];
let mut vals = vec![];
let mut record = Record::new();
cols.push("default-config-dir".to_string());
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not get config directory".into())),
})
}
record.push(
"default-config-dir",
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not get config directory".into()))
},
);
cols.push("config-path".to_string());
if let Some(path) = engine_state.get_config_path("config-path") {
let canon_config_path = canonicalize_path(engine_state, path);
vals.push(Value::String {
val: canon_config_path.to_string_lossy().to_string(),
span,
})
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("config.nu");
vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not get config directory".into())),
})
}
record.push(
"config-path",
if let Some(path) = engine_state.get_config_path("config-path") {
let canon_config_path = canonicalize_path(engine_state, path);
Value::string(canon_config_path.to_string_lossy(), span)
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("config.nu");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not get config directory".into()))
},
);
cols.push("env-path".to_string());
if let Some(path) = engine_state.get_config_path("env-path") {
let canon_env_path = canonicalize_path(engine_state, path);
vals.push(Value::String {
val: canon_env_path.to_string_lossy().to_string(),
span,
})
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("env.nu");
vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
record.push(
"env-path",
if let Some(path) = engine_state.get_config_path("env-path") {
let canon_env_path = canonicalize_path(engine_state, path);
Value::string(canon_env_path.to_string_lossy(), span)
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("env.nu");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not find environment path".into(),
)),
})
}
))
},
);
cols.push("history-path".to_string());
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
match engine_state.config.history_file_format {
nu_protocol::HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
record.push(
"history-path",
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
match engine_state.config.history_file_format {
nu_protocol::HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
}
nu_protocol::HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
nu_protocol::HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
let canon_hist_path = canonicalize_path(engine_state, &path);
vals.push(Value::String {
val: canon_hist_path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not find history path".into())),
})
}
let canon_hist_path = canonicalize_path(engine_state, &path);
Value::string(canon_hist_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not find history path".into()))
},
);
cols.push("loginshell-path".to_string());
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("login.nu");
let canon_login_path = canonicalize_path(engine_state, &path);
vals.push(Value::String {
val: canon_login_path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
record.push(
"loginshell-path",
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("login.nu");
let canon_login_path = canonicalize_path(engine_state, &path);
Value::string(canon_login_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not find login shell path".into(),
)),
})
}
))
},
);
#[cfg(feature = "plugin")]
{
cols.push("plugin-path".to_string());
if let Some(path) = &engine_state.plugin_signatures {
let canon_plugin_path = canonicalize_path(engine_state, path);
vals.push(Value::String {
val: canon_plugin_path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
record.push(
"plugin-path",
if let Some(path) = &engine_state.plugin_signatures {
let canon_plugin_path = canonicalize_path(engine_state, path);
Value::string(canon_plugin_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not get plugin signature location".into(),
)),
})
}
))
},
);
}
cols.push("home-path".to_string());
if let Some(path) = nu_path::home_dir() {
let canon_home_path = canonicalize_path(engine_state, &path);
vals.push(Value::String {
val: canon_home_path.to_string_lossy().into(),
record.push(
"home-path",
if let Some(path) = nu_path::home_dir() {
let canon_home_path = canonicalize_path(engine_state, &path);
Value::string(canon_home_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not get home path".into()))
},
);
record.push("temp-path", {
let canon_temp_path = canonicalize_path(engine_state, &std::env::temp_dir());
Value::string(canon_temp_path.to_string_lossy(), span)
});
record.push("pid", Value::int(std::process::id().into(), span));
record.push("os-info", {
let sys = sysinfo::System::new();
let ver = match sys.kernel_version() {
Some(v) => v,
None => "unknown".into(),
};
Value::record(
record! {
"name" => Value::string(std::env::consts::OS, span),
"arch" => Value::string(std::env::consts::ARCH, span),
"family" => Value::string(std::env::consts::FAMILY, span),
"kernel_version" => Value::string(ver, span),
},
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not get home path".into())),
})
}
cols.push("temp-path".to_string());
let canon_temp_path = canonicalize_path(engine_state, &std::env::temp_dir());
vals.push(Value::String {
val: canon_temp_path.to_string_lossy().into(),
span,
)
});
cols.push("pid".to_string());
vals.push(Value::int(std::process::id().into(), span));
record.push(
"startup-time",
Value::duration(engine_state.get_startup_time(), span),
);
cols.push("os-info".to_string());
let sys = sysinfo::System::new();
let ver = match sys.kernel_version() {
Some(v) => v,
None => "unknown".into(),
};
let os_record = Value::Record {
cols: vec![
"name".into(),
"arch".into(),
"family".into(),
"kernel_version".into(),
],
vals: vec![
Value::string(std::env::consts::OS, span),
Value::string(std::env::consts::ARCH, span),
Value::string(std::env::consts::FAMILY, span),
Value::string(ver, span),
],
span,
};
vals.push(os_record);
record.push(
"is-interactive",
Value::bool(engine_state.is_interactive, span),
);
cols.push("startup-time".to_string());
vals.push(Value::Duration {
val: engine_state.get_startup_time(),
span,
});
record.push("is-login", Value::bool(engine_state.is_login, span));
cols.push("is-interactive".to_string());
vals.push(Value::Bool {
val: engine_state.is_interactive,
span,
});
cols.push("is-login".to_string());
vals.push(Value::Bool {
val: engine_state.is_login,
span,
});
cols.push("current-exe".to_string());
if let Ok(current_exe) = std::env::current_exe() {
vals.push(Value::String {
val: current_exe.to_string_lossy().into(),
span,
});
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
record.push(
"current-exe",
if let Ok(current_exe) = std::env::current_exe() {
Value::string(current_exe.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not get current executable path".to_string(),
)),
})
}
))
},
);
Ok(Value::Record { cols, vals, span })
Ok(Value::record(record, span))
}
pub fn eval_variable(
@ -1459,13 +1410,7 @@ pub fn eval_variable(
pairs.sort_by(|a, b| a.0.cmp(&b.0));
let (env_columns, env_values) = pairs.into_iter().unzip();
Ok(Value::Record {
cols: env_columns,
vals: env_values,
span,
})
Ok(Value::record(pairs.into_iter().collect(), span))
}
var_id => stack.get_var(var_id, span),
}
@ -1490,30 +1435,20 @@ fn collect_profiling_metadata(
let element_str = Value::string(element_str, element_span);
let time_ns = (end_time - start_time).as_nanos() as i64;
let mut cols = vec![
"pipeline_idx".to_string(),
"element_idx".to_string(),
"depth".to_string(),
"span".to_string(),
];
let span_record = record! {
"start" => Value::int(element_span.start as i64, element_span),
"end" => Value::int(element_span.end as i64, element_span),
};
let mut vals = vec![
Value::int(pipeline_idx as i64, element_span),
Value::int(element_idx as i64, element_span),
Value::int(profiling_config.depth, element_span),
Value::record(
vec!["start".to_string(), "end".to_string()],
vec![
Value::int(element_span.start as i64, element_span),
Value::int(element_span.end as i64, element_span),
],
element_span,
),
];
let mut record = record! {
"pipeline_idx" => Value::int(pipeline_idx as i64, element_span),
"element_idx" => Value::int(element_idx as i64, element_span),
"depth" => Value::int(profiling_config.depth, element_span),
"span" => Value::record(span_record, element_span),
};
if profiling_config.collect_source {
cols.push("source".to_string());
vals.push(element_str);
record.push("source", element_str);
}
if profiling_config.collect_values {
@ -1529,21 +1464,12 @@ fn collect_profiling_metadata(
},
};
cols.push("value".to_string());
vals.push(value);
record.push("value", value);
}
cols.push("time".to_string());
vals.push(Value::Duration {
val: time_ns,
span: element_span,
});
record.push("time", Value::duration(time_ns, element_span));
let record = Value::Record {
cols,
vals,
span: element_span,
};
let record = Value::record(record, element_span);
let element_metadata = if let Ok((pipeline_data, ..)) = &eval_result {
pipeline_data.metadata()