2020-08-18 09:00:02 +02:00
|
|
|
use crate::base::coerce_compare;
|
|
|
|
use crate::base::shape::{Column, InlineShape};
|
|
|
|
use crate::primitive::style_primitive;
|
2020-08-04 00:47:19 +02:00
|
|
|
use chrono::{DateTime, NaiveDate, Utc};
|
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 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-04-18 03:50:58 +02:00
|
|
|
use nu_protocol::hir::Operator;
|
|
|
|
use nu_protocol::ShellTypeName;
|
2019-12-04 22:14:52 +01:00
|
|
|
use nu_protocol::{Primitive, Type, UntaggedValue};
|
2020-07-10 19:48:11 +02:00
|
|
|
use nu_source::{DebugDocBuilder, PrettyDebug, Span, Tagged};
|
2020-06-20 05:41:53 +02:00
|
|
|
use nu_table::TextStyle;
|
2021-01-29 14:44:02 +01:00
|
|
|
use num_traits::{ToPrimitive, Zero};
|
2020-09-01 07:09:55 +02:00
|
|
|
use std::collections::HashMap;
|
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 03:30:48 +01:00
|
|
|
|
2020-08-04 00:47:19 +02:00
|
|
|
pub struct Date;
|
|
|
|
|
|
|
|
impl Date {
|
|
|
|
pub fn from_regular_str(s: Tagged<&str>) -> Result<UntaggedValue, ShellError> {
|
|
|
|
let date = DateTime::parse_from_rfc3339(s.item).map_err(|err| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
&format!("Date parse error: {}", err),
|
|
|
|
"original value",
|
|
|
|
s.tag,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let date = date.with_timezone(&chrono::offset::Utc);
|
|
|
|
|
2020-12-12 19:18:03 +01:00
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Date(date.into())))
|
2020-08-04 00:47:19 +02: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 03:30:48 +01:00
|
|
|
|
2020-08-04 00:47:19 +02:00
|
|
|
pub fn naive_from_str(s: Tagged<&str>) -> Result<UntaggedValue, ShellError> {
|
|
|
|
let date = NaiveDate::parse_from_str(s.item, "%Y-%m-%d").map_err(|reason| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
&format!("Date parse error: {}", reason),
|
|
|
|
"original value",
|
|
|
|
s.tag,
|
|
|
|
)
|
|
|
|
})?;
|
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 03:30:48 +01:00
|
|
|
|
2020-08-04 00:47:19 +02:00
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Date(
|
2020-12-12 19:18:03 +01:00
|
|
|
DateTime::<Utc>::from_utc(date.and_hms(12, 34, 56), Utc).into(),
|
2020-08-04 00:47:19 +02:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn date_from_str(s: Tagged<&str>) -> Result<UntaggedValue, ShellError> {
|
|
|
|
Date::from_regular_str(s)
|
2020-10-03 16:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn date_naive_from_str(s: Tagged<&str>) -> Result<UntaggedValue, ShellError> {
|
|
|
|
Date::naive_from_str(s)
|
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 03:30:48 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 06:18:24 +02:00
|
|
|
pub fn merge_values(
|
|
|
|
left: &UntaggedValue,
|
|
|
|
right: &UntaggedValue,
|
|
|
|
) -> Result<UntaggedValue, (&'static str, &'static str)> {
|
|
|
|
match (left, right) {
|
|
|
|
(UntaggedValue::Row(columns), UntaggedValue::Row(columns_b)) => {
|
|
|
|
Ok(UntaggedValue::Row(columns.merge_from(columns_b)))
|
|
|
|
}
|
|
|
|
(left, right) => Err((left.type_name(), right.type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 22:50:43 +02:00
|
|
|
fn zero_division_error() -> UntaggedValue {
|
|
|
|
UntaggedValue::Error(ShellError::untagged_runtime_error("division by zero"))
|
|
|
|
}
|
|
|
|
|
2020-08-12 15:43:40 +02:00
|
|
|
pub fn unsafe_compute_values(
|
|
|
|
operator: Operator,
|
|
|
|
left: &UntaggedValue,
|
|
|
|
right: &UntaggedValue,
|
|
|
|
) -> Result<UntaggedValue, (&'static str, &'static str)> {
|
|
|
|
let computed = compute_values(operator, left, right);
|
|
|
|
|
|
|
|
if computed.is_ok() {
|
|
|
|
return computed;
|
|
|
|
}
|
|
|
|
|
|
|
|
match (left, right) {
|
|
|
|
(UntaggedValue::Primitive(lhs), UntaggedValue::Primitive(rhs)) => match (lhs, rhs) {
|
|
|
|
(Primitive::Filesize(x), Primitive::Int(y)) => match operator {
|
2021-01-29 23:35:18 +01:00
|
|
|
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))),
|
|
|
|
Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Filesize(x / y))),
|
2020-08-12 15:43:40 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
(Primitive::Int(x), Primitive::Filesize(y)) => match operator {
|
2021-01-29 23:35:18 +01:00
|
|
|
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))),
|
2020-08-12 15:43:40 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 03:50:58 +02:00
|
|
|
pub fn compute_values(
|
|
|
|
operator: Operator,
|
|
|
|
left: &UntaggedValue,
|
|
|
|
right: &UntaggedValue,
|
|
|
|
) -> Result<UntaggedValue, (&'static str, &'static str)> {
|
|
|
|
match (left, right) {
|
|
|
|
(UntaggedValue::Primitive(lhs), UntaggedValue::Primitive(rhs)) => match (lhs, rhs) {
|
2020-07-11 04:17:37 +02:00
|
|
|
(Primitive::Filesize(x), Primitive::Filesize(y)) => {
|
2020-04-18 03:50:58 +02:00
|
|
|
let result = match operator {
|
|
|
|
Operator::Plus => Ok(x + y),
|
|
|
|
Operator::Minus => Ok(x - y),
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
2020-07-11 04:17:37 +02:00
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Filesize(result)))
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
2021-01-29 14:44:02 +01:00
|
|
|
(Primitive::Filesize(x), Primitive::Int(y)) => match operator {
|
2021-01-29 23:35:18 +01:00
|
|
|
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))),
|
|
|
|
Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Filesize(x / y))),
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
(Primitive::Int(x), Primitive::Filesize(y)) => match operator {
|
|
|
|
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))),
|
2021-01-29 14:44:02 +01:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
2020-04-18 03:50:58 +02:00
|
|
|
(Primitive::Int(x), Primitive::Int(y)) => match operator {
|
|
|
|
Operator::Plus => Ok(UntaggedValue::Primitive(Primitive::Int(x + y))),
|
|
|
|
Operator::Minus => Ok(UntaggedValue::Primitive(Primitive::Int(x - y))),
|
|
|
|
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Int(x * y))),
|
|
|
|
Operator::Divide => {
|
2020-06-21 22:50:43 +02:00
|
|
|
if y.is_zero() {
|
|
|
|
Ok(zero_division_error())
|
|
|
|
} else if x - (y * (x / y)) == num_bigint::BigInt::from(0) {
|
2020-04-18 03:50:58 +02:00
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Int(x / y)))
|
|
|
|
} else {
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Decimal(
|
|
|
|
bigdecimal::BigDecimal::from(x.clone())
|
|
|
|
/ bigdecimal::BigDecimal::from(y.clone()),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 02:12:55 +02:00
|
|
|
Operator::Modulo => {
|
|
|
|
if y.is_zero() {
|
|
|
|
Ok(zero_division_error())
|
|
|
|
} else {
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Int(x % y)))
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:44:02 +01:00
|
|
|
Operator::Pow => {
|
|
|
|
let prim_u32 = ToPrimitive::to_u32(y);
|
|
|
|
match prim_u32 {
|
|
|
|
Some(num) => Ok(UntaggedValue::Primitive(Primitive::Int(x.pow(num)))),
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}
|
|
|
|
}
|
2020-04-18 03:50:58 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
(Primitive::Decimal(x), Primitive::Int(y)) => {
|
|
|
|
let result = match operator {
|
|
|
|
Operator::Plus => Ok(x + bigdecimal::BigDecimal::from(y.clone())),
|
|
|
|
Operator::Minus => Ok(x - bigdecimal::BigDecimal::from(y.clone())),
|
|
|
|
Operator::Multiply => Ok(x * bigdecimal::BigDecimal::from(y.clone())),
|
2020-06-21 22:50:43 +02:00
|
|
|
Operator::Divide => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
Ok(x / bigdecimal::BigDecimal::from(y.clone()))
|
|
|
|
}
|
2021-01-26 19:42:34 +01:00
|
|
|
Operator::Modulo => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
Ok(x % bigdecimal::BigDecimal::from(y.clone()))
|
|
|
|
}
|
2021-01-29 14:44:02 +01:00
|
|
|
// leaving this here for the hope that bigdecimal will one day support pow/powf/fpow
|
|
|
|
// Operator::Pow => {
|
|
|
|
// let xp = bigdecimal::ToPrimitive::to_f64(x).unwrap_or(0.0);
|
|
|
|
// let yp = bigdecimal::ToPrimitive::to_f64(y).unwrap_or(0.0);
|
|
|
|
// let pow = bigdecimal::FromPrimitive::from_f64(xp.powf(yp));
|
|
|
|
// match pow {
|
|
|
|
// Some(p) => Ok(p),
|
|
|
|
// None => Err((left.type_name(), right.type_name())),
|
|
|
|
// }
|
|
|
|
// }
|
2020-04-18 03:50:58 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
|
|
|
|
}
|
|
|
|
(Primitive::Int(x), Primitive::Decimal(y)) => {
|
|
|
|
let result = match operator {
|
|
|
|
Operator::Plus => Ok(bigdecimal::BigDecimal::from(x.clone()) + y),
|
|
|
|
Operator::Minus => Ok(bigdecimal::BigDecimal::from(x.clone()) - y),
|
|
|
|
Operator::Multiply => Ok(bigdecimal::BigDecimal::from(x.clone()) * y),
|
2020-06-21 22:50:43 +02:00
|
|
|
Operator::Divide => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
Ok(bigdecimal::BigDecimal::from(x.clone()) / y)
|
|
|
|
}
|
2021-01-26 19:42:34 +01:00
|
|
|
Operator::Modulo => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
Ok(bigdecimal::BigDecimal::from(x.clone()) % y)
|
|
|
|
}
|
2021-01-29 14:44:02 +01:00
|
|
|
// big decimal doesn't support pow yet
|
|
|
|
// Operator::Pow => {
|
|
|
|
// let yp = bigdecimal::ToPrimitive::to_u32(y).unwrap_or(0);
|
|
|
|
// Ok(bigdecimal::BigDecimal::from(x.pow(yp)))
|
|
|
|
// }
|
2020-04-18 03:50:58 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
|
|
|
|
}
|
|
|
|
(Primitive::Decimal(x), Primitive::Decimal(y)) => {
|
|
|
|
let result = match operator {
|
|
|
|
Operator::Plus => Ok(x + y),
|
|
|
|
Operator::Minus => Ok(x - y),
|
|
|
|
Operator::Multiply => Ok(x * y),
|
2020-06-21 22:50:43 +02:00
|
|
|
Operator::Divide => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
Ok(x / y)
|
|
|
|
}
|
2021-01-26 19:42:34 +01:00
|
|
|
Operator::Modulo => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
Ok(x % y)
|
|
|
|
}
|
2021-01-29 14:44:02 +01:00
|
|
|
// big decimal doesn't support pow yet
|
|
|
|
// Operator::Pow => {
|
|
|
|
// let xp = bigdecimal::ToPrimitive::to_f64(x).unwrap_or(0.0);
|
|
|
|
// let yp = bigdecimal::ToPrimitive::to_f64(y).unwrap_or(0.0);
|
|
|
|
// let pow = bigdecimal::FromPrimitive::from_f64(xp.powf(yp));
|
|
|
|
// match pow {
|
|
|
|
// Some(p) => Ok(p),
|
|
|
|
// None => Err((left.type_name(), right.type_name())),
|
|
|
|
// }
|
|
|
|
// }
|
2020-04-18 03:50:58 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
|
|
|
|
}
|
2020-07-10 19:48:11 +02:00
|
|
|
(Primitive::Date(x), Primitive::Date(y)) => match operator {
|
|
|
|
Operator::Minus => Ok(UntaggedValue::Primitive(Primitive::from(
|
|
|
|
x.signed_duration_since(*y),
|
|
|
|
))),
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
(Primitive::Date(x), Primitive::Duration(_)) => {
|
2020-04-18 03:50:58 +02:00
|
|
|
let result = match operator {
|
2020-07-10 19:48:11 +02:00
|
|
|
Operator::Plus => {
|
|
|
|
// FIXME: Not sure if I could do something better with the Span.
|
2020-10-09 03:51:47 +02:00
|
|
|
match Primitive::into_chrono_duration(rhs.clone(), Span::unknown()) {
|
|
|
|
Ok(y) => match x.checked_add_signed(y) {
|
|
|
|
Some(value) => Ok(value),
|
|
|
|
None => Err(("Date", "Duration and date addition overflow")),
|
|
|
|
},
|
|
|
|
Err(_) => Err(("Date", "Duration overflow")),
|
|
|
|
}
|
2020-07-10 19:48:11 +02:00
|
|
|
}
|
2020-04-18 03:50:58 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Date(result)))
|
|
|
|
}
|
2020-05-11 03:44:49 +02:00
|
|
|
(Primitive::Duration(x), Primitive::Duration(y)) => {
|
|
|
|
let result = match operator {
|
|
|
|
Operator::Plus => Ok(x + y),
|
|
|
|
Operator::Minus => Ok(x - y),
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Duration(result)))
|
|
|
|
}
|
2020-09-13 01:56:05 +02:00
|
|
|
(Primitive::Int(x), Primitive::Duration(y)) => {
|
|
|
|
let result = match operator {
|
|
|
|
Operator::Plus => Ok(x + y),
|
|
|
|
Operator::Minus => Ok(x - y),
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Duration(result)))
|
|
|
|
}
|
|
|
|
(Primitive::Duration(x), Primitive::Decimal(y)) => {
|
|
|
|
let result = match operator {
|
|
|
|
Operator::Divide => {
|
|
|
|
if y.is_zero() {
|
|
|
|
return Ok(zero_division_error());
|
|
|
|
}
|
|
|
|
let y = y.as_bigint_and_exponent();
|
|
|
|
Ok(x / y.0)
|
|
|
|
}
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Duration(result)))
|
|
|
|
}
|
2020-04-18 03:50:58 +02:00
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
},
|
|
|
|
_ => Err((left.type_name(), right.type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 23:49:57 +02:00
|
|
|
/// If left is {{ Operator }} right
|
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 03:30:48 +01:00
|
|
|
pub fn compare_values(
|
2020-04-18 03:50:58 +02:00
|
|
|
operator: Operator,
|
2019-11-21 18:18:00 +01:00
|
|
|
left: &UntaggedValue,
|
|
|
|
right: &UntaggedValue,
|
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 03:30:48 +01:00
|
|
|
) -> Result<bool, (&'static str, &'static str)> {
|
2020-04-23 22:00:49 +02:00
|
|
|
let coerced = coerce_compare(left, right)?;
|
|
|
|
let ordering = coerced.compare();
|
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 03:30:48 +01:00
|
|
|
|
2020-04-23 22:00:49 +02:00
|
|
|
use std::cmp::Ordering;
|
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 03:30:48 +01:00
|
|
|
|
2020-08-03 00:34:33 +02:00
|
|
|
let result = matches!(
|
|
|
|
(operator, ordering),
|
|
|
|
(Operator::Equal, Ordering::Equal)
|
|
|
|
| (Operator::GreaterThan, Ordering::Greater)
|
|
|
|
| (Operator::GreaterThanOrEqual, Ordering::Greater)
|
|
|
|
| (Operator::GreaterThanOrEqual, Ordering::Equal)
|
|
|
|
| (Operator::LessThan, Ordering::Less)
|
|
|
|
| (Operator::LessThanOrEqual, Ordering::Less)
|
|
|
|
| (Operator::LessThanOrEqual, Ordering::Equal)
|
|
|
|
| (Operator::NotEqual, Ordering::Greater)
|
|
|
|
| (Operator::NotEqual, Ordering::Less)
|
|
|
|
);
|
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 03:30:48 +01:00
|
|
|
|
2020-04-23 22:00:49 +02:00
|
|
|
Ok(result)
|
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 03:30:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_type<'a>(value: impl Into<&'a UntaggedValue>, width: usize) -> String {
|
2019-12-04 22:14:52 +01:00
|
|
|
Type::from_value(value.into()).colored_string(width)
|
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 03:30:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_leaf<'a>(value: impl Into<&'a UntaggedValue>) -> DebugDocBuilder {
|
|
|
|
InlineShape::from_value(value.into()).format().pretty()
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:09:55 +02:00
|
|
|
pub fn style_leaf<'a>(
|
|
|
|
value: impl Into<&'a UntaggedValue>,
|
2021-02-22 19:33:34 +01:00
|
|
|
color_hash_map: &HashMap<String, nu_ansi_term::Style>,
|
2020-09-01 07:09:55 +02:00
|
|
|
) -> TextStyle {
|
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 03:30:48 +01:00
|
|
|
match value.into() {
|
2020-09-01 07:09:55 +02:00
|
|
|
UntaggedValue::Primitive(p) => {
|
|
|
|
// This is just to return the name of the type so that style_primitive
|
|
|
|
// can work on a string versus a type like String("some_text")
|
|
|
|
let str: &str = &p.to_string();
|
|
|
|
let str_len = str.len();
|
|
|
|
let paren_index = str.find('(').unwrap_or(str_len - 1);
|
|
|
|
let prim_type = str[0..paren_index].to_string();
|
|
|
|
style_primitive(&prim_type, &color_hash_map)
|
|
|
|
}
|
2020-09-02 02:08:41 +02:00
|
|
|
_ => TextStyle::basic_left(),
|
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 03:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_for_column<'a>(
|
|
|
|
value: impl Into<&'a UntaggedValue>,
|
|
|
|
column: impl Into<Column>,
|
|
|
|
) -> DebugDocBuilder {
|
|
|
|
InlineShape::from_value(value.into())
|
|
|
|
.format_for_column(column)
|
|
|
|
.pretty()
|
|
|
|
}
|
2020-04-30 06:18:24 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-04 00:47:19 +02:00
|
|
|
use super::merge_values;
|
|
|
|
use super::Date as d;
|
2020-04-30 06:18:24 +02:00
|
|
|
use super::UntaggedValue as v;
|
2020-08-04 00:47:19 +02:00
|
|
|
use nu_source::TaggedItem;
|
2020-04-30 06:18:24 +02:00
|
|
|
|
2020-08-04 00:47:19 +02:00
|
|
|
use indexmap::indexmap;
|
2020-04-30 06:18:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merges_tables() {
|
2020-08-04 00:47:19 +02:00
|
|
|
let (author_1_date, author_2_date) = (
|
|
|
|
"2020-04-29".to_string().tagged_unknown(),
|
|
|
|
"2019-10-10".to_string().tagged_unknown(),
|
|
|
|
);
|
|
|
|
|
2020-04-30 06:18:24 +02:00
|
|
|
let table_author_row = v::row(indexmap! {
|
|
|
|
"name".into() => v::string("Andrés").into_untagged_value(),
|
|
|
|
"country".into() => v::string("EC").into_untagged_value(),
|
2020-08-04 00:47:19 +02:00
|
|
|
"date".into() => d::naive_from_str(author_1_date.borrow_tagged()).unwrap().into_untagged_value()
|
2020-04-30 06:18:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let other_table_author_row = v::row(indexmap! {
|
|
|
|
"name".into() => v::string("YK").into_untagged_value(),
|
|
|
|
"country".into() => v::string("US").into_untagged_value(),
|
2020-08-04 00:47:19 +02:00
|
|
|
"date".into() => d::naive_from_str(author_2_date.borrow_tagged()).unwrap().into_untagged_value()
|
2020-04-30 06:18:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
other_table_author_row,
|
|
|
|
merge_values(&table_author_row, &other_table_author_row).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|