2019-11-04 15:47:03 +00:00
|
|
|
pub(crate) mod shape;
|
|
|
|
|
2019-07-23 22:22:11 +00:00
|
|
|
use crate::context::CommandRegistry;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use crate::evaluate::evaluate_baseline_expr;
|
|
|
|
use bigdecimal::BigDecimal;
|
2019-05-15 18:14:51 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2019-05-26 06:54:41 +00:00
|
|
|
use derive_new::new;
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-17 22:26:27 +00:00
|
|
|
use log::trace;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 21:14:52 +00:00
|
|
|
use nu_parser::{hir, CompareOperator};
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_protocol::{
|
2019-12-04 19:52:31 +00:00
|
|
|
Evaluate, EvaluateTrait, Primitive, Scope, ShellTypeName, SpannedTypeName, TaggedDictBuilder,
|
|
|
|
UntaggedValue, Value,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
};
|
|
|
|
use nu_source::{Tag, Text};
|
2019-12-09 18:52:01 +00:00
|
|
|
use nu_value_ext::ValueExt;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use num_bigint::BigInt;
|
|
|
|
use num_traits::Zero;
|
|
|
|
use query_interface::{interfaces, vtable_for, ObjectHash};
|
2019-08-02 19:15:07 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-05-15 18:14:51 +00:00
|
|
|
use std::time::SystemTime;
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-05-28 02:01:37 +00:00
|
|
|
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, new, Serialize)]
|
2019-05-26 06:54:41 +00:00
|
|
|
pub struct Operation {
|
2019-08-29 11:08:28 +00:00
|
|
|
pub(crate) left: Value,
|
2019-12-04 21:14:52 +00:00
|
|
|
pub(crate) operator: CompareOperator,
|
2019-08-29 11:08:28 +00:00
|
|
|
pub(crate) right: Value,
|
2019-05-26 06:54:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Serialize, Deserialize, new)]
|
2019-05-28 06:45:18 +00:00
|
|
|
pub struct Block {
|
2019-08-29 11:08:28 +00:00
|
|
|
pub(crate) expressions: Vec<hir::Expression>,
|
|
|
|
pub(crate) source: Text,
|
2019-09-14 16:30:24 +00:00
|
|
|
pub(crate) tag: Tag,
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
interfaces!(Block: dyn ObjectHash);
|
2019-06-29 08:55:42 +00:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
#[typetag::serde]
|
|
|
|
impl EvaluateTrait for Block {
|
|
|
|
fn invoke(&self, scope: &Scope) -> Result<Value, ShellError> {
|
2019-12-06 15:28:26 +00:00
|
|
|
if self.expressions.is_empty() {
|
2019-12-04 19:52:31 +00:00
|
|
|
return Ok(UntaggedValue::nothing().into_value(&self.tag));
|
2019-06-29 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut last = None;
|
|
|
|
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-17 22:26:27 +00:00
|
|
|
trace!(
|
|
|
|
"EXPRS = {:?}",
|
|
|
|
self.expressions
|
|
|
|
.iter()
|
2019-11-25 18:07:20 +00:00
|
|
|
.map(|e| format!("{:?}", e))
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-17 22:26:27 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
|
2019-06-29 08:55:42 +00:00
|
|
|
for expr in self.expressions.iter() {
|
2019-07-23 22:22:11 +00:00
|
|
|
last = Some(evaluate_baseline_expr(
|
|
|
|
&expr,
|
|
|
|
&CommandRegistry::empty(),
|
|
|
|
&scope,
|
|
|
|
&self.source,
|
|
|
|
)?)
|
2019-06-29 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(last.unwrap())
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
2019-08-30 17:29:04 +00:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
fn clone_box(&self) -> Evaluate {
|
|
|
|
let block = self.clone();
|
|
|
|
Evaluate::new(block)
|
2019-08-26 14:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-07-03 20:31:15 +00:00
|
|
|
pub enum Switch {
|
|
|
|
Present,
|
|
|
|
Absent,
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
impl std::convert::TryFrom<Option<&Value>> for Switch {
|
2019-07-03 20:31:15 +00:00
|
|
|
type Error = ShellError;
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn try_from(value: Option<&Value>) -> Result<Switch, ShellError> {
|
2019-07-03 20:31:15 +00:00
|
|
|
match value {
|
|
|
|
None => Ok(Switch::Absent),
|
2019-11-21 14:33:14 +00:00
|
|
|
Some(value) => match &value.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Boolean(true)) => Ok(Switch::Present),
|
|
|
|
_ => Err(ShellError::type_error("Boolean", value.spanned_type_name())),
|
2019-07-03 20:31:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub(crate) fn select_fields(obj: &Value, fields: &[String], tag: impl Into<Tag>) -> Value {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut out = TaggedDictBuilder::new(tag);
|
2019-05-15 18:14:51 +00:00
|
|
|
|
|
|
|
let descs = obj.data_descriptors();
|
|
|
|
|
|
|
|
for field in fields {
|
2019-07-13 02:07:06 +00:00
|
|
|
match descs.iter().find(|d| *d == field) {
|
2019-12-04 19:52:31 +00:00
|
|
|
None => out.insert_untagged(field, UntaggedValue::nothing()),
|
2019-11-21 14:33:14 +00:00
|
|
|
Some(desc) => out.insert_value(desc.clone(), obj.get_data(desc).borrow().clone()),
|
2019-05-15 18:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
out.into_value()
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub(crate) fn reject_fields(obj: &Value, fields: &[String], tag: impl Into<Tag>) -> Value {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut out = TaggedDictBuilder::new(tag);
|
2019-05-15 21:44:06 +00:00
|
|
|
|
|
|
|
let descs = obj.data_descriptors();
|
|
|
|
|
|
|
|
for desc in descs {
|
2019-08-29 02:44:08 +00:00
|
|
|
if fields.iter().any(|field| *field == desc) {
|
|
|
|
continue;
|
|
|
|
} else {
|
2019-11-21 14:33:14 +00:00
|
|
|
out.insert_value(desc.clone(), obj.get_data(&desc).borrow().clone())
|
2019-05-15 21:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
out.into_value()
|
2019-05-15 21:44:06 +00:00
|
|
|
}
|
2019-05-16 02:42:44 +00:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
pub(crate) enum CompareValues {
|
2019-09-01 16:20:31 +00:00
|
|
|
Ints(BigInt, BigInt),
|
|
|
|
Decimals(BigDecimal, BigDecimal),
|
2019-05-28 06:45:18 +00:00
|
|
|
String(String, String),
|
2019-11-16 01:36:51 +00:00
|
|
|
Date(DateTime<Utc>, DateTime<Utc>),
|
2019-11-17 05:48:48 +00:00
|
|
|
DateDuration(DateTime<Utc>, u64),
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CompareValues {
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
pub fn compare(&self) -> std::cmp::Ordering {
|
2019-05-28 06:45:18 +00:00
|
|
|
match self {
|
|
|
|
CompareValues::Ints(left, right) => left.cmp(right),
|
2019-08-30 17:29:04 +00:00
|
|
|
CompareValues::Decimals(left, right) => left.cmp(right),
|
2019-05-28 06:45:18 +00:00
|
|
|
CompareValues::String(left, right) => left.cmp(right),
|
2019-11-17 05:48:48 +00:00
|
|
|
CompareValues::Date(left, right) => left.cmp(right),
|
|
|
|
CompareValues::DateDuration(left, right) => {
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
// Create the datetime we're comparing against, as duration is an offset from now
|
|
|
|
let right: DateTime<Utc> = (SystemTime::now() - Duration::from_secs(*right)).into();
|
|
|
|
right.cmp(left)
|
|
|
|
}
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
pub(crate) fn coerce_compare(
|
2019-11-21 17:18:00 +00:00
|
|
|
left: &UntaggedValue,
|
|
|
|
right: &UntaggedValue,
|
2019-11-04 15:47:03 +00:00
|
|
|
) -> Result<CompareValues, (&'static str, &'static str)> {
|
2019-11-21 17:18:00 +00:00
|
|
|
match (left, right) {
|
2019-11-21 14:33:14 +00:00
|
|
|
(UntaggedValue::Primitive(left), UntaggedValue::Primitive(right)) => {
|
|
|
|
coerce_compare_primitive(left, right)
|
|
|
|
}
|
2019-05-28 06:45:18 +00:00
|
|
|
|
2019-06-24 00:55:31 +00:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 00:55:31 +00:00
|
|
|
fn coerce_compare_primitive(
|
|
|
|
left: &Primitive,
|
|
|
|
right: &Primitive,
|
2019-11-04 15:47:03 +00:00
|
|
|
) -> Result<CompareValues, (&'static str, &'static str)> {
|
2019-05-28 06:45:18 +00:00
|
|
|
use Primitive::*;
|
|
|
|
|
2019-06-24 00:55:31 +00:00
|
|
|
Ok(match (left, right) {
|
2019-09-01 16:20:31 +00:00
|
|
|
(Int(left), Int(right)) => CompareValues::Ints(left.clone(), right.clone()),
|
|
|
|
(Int(left), Decimal(right)) => {
|
|
|
|
CompareValues::Decimals(BigDecimal::zero() + left, right.clone())
|
|
|
|
}
|
|
|
|
(Int(left), Bytes(right)) => CompareValues::Ints(left.clone(), BigInt::from(*right)),
|
|
|
|
(Decimal(left), Decimal(right)) => CompareValues::Decimals(left.clone(), right.clone()),
|
|
|
|
(Decimal(left), Int(right)) => {
|
|
|
|
CompareValues::Decimals(left.clone(), BigDecimal::zero() + right)
|
|
|
|
}
|
2019-08-30 23:37:23 +00:00
|
|
|
(Decimal(left), Bytes(right)) => {
|
2019-09-01 16:20:31 +00:00
|
|
|
CompareValues::Decimals(left.clone(), BigDecimal::from(*right))
|
2019-08-30 23:37:23 +00:00
|
|
|
}
|
2019-09-01 16:20:31 +00:00
|
|
|
(Bytes(left), Int(right)) => CompareValues::Ints(BigInt::from(*left), right.clone()),
|
2019-08-30 23:37:23 +00:00
|
|
|
(Bytes(left), Decimal(right)) => {
|
2019-09-01 16:20:31 +00:00
|
|
|
CompareValues::Decimals(BigDecimal::from(*left), right.clone())
|
2019-08-30 23:37:23 +00:00
|
|
|
}
|
2019-06-24 00:55:31 +00:00
|
|
|
(String(left), String(right)) => CompareValues::String(left.clone(), right.clone()),
|
2019-12-03 06:44:59 +00:00
|
|
|
(Line(left), String(right)) => CompareValues::String(left.clone(), right.clone()),
|
|
|
|
(String(left), Line(right)) => CompareValues::String(left.clone(), right.clone()),
|
|
|
|
(Line(left), Line(right)) => CompareValues::String(left.clone(), right.clone()),
|
2019-12-06 15:28:26 +00:00
|
|
|
(Date(left), Date(right)) => CompareValues::Date(*left, *right),
|
|
|
|
(Date(left), Duration(right)) => CompareValues::DateDuration(*left, *right),
|
2019-06-24 00:55:31 +00:00
|
|
|
_ => return Err((left.type_name(), right.type_name())),
|
|
|
|
})
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
2019-10-18 12:08:04 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use indexmap::IndexMap;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{ColumnPath as ColumnPathValue, PathMember, UntaggedValue, Value};
|
2019-11-21 14:33:14 +00:00
|
|
|
use nu_source::*;
|
2019-12-09 18:52:01 +00:00
|
|
|
use nu_value_ext::{as_column_path, ValueExt};
|
2019-11-04 15:47:03 +00:00
|
|
|
use num_bigint::BigInt;
|
2019-10-18 12:08:04 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn string(input: impl Into<String>) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::string(input.into()).into_untagged_value()
|
2019-10-18 12:08:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn int(input: impl Into<BigInt>) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::int(input.into()).into_untagged_value()
|
2019-11-01 21:19:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn row(entries: IndexMap<String, Value>) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::row(entries).into_untagged_value()
|
2019-10-18 12:08:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 07:36:08 +00:00
|
|
|
fn table(list: &[Value]) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::table(list).into_untagged_value()
|
2019-10-18 12:08:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
fn error_callback(
|
|
|
|
reason: &'static str,
|
|
|
|
) -> impl FnOnce((&Value, &PathMember, ShellError)) -> ShellError {
|
|
|
|
move |(_obj_source, _column_path_tried, _err)| ShellError::unimplemented(reason)
|
2019-10-30 22:46:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 07:36:08 +00:00
|
|
|
fn column_path(paths: &[Value]) -> Tagged<ColumnPathValue> {
|
|
|
|
as_column_path(&table(paths)).unwrap()
|
2019-10-18 12:08:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:55:56 +00:00
|
|
|
#[test]
|
|
|
|
fn gets_matching_field_from_a_row() {
|
2019-12-04 19:52:31 +00:00
|
|
|
let row = UntaggedValue::row(indexmap! {
|
2019-12-31 07:36:08 +00:00
|
|
|
"amigos".into() => table(&[string("andres"),string("jonathan"),string("yehuda")])
|
2019-11-21 14:33:14 +00:00
|
|
|
})
|
|
|
|
.into_untagged_value();
|
2019-10-18 12:08:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-04 15:47:03 +00:00
|
|
|
row.get_data_by_key("amigos".spanned_unknown()).unwrap(),
|
2019-12-31 07:36:08 +00:00
|
|
|
table(&[string("andres"), string("jonathan"), string("yehuda")])
|
2019-10-18 12:08:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-20 11:55:56 +00:00
|
|
|
fn gets_matching_field_from_nested_rows_inside_a_row() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[string("package"), string("version")]);
|
2019-10-18 12:08:04 +00:00
|
|
|
|
2019-10-20 11:55:56 +00:00
|
|
|
let (version, tag) = string("0.4.0").into_parts();
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let value = UntaggedValue::row(indexmap! {
|
2019-10-20 11:55:56 +00:00
|
|
|
"package".into() =>
|
|
|
|
row(indexmap! {
|
|
|
|
"name".into() => string("nu"),
|
|
|
|
"version".into() => string("0.4.0")
|
|
|
|
})
|
|
|
|
});
|
2019-10-18 12:08:04 +00:00
|
|
|
|
2019-10-20 11:55:56 +00:00
|
|
|
assert_eq!(
|
2019-11-04 15:47:03 +00:00
|
|
|
*value
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag)
|
2019-11-04 15:47:03 +00:00
|
|
|
.get_data_by_column_path(&field_path, Box::new(error_callback("package.version")))
|
2019-10-30 22:46:40 +00:00
|
|
|
.unwrap(),
|
2019-10-20 11:55:56 +00:00
|
|
|
version
|
|
|
|
)
|
2019-10-18 12:08:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
#[test]
|
|
|
|
fn gets_first_matching_field_from_rows_with_same_field_inside_a_table() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[string("package"), string("authors"), string("name")]);
|
2019-11-04 15:47:03 +00:00
|
|
|
|
|
|
|
let (_, tag) = string("Andrés N. Robalino").into_parts();
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let value = UntaggedValue::row(indexmap! {
|
2019-11-04 15:47:03 +00:00
|
|
|
"package".into() => row(indexmap! {
|
|
|
|
"name".into() => string("nu"),
|
|
|
|
"version".into() => string("0.4.0"),
|
2019-12-31 07:36:08 +00:00
|
|
|
"authors".into() => table(&[
|
2019-11-04 15:47:03 +00:00
|
|
|
row(indexmap!{"name".into() => string("Andrés N. Robalino")}),
|
|
|
|
row(indexmap!{"name".into() => string("Jonathan Turner")}),
|
|
|
|
row(indexmap!{"name".into() => string("Yehuda Katz")})
|
|
|
|
])
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
value
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag)
|
2019-11-04 15:47:03 +00:00
|
|
|
.get_data_by_column_path(
|
|
|
|
&field_path,
|
|
|
|
Box::new(error_callback("package.authors.name"))
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2019-12-31 07:36:08 +00:00
|
|
|
table(&[
|
2019-11-04 15:47:03 +00:00
|
|
|
string("Andrés N. Robalino"),
|
|
|
|
string("Jonathan Turner"),
|
|
|
|
string("Yehuda Katz")
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:55:26 +00:00
|
|
|
#[test]
|
2019-10-31 19:20:22 +00:00
|
|
|
fn column_path_that_contains_just_a_number_gets_a_row_from_a_table() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[string("package"), string("authors"), int(0)]);
|
2019-10-30 10:55:26 +00:00
|
|
|
|
|
|
|
let (_, tag) = string("Andrés N. Robalino").into_parts();
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let value = UntaggedValue::row(indexmap! {
|
2019-10-30 10:55:26 +00:00
|
|
|
"package".into() => row(indexmap! {
|
|
|
|
"name".into() => string("nu"),
|
|
|
|
"version".into() => string("0.4.0"),
|
2019-12-31 07:36:08 +00:00
|
|
|
"authors".into() => table(&[
|
2019-10-30 10:55:26 +00:00
|
|
|
row(indexmap!{"name".into() => string("Andrés N. Robalino")}),
|
|
|
|
row(indexmap!{"name".into() => string("Jonathan Turner")}),
|
|
|
|
row(indexmap!{"name".into() => string("Yehuda Katz")})
|
|
|
|
])
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-04 15:47:03 +00:00
|
|
|
*value
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag)
|
2019-11-04 15:47:03 +00:00
|
|
|
.get_data_by_column_path(&field_path, Box::new(error_callback("package.authors.0")))
|
2019-10-30 22:46:40 +00:00
|
|
|
.unwrap(),
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::row(indexmap! {
|
2019-10-30 10:55:26 +00:00
|
|
|
"name".into() => string("Andrés N. Robalino")
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:20:22 +00:00
|
|
|
#[test]
|
|
|
|
fn column_path_that_contains_just_a_number_gets_a_row_from_a_row() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[string("package"), string("authors"), string("0")]);
|
2019-10-31 19:20:22 +00:00
|
|
|
|
|
|
|
let (_, tag) = string("Andrés N. Robalino").into_parts();
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let value = UntaggedValue::row(indexmap! {
|
2019-10-31 19:20:22 +00:00
|
|
|
"package".into() => row(indexmap! {
|
|
|
|
"name".into() => string("nu"),
|
|
|
|
"version".into() => string("0.4.0"),
|
|
|
|
"authors".into() => row(indexmap! {
|
|
|
|
"0".into() => row(indexmap!{"name".into() => string("Andrés N. Robalino")}),
|
|
|
|
"1".into() => row(indexmap!{"name".into() => string("Jonathan Turner")}),
|
|
|
|
"2".into() => row(indexmap!{"name".into() => string("Yehuda Katz")}),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-04 15:47:03 +00:00
|
|
|
*value
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag)
|
2019-11-04 15:47:03 +00:00
|
|
|
.get_data_by_column_path(
|
|
|
|
&field_path,
|
|
|
|
Box::new(error_callback("package.authors.\"0\""))
|
|
|
|
)
|
2019-10-31 19:20:22 +00:00
|
|
|
.unwrap(),
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::row(indexmap! {
|
2019-10-31 19:20:22 +00:00
|
|
|
"name".into() => string("Andrés N. Robalino")
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:08:04 +00:00
|
|
|
#[test]
|
2019-10-20 11:55:56 +00:00
|
|
|
fn replaces_matching_field_from_a_row() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[string("amigos")]);
|
2019-10-18 12:08:04 +00:00
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let sample = UntaggedValue::row(indexmap! {
|
2019-12-31 07:36:08 +00:00
|
|
|
"amigos".into() => table(&[
|
2019-10-20 11:55:56 +00:00
|
|
|
string("andres"),
|
|
|
|
string("jonathan"),
|
|
|
|
string("yehuda"),
|
|
|
|
]),
|
|
|
|
});
|
2019-10-18 12:08:04 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
let replacement = string("jonas");
|
2019-10-20 11:55:56 +00:00
|
|
|
|
|
|
|
let actual = sample
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_untagged_value()
|
2019-11-04 15:47:03 +00:00
|
|
|
.replace_data_at_column_path(&field_path, replacement)
|
2019-10-20 11:55:56 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(actual, row(indexmap! {"amigos".into() => string("jonas")}));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_matching_field_from_nested_rows_inside_a_row() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[
|
2019-10-20 11:55:56 +00:00
|
|
|
string("package"),
|
|
|
|
string("authors"),
|
|
|
|
string("los.3.caballeros"),
|
|
|
|
]);
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let sample = UntaggedValue::row(indexmap! {
|
2019-10-20 11:55:56 +00:00
|
|
|
"package".into() => row(indexmap! {
|
|
|
|
"authors".into() => row(indexmap! {
|
2019-12-31 07:36:08 +00:00
|
|
|
"los.3.mosqueteros".into() => table(&[string("andres::yehuda::jonathan")]),
|
|
|
|
"los.3.amigos".into() => table(&[string("andres::yehuda::jonathan")]),
|
|
|
|
"los.3.caballeros".into() => table(&[string("andres::yehuda::jonathan")])
|
2019-10-20 11:55:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-12-31 07:36:08 +00:00
|
|
|
let replacement = table(&[string("yehuda::jonathan::andres")]);
|
2019-11-21 14:33:14 +00:00
|
|
|
let tag = replacement.tag.clone();
|
2019-10-20 11:55:56 +00:00
|
|
|
|
|
|
|
let actual = sample
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag.clone())
|
2019-11-04 15:47:03 +00:00
|
|
|
.replace_data_at_column_path(&field_path, replacement.clone())
|
2019-10-20 11:55:56 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::row(indexmap! {
|
2019-10-20 11:55:56 +00:00
|
|
|
"package".into() => row(indexmap! {
|
|
|
|
"authors".into() => row(indexmap! {
|
2019-12-31 07:36:08 +00:00
|
|
|
"los.3.mosqueteros".into() => table(&[string("andres::yehuda::jonathan")]),
|
|
|
|
"los.3.amigos".into() => table(&[string("andres::yehuda::jonathan")]),
|
|
|
|
"los.3.caballeros".into() => replacement})})})
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag)
|
2019-10-20 11:55:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn replaces_matching_field_from_rows_inside_a_table() {
|
2019-12-31 07:36:08 +00:00
|
|
|
let field_path = column_path(&[
|
2019-10-20 11:55:56 +00:00
|
|
|
string("shell_policy"),
|
|
|
|
string("releases"),
|
|
|
|
string("nu.version.arepa"),
|
|
|
|
]);
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
let sample = UntaggedValue::row(indexmap! {
|
2019-10-20 11:55:56 +00:00
|
|
|
"shell_policy".into() => row(indexmap! {
|
2019-12-31 07:36:08 +00:00
|
|
|
"releases".into() => table(&[
|
2019-10-20 11:55:56 +00:00
|
|
|
row(indexmap! {
|
|
|
|
"nu.version.arepa".into() => row(indexmap! {
|
|
|
|
"code".into() => string("0.4.0"), "tag_line".into() => string("GitHub-era")
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
row(indexmap! {
|
|
|
|
"nu.version.taco".into() => row(indexmap! {
|
|
|
|
"code".into() => string("0.3.0"), "tag_line".into() => string("GitHub-era")
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
row(indexmap! {
|
|
|
|
"nu.version.stable".into() => row(indexmap! {
|
|
|
|
"code".into() => string("0.2.0"), "tag_line".into() => string("GitHub-era")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
])
|
2019-10-18 12:08:04 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
let replacement = row(indexmap! {
|
2019-10-20 11:55:56 +00:00
|
|
|
"code".into() => string("0.5.0"),
|
|
|
|
"tag_line".into() => string("CABALLEROS")
|
2019-11-21 14:33:14 +00:00
|
|
|
});
|
|
|
|
let tag = replacement.tag.clone();
|
2019-10-20 11:55:56 +00:00
|
|
|
|
|
|
|
let actual = sample
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag.clone())
|
2019-11-04 15:47:03 +00:00
|
|
|
.replace_data_at_column_path(&field_path, replacement.clone())
|
2019-10-20 11:55:56 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::row(indexmap! {
|
2019-10-20 11:55:56 +00:00
|
|
|
"shell_policy".into() => row(indexmap! {
|
2019-12-31 07:36:08 +00:00
|
|
|
"releases".into() => table(&[
|
2019-10-20 11:55:56 +00:00
|
|
|
row(indexmap! {
|
2019-11-21 14:33:14 +00:00
|
|
|
"nu.version.arepa".into() => replacement
|
2019-10-20 11:55:56 +00:00
|
|
|
}),
|
|
|
|
row(indexmap! {
|
|
|
|
"nu.version.taco".into() => row(indexmap! {
|
|
|
|
"code".into() => string("0.3.0"), "tag_line".into() => string("GitHub-era")
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
row(indexmap! {
|
|
|
|
"nu.version.stable".into() => row(indexmap! {
|
|
|
|
"code".into() => string("0.2.0"), "tag_line".into() => string("GitHub-era")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
])
|
|
|
|
})
|
2019-11-21 14:33:14 +00:00
|
|
|
}).into_value(&tag)
|
2019-10-20 11:55:56 +00:00
|
|
|
);
|
2019-10-18 12:08:04 +00:00
|
|
|
}
|
|
|
|
}
|