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 crate::data::base::coerce_compare;
|
|
|
|
use crate::data::base::shape::{Column, InlineShape, TypeShape};
|
|
|
|
use crate::data::primitive::style_primitive;
|
|
|
|
use chrono::DateTime;
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_parser::Operator;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{Primitive, 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
|
|
|
use nu_source::{DebugDocBuilder, PrettyDebug, Tagged};
|
|
|
|
|
|
|
|
pub fn date_from_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);
|
|
|
|
|
|
|
|
Ok(UntaggedValue::Primitive(Primitive::Date(date)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compare_values(
|
|
|
|
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)> {
|
|
|
|
match operator {
|
|
|
|
_ => {
|
|
|
|
let coerced = coerce_compare(left, right)?;
|
|
|
|
let ordering = coerced.compare();
|
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
|
|
let result = match (operator, ordering) {
|
|
|
|
(Operator::Equal, Ordering::Equal) => true,
|
|
|
|
(Operator::NotEqual, Ordering::Less) | (Operator::NotEqual, Ordering::Greater) => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
(Operator::LessThan, Ordering::Less) => true,
|
|
|
|
(Operator::GreaterThan, Ordering::Greater) => true,
|
|
|
|
(Operator::GreaterThanOrEqual, Ordering::Greater)
|
|
|
|
| (Operator::GreaterThanOrEqual, Ordering::Equal) => true,
|
|
|
|
(Operator::LessThanOrEqual, Ordering::Less)
|
|
|
|
| (Operator::LessThanOrEqual, Ordering::Equal) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_type<'a>(value: impl Into<&'a UntaggedValue>, width: usize) -> String {
|
|
|
|
TypeShape::from_value(value.into()).colored_string(width)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_leaf<'a>(value: impl Into<&'a UntaggedValue>) -> DebugDocBuilder {
|
|
|
|
InlineShape::from_value(value.into()).format().pretty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn style_leaf<'a>(value: impl Into<&'a UntaggedValue>) -> &'static str {
|
|
|
|
match value.into() {
|
|
|
|
UntaggedValue::Primitive(p) => style_primitive(p),
|
|
|
|
_ => "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|