2022-12-21 22:21:03 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
ast::{Expr, Expression},
|
|
|
|
engine::StateWorkingSet,
|
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}`.
2023-08-24 19:50:29 +00:00
|
|
|
ParseError, Record, Span, Value,
|
2022-12-21 22:21:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Evaluate a constant value at parse time
|
|
|
|
///
|
|
|
|
/// Based off eval_expression() in the engine
|
|
|
|
pub fn eval_constant(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
expr: &Expression,
|
|
|
|
) -> Result<Value, ParseError> {
|
|
|
|
match &expr.expr {
|
2023-07-21 13:20:33 +00:00
|
|
|
Expr::Bool(b) => Ok(Value::bool(*b, expr.span)),
|
2022-12-21 22:21:03 +00:00
|
|
|
Expr::Int(i) => Ok(Value::int(*i, expr.span)),
|
|
|
|
Expr::Float(f) => Ok(Value::float(*f, expr.span)),
|
|
|
|
Expr::Binary(b) => Ok(Value::Binary {
|
|
|
|
val: b.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2023-04-08 20:04:57 +00:00
|
|
|
Expr::Filepath(path) => Ok(Value::String {
|
|
|
|
val: path.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2023-07-13 21:02:05 +00:00
|
|
|
Expr::Var(var_id) => match working_set.get_variable(*var_id).const_val.as_ref() {
|
2022-12-21 22:21:03 +00:00
|
|
|
Some(val) => Ok(val.clone()),
|
|
|
|
None => Err(ParseError::NotAConstant(expr.span)),
|
|
|
|
},
|
|
|
|
Expr::CellPath(cell_path) => Ok(Value::CellPath {
|
|
|
|
val: cell_path.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
|
|
|
Expr::FullCellPath(cell_path) => {
|
|
|
|
let value = eval_constant(working_set, &cell_path.head)?;
|
|
|
|
|
2023-03-16 03:50:58 +00:00
|
|
|
match value.follow_cell_path(&cell_path.tail, false) {
|
2022-12-21 22:21:03 +00:00
|
|
|
Ok(val) => Ok(val),
|
|
|
|
// TODO: Better error conversion
|
|
|
|
Err(shell_error) => Err(ParseError::LabeledError(
|
|
|
|
"Error when following cell path".to_string(),
|
2023-01-30 01:37:54 +00:00
|
|
|
format!("{shell_error:?}"),
|
2022-12-21 22:21:03 +00:00
|
|
|
expr.span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::DateTime(dt) => Ok(Value::Date {
|
|
|
|
val: *dt,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
|
|
|
Expr::List(x) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in x {
|
|
|
|
output.push(eval_constant(working_set, expr)?);
|
|
|
|
}
|
|
|
|
Ok(Value::List {
|
|
|
|
vals: output,
|
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Expr::Record(fields) => {
|
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}`.
2023-08-24 19:50:29 +00:00
|
|
|
let mut record = Record::new();
|
2022-12-21 22:21:03 +00:00
|
|
|
for (col, val) in fields {
|
|
|
|
// avoid duplicate cols.
|
|
|
|
let col_name = value_as_string(eval_constant(working_set, col)?, expr.span)?;
|
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}`.
2023-08-24 19:50:29 +00:00
|
|
|
let pos = record.cols.iter().position(|c| c == &col_name);
|
2022-12-21 22:21:03 +00:00
|
|
|
match pos {
|
|
|
|
Some(index) => {
|
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}`.
2023-08-24 19:50:29 +00:00
|
|
|
record.vals[index] = eval_constant(working_set, val)?;
|
2022-12-21 22:21:03 +00:00
|
|
|
}
|
|
|
|
None => {
|
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}`.
2023-08-24 19:50:29 +00:00
|
|
|
record.push(col_name, eval_constant(working_set, val)?);
|
2022-12-21 22:21:03 +00: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}`.
2023-08-24 19:50:29 +00:00
|
|
|
Ok(Value::record(record, expr.span))
|
2022-12-21 22:21:03 +00:00
|
|
|
}
|
|
|
|
Expr::Table(headers, vals) => {
|
|
|
|
let mut output_headers = vec![];
|
|
|
|
for expr in headers {
|
|
|
|
output_headers.push(value_as_string(
|
|
|
|
eval_constant(working_set, expr)?,
|
|
|
|
expr.span,
|
|
|
|
)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output_rows = vec![];
|
|
|
|
for val in vals {
|
|
|
|
let mut row = vec![];
|
|
|
|
for expr in val {
|
|
|
|
row.push(eval_constant(working_set, expr)?);
|
|
|
|
}
|
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}`.
2023-08-24 19:50:29 +00:00
|
|
|
output_rows.push(Value::record(
|
|
|
|
Record {
|
|
|
|
cols: output_headers.clone(),
|
|
|
|
vals: row,
|
|
|
|
},
|
|
|
|
expr.span,
|
|
|
|
));
|
2022-12-21 22:21:03 +00:00
|
|
|
}
|
|
|
|
Ok(Value::List {
|
|
|
|
vals: output_rows,
|
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Expr::Keyword(_, _, expr) => eval_constant(working_set, expr),
|
|
|
|
Expr::String(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
|
|
|
Expr::Nothing => Ok(Value::Nothing { span: expr.span }),
|
2023-05-20 13:23:25 +00:00
|
|
|
Expr::ValueWithUnit(expr, unit) => {
|
|
|
|
if let Ok(Value::Int { val, .. }) = eval_constant(working_set, expr) {
|
2023-08-24 20:48:05 +00:00
|
|
|
unit.item.to_value(val, unit.span).map_err(|_| {
|
|
|
|
ParseError::InvalidLiteral(
|
|
|
|
"literal can not fit in unit".into(),
|
|
|
|
"literal can not fit in unit".into(),
|
|
|
|
unit.span,
|
|
|
|
)
|
|
|
|
})
|
2023-05-20 13:23:25 +00:00
|
|
|
} else {
|
|
|
|
Err(ParseError::NotAConstant(expr.span))
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 22:21:03 +00:00
|
|
|
_ => Err(ParseError::NotAConstant(expr.span)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the value as a string
|
|
|
|
pub fn value_as_string(value: Value, span: Span) -> Result<String, ParseError> {
|
|
|
|
match value {
|
|
|
|
Value::String { val, .. } => Ok(val),
|
|
|
|
_ => Err(ParseError::NotAConstant(span)),
|
|
|
|
}
|
|
|
|
}
|