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 mod column_path;
|
|
|
|
mod convert;
|
|
|
|
mod debug;
|
|
|
|
pub mod dict;
|
2020-09-29 05:32:29 +02:00
|
|
|
pub mod did_you_mean;
|
2020-04-26 19:30:52 +02:00
|
|
|
pub mod iter;
|
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 mod primitive;
|
2019-12-04 22:14:52 +01:00
|
|
|
pub mod range;
|
2020-10-13 04:44:28 +02:00
|
|
|
mod serde_bigdecimal;
|
|
|
|
mod serde_bigint;
|
2021-01-10 03:50:49 +01:00
|
|
|
pub mod value_structure;
|
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-13 09:59:57 +02:00
|
|
|
use crate::hir;
|
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::type_name::{ShellTypeName, SpannedTypeName};
|
|
|
|
use crate::value::dict::Dictionary;
|
2020-04-26 19:30:52 +02:00
|
|
|
use crate::value::iter::{RowValueIter, TableValueIter};
|
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::value::primitive::Primitive;
|
2019-12-04 22:14:52 +01:00
|
|
|
use crate::value::range::{Range, RangeInclusion};
|
2020-10-16 00:25:17 +02:00
|
|
|
use crate::ColumnPath;
|
2019-12-04 20:52:31 +01:00
|
|
|
use bigdecimal::BigDecimal;
|
2020-09-21 19:28:31 +02:00
|
|
|
use bigdecimal::FromPrimitive;
|
2020-12-12 19:18:03 +01:00
|
|
|
use chrono::{DateTime, FixedOffset, Utc};
|
2019-12-04 20:52:31 +01:00
|
|
|
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 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-10-16 00:25:17 +02:00
|
|
|
use nu_source::{AnchorLocation, HasSpan, Span, Spanned, SpannedItem, Tag};
|
2019-12-04 20:52:31 +01:00
|
|
|
use num_bigint::BigInt;
|
2020-04-24 19:28:38 +02:00
|
|
|
use num_traits::ToPrimitive;
|
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 serde::{Deserialize, Serialize};
|
2020-07-18 19:19:03 +02:00
|
|
|
use std::hash::{Hash, Hasher};
|
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 std::path::PathBuf;
|
2019-12-04 20:52:31 +01:00
|
|
|
use std::time::SystemTime;
|
2020-07-30 06:54:55 +02:00
|
|
|
|
2021-05-12 03:01:31 +02:00
|
|
|
#[cfg(feature = "dataframe")]
|
|
|
|
use crate::dataframe::NuDataFrame;
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// The core structured values that flow through a pipeline
|
2019-12-31 05:05:02 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
|
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 enum UntaggedValue {
|
2020-01-18 20:42:36 +01:00
|
|
|
/// A primitive (or fundamental) type of values
|
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
|
|
|
Primitive(Primitive),
|
2020-05-11 04:47:27 +02:00
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// A table row
|
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
|
|
|
Row(Dictionary),
|
2020-05-11 04:47:27 +02:00
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// A full inner (or embedded) table
|
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
|
|
|
Table(Vec<Value>),
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// An error value that represents an error that occurred as the values in the pipeline were built
|
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
|
|
|
Error(ShellError),
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
/// A block of Nu code, eg `{ ls | get name ; echo "done" }` with its captured values
|
|
|
|
Block(Box<hir::CapturedBlock>),
|
2021-05-12 03:01:31 +02:00
|
|
|
|
|
|
|
/// NuDataframe
|
|
|
|
#[cfg(feature = "dataframe")]
|
|
|
|
Dataframe(NuDataFrame),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl UntaggedValue {
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Get the corresponding descriptors (column names) associated with this 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 03:30:48 +01:00
|
|
|
pub fn data_descriptors(&self) -> Vec<String> {
|
|
|
|
match self {
|
2019-12-31 08:36:08 +01:00
|
|
|
UntaggedValue::Row(columns) => columns.entries.keys().map(|x| x.to_string()).collect(),
|
2020-08-03 19:43:27 +02:00
|
|
|
_ => vec![],
|
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-01-18 20:42:36 +01:00
|
|
|
/// Convert this UntaggedValue to a Value with the given 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
|
|
|
pub fn into_value(self, tag: impl Into<Tag>) -> Value {
|
|
|
|
Value {
|
|
|
|
value: self,
|
|
|
|
tag: tag.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Convert this UntaggedValue into a Value with an empty 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
|
|
|
pub fn into_untagged_value(self) -> Value {
|
|
|
|
Value {
|
|
|
|
value: self,
|
|
|
|
tag: Tag::unknown(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Returns true if this value represents boolean true
|
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 is_true(&self) -> bool {
|
2020-07-15 02:11:41 +02:00
|
|
|
matches!(self, UntaggedValue::Primitive(Primitive::Boolean(true)))
|
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
|
|
|
/// Returns true if this value represents a filesize
|
|
|
|
pub fn is_filesize(&self) -> bool {
|
|
|
|
matches!(self, UntaggedValue::Primitive(Primitive::Filesize(_)))
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:37:47 +02:00
|
|
|
/// Returns true if this value represents a duration
|
2020-09-13 01:56:05 +02:00
|
|
|
pub fn is_duration(&self) -> bool {
|
|
|
|
matches!(self, UntaggedValue::Primitive(Primitive::Duration(_)))
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:33:50 +02:00
|
|
|
/// Returns true if this value represents a table
|
|
|
|
pub fn is_table(&self) -> bool {
|
2020-07-15 02:11:41 +02:00
|
|
|
matches!(self, UntaggedValue::Table(_))
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 03:04:19 +02:00
|
|
|
/// Returns true if this value represents a row
|
|
|
|
pub fn is_row(&self) -> bool {
|
|
|
|
matches!(self, UntaggedValue::Row(_))
|
|
|
|
}
|
|
|
|
|
2020-07-20 19:31:58 +02:00
|
|
|
/// Returns true if this value represents a string
|
|
|
|
pub fn is_string(&self) -> bool {
|
|
|
|
matches!(self, UntaggedValue::Primitive(Primitive::String(_)))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Returns true if the value represents something other than Nothing
|
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 is_some(&self) -> bool {
|
|
|
|
!self.is_none()
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Returns true if the value represents Nothing
|
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 is_none(&self) -> bool {
|
2020-07-15 02:11:41 +02:00
|
|
|
matches!(self, UntaggedValue::Primitive(Primitive::Nothing))
|
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-01-18 20:42:36 +01:00
|
|
|
/// Returns true if the value represents an error
|
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 is_error(&self) -> bool {
|
2020-07-15 02:11:41 +02:00
|
|
|
matches!(self, UntaggedValue::Error(_err))
|
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-01-18 20:42:36 +01:00
|
|
|
/// Expect this value to be an error and return it
|
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 expect_error(&self) -> ShellError {
|
|
|
|
match self {
|
|
|
|
UntaggedValue::Error(err) => err.clone(),
|
|
|
|
_ => panic!("Don't call expect_error without first calling is_error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Expect this value to be a string and return it
|
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 expect_string(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(string)) => &string[..],
|
|
|
|
_ => panic!("expect_string assumes that the value must be a string"),
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 20:52:31 +01:00
|
|
|
|
2020-04-24 19:28:38 +02:00
|
|
|
/// Expect this value to be an integer and return it
|
|
|
|
pub fn expect_int(&self) -> i64 {
|
|
|
|
let big_int = match self {
|
|
|
|
UntaggedValue::Primitive(Primitive::Int(int)) => Some(int),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match big_int.and_then(|i| i.to_i64()) {
|
|
|
|
Some(i) => i,
|
|
|
|
_ => panic!("expect_int assumes that the value must be a integer"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating row values
|
2019-12-04 20:52:31 +01:00
|
|
|
pub fn row(entries: IndexMap<String, Value>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Row(entries.into())
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating table values
|
2019-12-31 08:36:08 +01:00
|
|
|
pub fn table(list: &[Value]) -> UntaggedValue {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::Table(list.to_vec())
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating string values
|
2019-12-04 20:52:31 +01:00
|
|
|
pub fn string(s: impl Into<String>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(s.into()))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating column-path values
|
2020-10-20 11:07:13 +02:00
|
|
|
pub fn column_path(s: &str, span: Span) -> UntaggedValue {
|
|
|
|
let s = s.to_string().spanned(span);
|
2020-10-16 00:25:17 +02:00
|
|
|
|
|
|
|
UntaggedValue::Primitive(Primitive::ColumnPath(ColumnPath::build(&s)))
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating integer values
|
2021-05-14 10:35:09 +02:00
|
|
|
pub fn int(i: impl Into<i64>) -> UntaggedValue {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::Int(i.into()))
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:35:09 +02:00
|
|
|
/// Helper for creating big integer values
|
|
|
|
pub fn big_int(i: impl Into<BigInt>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::BigInt(i.into()))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating glob pattern values
|
2021-01-08 08:30:41 +01:00
|
|
|
pub fn glob_pattern(s: impl Into<String>) -> UntaggedValue {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::String(s.into()))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating filepath values
|
2021-01-08 08:30:41 +01:00
|
|
|
pub fn filepath(s: impl Into<PathBuf>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::FilePath(s.into()))
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-07-11 04:17:37 +02:00
|
|
|
/// Helper for creating filesize values
|
2021-05-14 10:35:09 +02:00
|
|
|
pub fn filesize(s: u64) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Filesize(s))
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating decimal values
|
2020-10-20 11:07:13 +02:00
|
|
|
pub fn decimal(s: impl Into<BigDecimal>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Decimal(s.into()))
|
2020-09-21 19:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper for creating decimal values
|
|
|
|
pub fn decimal_from_float(f: f64, span: Span) -> UntaggedValue {
|
|
|
|
let dec = BigDecimal::from_f64(f);
|
|
|
|
|
2020-10-09 08:27:01 +02:00
|
|
|
// BigDecimal doesn't have the concept of inf/NaN so handle manually
|
|
|
|
if f.is_sign_negative() && f.is_infinite() {
|
|
|
|
UntaggedValue::from("-inf")
|
|
|
|
} else if f.is_infinite() {
|
|
|
|
UntaggedValue::from("inf")
|
|
|
|
} else if f.is_nan() {
|
|
|
|
UntaggedValue::from("NaN")
|
|
|
|
} else {
|
|
|
|
match dec {
|
|
|
|
Some(dec) => UntaggedValue::Primitive(Primitive::Decimal(dec)),
|
|
|
|
None => UntaggedValue::Error(ShellError::labeled_error(
|
|
|
|
"Can not convert f64 to big decimal",
|
|
|
|
"can not create decimal",
|
|
|
|
span,
|
|
|
|
)),
|
|
|
|
}
|
2020-09-21 19:28:31 +02:00
|
|
|
}
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating binary (non-text) buffer values
|
2019-12-04 20:52:31 +01:00
|
|
|
pub fn binary(binary: Vec<u8>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Binary(binary))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating range values
|
2019-12-04 22:14:52 +01:00
|
|
|
pub fn range(
|
|
|
|
left: (Spanned<Primitive>, RangeInclusion),
|
|
|
|
right: (Spanned<Primitive>, RangeInclusion),
|
|
|
|
) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Range(Box::new(Range::new(left, right))))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating boolean values
|
2020-09-14 03:15:44 +02:00
|
|
|
pub fn boolean(b: impl Into<bool>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Boolean(b.into()))
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating date duration values
|
2020-12-15 17:37:12 +01:00
|
|
|
pub fn duration(nanos: impl Into<BigInt>) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Duration(nanos.into()))
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating datatime values
|
2019-12-04 20:52:31 +01:00
|
|
|
pub fn system_date(s: SystemTime) -> UntaggedValue {
|
2020-12-12 19:18:03 +01:00
|
|
|
let utc: DateTime<Utc> = s.into();
|
|
|
|
UntaggedValue::Primitive(Primitive::Date(utc.into()))
|
2019-12-04 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 19:18:03 +01:00
|
|
|
pub fn date(d: impl Into<DateTime<FixedOffset>>) -> UntaggedValue {
|
2020-02-13 16:47:04 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::Date(d.into()))
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Helper for creating the Nothing value
|
2019-12-04 20:52:31 +01:00
|
|
|
pub fn nothing() -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(Primitive::Nothing)
|
|
|
|
}
|
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-01-18 20:42:36 +01:00
|
|
|
/// The fundamental structured value that flows through the pipeline, with associated metadata
|
2020-07-18 19:19:03 +02:00
|
|
|
#[derive(Debug, Clone, PartialOrd, Ord, Eq, Serialize, Deserialize)]
|
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 struct Value {
|
|
|
|
pub value: UntaggedValue,
|
|
|
|
pub tag: Tag,
|
|
|
|
}
|
|
|
|
|
2020-07-18 19:19:03 +02:00
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.value == other.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for Value {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.value.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Overload deferencing to give back the UntaggedValue inside of a 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 03:30:48 +01:00
|
|
|
impl std::ops::Deref for Value {
|
|
|
|
type Target = UntaggedValue;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
2020-10-26 15:25:06 +01:00
|
|
|
/// Helper to create a Value
|
|
|
|
pub fn new(untagged_value: UntaggedValue, the_tag: Tag) -> Self {
|
|
|
|
Value {
|
|
|
|
value: untagged_value,
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Get the corresponding anchor (originating location) for the 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 03:30:48 +01:00
|
|
|
pub fn anchor(&self) -> Option<AnchorLocation> {
|
|
|
|
self.tag.anchor()
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Get the name (url, filepath, etc) behind an anchor for the 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 03:30:48 +01:00
|
|
|
pub fn anchor_name(&self) -> Option<String> {
|
|
|
|
self.tag.anchor_name()
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Get the metadata for the 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 03:30:48 +01:00
|
|
|
pub fn tag(&self) -> Tag {
|
|
|
|
self.tag.clone()
|
|
|
|
}
|
|
|
|
|
2021-04-12 04:35:01 +02:00
|
|
|
pub fn error(e: ShellError) -> Value {
|
|
|
|
UntaggedValue::Error(e).into_untagged_value()
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// View the Value as a string, if possible
|
2019-12-03 07:44:59 +01:00
|
|
|
pub fn as_string(&self) -> Result<String, ShellError> {
|
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 &self.value {
|
2019-12-03 07:44:59 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::String(string)) => Ok(string.clone()),
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::FilePath(path)) => {
|
2020-04-20 23:45:11 +02:00
|
|
|
Ok(path.to_string_lossy().to_string())
|
|
|
|
}
|
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
|
|
|
_ => Err(ShellError::type_error("string", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 03:06:11 +01:00
|
|
|
/// View the Value as a FilePath (PathBuf), if possible
|
|
|
|
pub fn as_filepath(&self) -> Result<PathBuf, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::FilePath(path)) => Ok(path.clone()),
|
2021-04-12 01:53:31 +02:00
|
|
|
_ => Err(ShellError::type_error("path", self.spanned_type_name())),
|
2021-02-03 03:06:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:35:09 +02:00
|
|
|
/// View the Value as a Int, if possible
|
|
|
|
pub fn as_int(&self) -> Result<i64, ShellError> {
|
2021-02-03 03:06:11 +01:00
|
|
|
match &self.value {
|
2021-05-14 10:35:09 +02:00
|
|
|
UntaggedValue::Primitive(Primitive::Int(n)) => Ok(*n),
|
2021-02-03 03:06:11 +01:00
|
|
|
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:35:09 +02:00
|
|
|
/// View the Value as a Int, if possible
|
|
|
|
pub fn as_big_int(&self) -> Result<BigInt, ShellError> {
|
2021-02-03 03:06:11 +01:00
|
|
|
match &self.value {
|
2021-05-14 10:35:09 +02:00
|
|
|
UntaggedValue::Primitive(Primitive::BigInt(n)) => Ok(n.clone()),
|
2021-02-03 03:06:11 +01:00
|
|
|
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:35:09 +02:00
|
|
|
/// View the Value as a Filesize (u64), if possible
|
|
|
|
pub fn as_filesize(&self) -> Result<u64, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Filesize(fs)) => Ok(*fs),
|
|
|
|
_ => Err(ShellError::type_error("int", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 03:06:11 +01:00
|
|
|
/// View the Value as a Duration (BigInt), if possible
|
|
|
|
pub fn as_duration(&self) -> Result<BigInt, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Duration(dur)) => Ok(dur.clone()),
|
|
|
|
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// View the Value as a Decimal (BigDecimal), if possible
|
|
|
|
pub fn as_decimal(&self) -> Result<BigDecimal, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Decimal(d)) => Ok(d.clone()),
|
|
|
|
_ => Err(ShellError::type_error(
|
|
|
|
"bigdecimal",
|
|
|
|
self.spanned_type_name(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 18:30:58 +02:00
|
|
|
pub fn convert_to_string(&self) -> String {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(s)) => s.clone(),
|
2020-08-04 00:47:19 +02:00
|
|
|
UntaggedValue::Primitive(Primitive::Date(dt)) => dt.format("%Y-%m-%d").to_string(),
|
2020-05-21 18:30:58 +02:00
|
|
|
UntaggedValue::Primitive(Primitive::Boolean(x)) => format!("{}", x),
|
|
|
|
UntaggedValue::Primitive(Primitive::Decimal(x)) => format!("{}", x),
|
|
|
|
UntaggedValue::Primitive(Primitive::Int(x)) => format!("{}", x),
|
2020-07-11 04:17:37 +02:00
|
|
|
UntaggedValue::Primitive(Primitive::Filesize(x)) => format!("{}", x),
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::FilePath(x)) => format!("{}", x.display()),
|
2020-05-21 18:30:58 +02:00
|
|
|
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => {
|
2020-09-29 05:33:43 +02:00
|
|
|
let joined: String = path
|
2020-05-21 18:30:58 +02:00
|
|
|
.iter()
|
2020-09-29 05:33:43 +02:00
|
|
|
.map(|member| member.as_string())
|
|
|
|
.collect::<Vec<String>>()
|
2020-05-21 18:30:58 +02:00
|
|
|
.join(".");
|
|
|
|
|
|
|
|
if joined.contains(' ') {
|
|
|
|
format!("\"{}\"", joined)
|
|
|
|
} else {
|
|
|
|
joined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => String::from(""),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:15:47 +02:00
|
|
|
pub fn format(&self, fmt: &str) -> Result<String, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Date(dt)) => Ok(dt.format(fmt).to_string()),
|
|
|
|
_ => Err(ShellError::type_error("date", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// View into the borrowed string contents of a Value, if possible
|
2019-12-02 17:14:05 +01:00
|
|
|
pub fn as_forgiving_string(&self) -> Result<&str, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(string)) => Ok(&string[..]),
|
|
|
|
_ => Err(ShellError::type_error("string", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// View the Value as a path, if possible
|
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 as_path(&self) -> Result<PathBuf, ShellError> {
|
|
|
|
match &self.value {
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::FilePath(path)) => Ok(path.clone()),
|
2019-12-31 08:36:08 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::String(path_str)) => Ok(PathBuf::from(&path_str)),
|
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
|
|
|
_ => Err(ShellError::type_error("Path", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 22:14:52 +01:00
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// View the Value as a Primitive value, if possible
|
2019-12-04 22:14:52 +01:00
|
|
|
pub fn as_primitive(&self) -> Result<Primitive, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => Ok(primitive.clone()),
|
|
|
|
_ => Err(ShellError::type_error(
|
|
|
|
"Primitive",
|
|
|
|
self.spanned_type_name(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:34:41 +02:00
|
|
|
/// View the Value as a Primitive value, if possible
|
|
|
|
pub fn is_primitive(&self) -> bool {
|
2020-07-15 02:11:41 +02:00
|
|
|
matches!(&self.value, UntaggedValue::Primitive(_))
|
2020-06-12 10:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:58:18 +02:00
|
|
|
/// View the Value as char, if possible
|
|
|
|
pub fn as_char(&self) -> Result<char, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_char(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("char", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:15:36 +02:00
|
|
|
/// View the Value as unsigned size, if possible
|
|
|
|
pub fn as_usize(&self) -> Result<usize, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_usize(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// View the Value as unsigned 64-bit, if possible
|
2019-12-04 22:14:52 +01:00
|
|
|
pub fn as_u64(&self) -> Result<u64, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_u64(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 23:46:18 +01:00
|
|
|
|
2020-11-11 21:35:02 +01:00
|
|
|
/// View the Value as signed 64-bit, if possible
|
|
|
|
pub fn as_i64(&self) -> Result<i64, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_i64(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 05:58:18 +02:00
|
|
|
/// View the Value as unsigned 32-bit, if possible
|
|
|
|
pub fn as_u32(&self) -> Result<u32, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_u32(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// View the Value as signed 32-bit, if possible
|
2021-03-06 06:46:27 +01:00
|
|
|
pub fn as_i32(&self) -> Result<i32, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_i32(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 05:58:18 +02:00
|
|
|
/// View the Value as signed 16-bit, if possible
|
|
|
|
pub fn as_i16(&self) -> Result<i16, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_i16(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 03:01:31 +02:00
|
|
|
/// View the Value as signed 32-bit float, if possible
|
|
|
|
pub fn as_f32(&self) -> Result<f32, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_f32(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// View the Value as signed 64-bit float, if possible
|
|
|
|
pub fn as_f64(&self) -> Result<f64, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(primitive) => primitive.as_f64(self.tag.span),
|
|
|
|
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// View the Value as boolean, if possible
|
2020-01-17 23:46:18 +01:00
|
|
|
pub fn as_bool(&self) -> Result<bool, ShellError> {
|
|
|
|
match &self.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Boolean(p)) => Ok(*p),
|
|
|
|
_ => Err(ShellError::type_error("boolean", self.spanned_type_name())),
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 19:30:52 +02:00
|
|
|
|
|
|
|
/// Returns an iterator of the values rows
|
|
|
|
pub fn table_entries(&self) -> TableValueIter<'_> {
|
|
|
|
crate::value::iter::table_entries(&self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator of the value's cells
|
|
|
|
pub fn row_entries(&self) -> RowValueIter<'_> {
|
|
|
|
crate::value::iter::row_entries(&self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the value is empty
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
match &self {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(p),
|
|
|
|
..
|
|
|
|
} => p.is_empty(),
|
|
|
|
Value {
|
2020-09-12 08:40:52 +02:00
|
|
|
value: UntaggedValue::Table(rows),
|
2020-04-26 19:30:52 +02:00
|
|
|
..
|
2020-09-12 08:40:52 +02:00
|
|
|
} => rows.is_empty(),
|
2020-04-26 19:30:52 +02:00
|
|
|
r
|
|
|
|
@
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(_),
|
|
|
|
..
|
|
|
|
} => r.row_entries().all(|(_, value)| value.is_empty()),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 06:50:26 +02:00
|
|
|
|
|
|
|
pub fn nothing() -> Value {
|
|
|
|
UntaggedValue::nothing().into_untagged_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 03:30:48 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
impl From<String> for Value {
|
|
|
|
fn from(s: String) -> Value {
|
|
|
|
let end = s.len();
|
2020-01-24 20:16:41 +01:00
|
|
|
Value {
|
2020-05-18 14:56:01 +02:00
|
|
|
value: s.into(),
|
2020-01-24 20:16:41 +01:00
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
impl From<&str> for Value {
|
|
|
|
fn from(s: &str) -> Value {
|
|
|
|
let end = s.len();
|
|
|
|
Value {
|
|
|
|
value: s.into(),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 12:21:20 +02:00
|
|
|
impl From<bool> for Value {
|
|
|
|
fn from(s: bool) -> Value {
|
|
|
|
Value {
|
|
|
|
value: s.into(),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::unknown(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
impl<T> From<T> for UntaggedValue
|
|
|
|
where
|
|
|
|
T: Into<Primitive>,
|
|
|
|
{
|
|
|
|
/// Convert a Primitive to an UntaggedValue
|
|
|
|
fn from(input: T) -> UntaggedValue {
|
|
|
|
UntaggedValue::Primitive(input.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ShellError> for UntaggedValue {
|
|
|
|
fn from(e: ShellError) -> Self {
|
|
|
|
UntaggedValue::Error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UntaggedValue> for Value {
|
|
|
|
/// Convert an UntaggedValue into a Value with a default tag
|
|
|
|
fn from(value: UntaggedValue) -> Value {
|
|
|
|
Value {
|
|
|
|
value,
|
|
|
|
tag: Tag::default(),
|
|
|
|
}
|
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-05-18 14:56:01 +02:00
|
|
|
impl From<Value> for UntaggedValue {
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Convert a Value into an UntaggedValue
|
2020-05-18 14:56:01 +02:00
|
|
|
fn from(v: Value) -> UntaggedValue {
|
|
|
|
v.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 03:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Convert a borrowed Value into a borrowed UntaggedValue
|
2021-02-12 11:13:14 +01:00
|
|
|
impl<'a> From<&'a Value> for &'a UntaggedValue {
|
|
|
|
fn from(x: &'a Value) -> Self {
|
|
|
|
&x.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 03:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasSpan for Value {
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Return the corresponding Span for the 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 03:30:48 +01:00
|
|
|
fn span(&self) -> Span {
|
|
|
|
self.tag.span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ShellTypeName for Value {
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Get the type name for the 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 03:30:48 +01:00
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
ShellTypeName::type_name(&self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ShellTypeName for UntaggedValue {
|
2020-01-18 20:42:36 +01:00
|
|
|
/// Get the type name for the 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
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
match &self {
|
|
|
|
UntaggedValue::Primitive(p) => p.type_name(),
|
|
|
|
UntaggedValue::Row(_) => "row",
|
|
|
|
UntaggedValue::Table(_) => "table",
|
|
|
|
UntaggedValue::Error(_) => "error",
|
|
|
|
UntaggedValue::Block(_) => "block",
|
2021-05-12 03:01:31 +02:00
|
|
|
#[cfg(feature = "dataframe")]
|
|
|
|
UntaggedValue::Dataframe(_) => "dataframe",
|
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-03-08 06:33:30 +01:00
|
|
|
pub fn merge_descriptors(values: &[Value]) -> Vec<String> {
|
|
|
|
let mut ret: Vec<String> = vec![];
|
2020-05-11 04:47:27 +02:00
|
|
|
let value_column = "".to_string();
|
2020-03-08 06:33:30 +01:00
|
|
|
for value in values {
|
|
|
|
let descs = value.data_descriptors();
|
|
|
|
|
|
|
|
if descs.is_empty() {
|
|
|
|
if !ret.contains(&value_column) {
|
2020-05-11 04:47:27 +02:00
|
|
|
ret.push("".to_string());
|
2020-03-08 06:33:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for desc in value.data_descriptors() {
|
|
|
|
if !ret.contains(&desc) {
|
|
|
|
ret.push(desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
2020-10-09 08:27:01 +02:00
|
|
|
|
2020-10-26 15:25:06 +01:00
|
|
|
// Extensions
|
|
|
|
|
|
|
|
pub trait StringExt {
|
|
|
|
fn to_string_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_string_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_string_value_create_tag(&self) -> Value;
|
|
|
|
fn to_column_path_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_column_path_untagged_value(&self, span: Span) -> UntaggedValue;
|
|
|
|
fn to_pattern_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_pattern_untagged_value(&self) -> UntaggedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StringExt for String {
|
|
|
|
fn to_string_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(self.to_string())),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_string_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.len();
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(self.to_string())),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_string_untagged_value(&self) -> UntaggedValue {
|
|
|
|
UntaggedValue::string(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_column_path_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::ColumnPath(ColumnPath::build(
|
|
|
|
&the_tag.span.spanned_string(self), // this is suspect
|
|
|
|
))),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_column_path_untagged_value(&self, span: Span) -> UntaggedValue {
|
|
|
|
let s = self.to_string().spanned(span);
|
|
|
|
UntaggedValue::Primitive(Primitive::ColumnPath(ColumnPath::build(&s)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_pattern_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
2021-01-08 08:30:41 +01:00
|
|
|
value: UntaggedValue::Primitive(Primitive::GlobPattern(self.to_string())),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_pattern_untagged_value(&self) -> UntaggedValue {
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::glob_pattern(self)
|
2020-10-26 15:25:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait StrExt {
|
|
|
|
fn to_str_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_str_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_str_value_create_tag(&self) -> Value;
|
|
|
|
fn to_column_path_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_column_path_untagged_value(&self, span: Span) -> UntaggedValue;
|
|
|
|
fn to_pattern_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_pattern_untagged_value(&self) -> UntaggedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StrExt for &str {
|
|
|
|
fn to_str_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(self.to_string())),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_str_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.len();
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(self.to_string())),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_str_untagged_value(&self) -> UntaggedValue {
|
|
|
|
UntaggedValue::string(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_column_path_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::ColumnPath(ColumnPath::build(
|
|
|
|
&the_tag.span.spanned_string(self), // this is suspect
|
|
|
|
))),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_column_path_untagged_value(&self, span: Span) -> UntaggedValue {
|
|
|
|
let s = self.to_string().spanned(span);
|
|
|
|
UntaggedValue::Primitive(Primitive::ColumnPath(ColumnPath::build(&s)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_pattern_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
2021-01-08 08:30:41 +01:00
|
|
|
value: UntaggedValue::Primitive(Primitive::GlobPattern(self.to_string())),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_pattern_untagged_value(&self) -> UntaggedValue {
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::glob_pattern(*self)
|
2020-10-26 15:25:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait U64Ext {
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_value_create_tag(&self) -> Value;
|
|
|
|
fn to_duration_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_duration_value(&self, tag: Tag) -> Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl U64Ext for u64 {
|
|
|
|
fn to_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
2021-05-18 23:32:37 +02:00
|
|
|
value: UntaggedValue::Primitive(Primitive::Int(*self as i64)),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_duration_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Duration(BigInt::from(*self))),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.to_string().len();
|
|
|
|
Value {
|
2021-05-18 23:32:37 +02:00
|
|
|
value: UntaggedValue::Primitive(Primitive::Int(*self as i64)),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue {
|
2021-05-18 23:32:37 +02:00
|
|
|
UntaggedValue::int(*self as i64)
|
2020-10-26 15:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_duration_untagged_value(&self) -> UntaggedValue {
|
|
|
|
UntaggedValue::duration(BigInt::from(*self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait I64Ext {
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_value_create_tag(&self) -> Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl I64Ext for i64 {
|
|
|
|
fn to_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
2021-05-14 10:35:09 +02:00
|
|
|
value: UntaggedValue::Primitive(Primitive::Int(*self)),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.to_string().len();
|
|
|
|
Value {
|
2021-05-14 10:35:09 +02:00
|
|
|
value: UntaggedValue::Primitive(Primitive::Int(*self)),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue {
|
|
|
|
UntaggedValue::int(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DecimalExt {
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_value_create_tag(&self) -> Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecimalExt for f64 {
|
|
|
|
fn to_value(&self, the_tag: Tag) -> Value {
|
|
|
|
if let Some(f) = BigDecimal::from_f64(*self) {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Decimal(f)),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!("Internal error: protocol did not use f64-compatible decimal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.to_string().len();
|
|
|
|
if let Some(f) = BigDecimal::from_f64(*self) {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Decimal(f)),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!("Internal error: protocol did not use f64-compatible decimal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue {
|
|
|
|
if let Some(f) = BigDecimal::from_f64(*self) {
|
|
|
|
UntaggedValue::decimal(f)
|
|
|
|
} else {
|
|
|
|
unreachable!("Internal error: protocol did not use f64-compatible decimal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PathBufExt {
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_value_create_tag(&self) -> Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathBufExt for PathBuf {
|
|
|
|
fn to_value(&self, the_tag: Tag) -> Value {
|
|
|
|
let pb = self.clone();
|
|
|
|
Value {
|
2021-01-08 08:30:41 +01:00
|
|
|
value: UntaggedValue::Primitive(Primitive::FilePath(pb)),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_create_tag(&self) -> Value {
|
|
|
|
let end = self
|
|
|
|
.to_str()
|
|
|
|
.expect("unable to convert pathbuf to str")
|
|
|
|
.len();
|
|
|
|
let pb = self.clone();
|
|
|
|
Value {
|
2021-01-08 08:30:41 +01:00
|
|
|
value: UntaggedValue::Primitive(Primitive::FilePath(pb)),
|
2020-10-26 15:25:06 +01:00
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue {
|
|
|
|
let pb = self.clone();
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::filepath(pb)
|
2020-10-26 15:25:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait BooleanExt {
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_value_create_tag(&self) -> Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BooleanExt for bool {
|
|
|
|
fn to_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Boolean(*self)),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.to_string().len();
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Boolean(*self)),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue {
|
|
|
|
UntaggedValue::boolean(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DateTimeExt {
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue;
|
|
|
|
fn to_value(&self, tag: Tag) -> Value;
|
|
|
|
fn to_value_create_tag(&self) -> Value;
|
|
|
|
}
|
|
|
|
|
2020-12-12 19:18:03 +01:00
|
|
|
impl DateTimeExt for DateTime<FixedOffset> {
|
2020-10-26 15:25:06 +01:00
|
|
|
fn to_value(&self, the_tag: Tag) -> Value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Date(*self)),
|
|
|
|
tag: the_tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_create_tag(&self) -> Value {
|
|
|
|
let end = self.to_string().len();
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Date(*self)),
|
|
|
|
tag: Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_untagged_value(&self) -> UntaggedValue {
|
|
|
|
UntaggedValue::date(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 08:27:01 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-02-02 18:09:19 +01:00
|
|
|
use indexmap::indexmap;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_descriptors() {
|
|
|
|
let value = vec![
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h1".into() => Value::from("Ecuador")
|
|
|
|
})
|
|
|
|
.into_untagged_value(),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h2".into() => Value::from("Ecuador")
|
|
|
|
})
|
|
|
|
.into_untagged_value(),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h3".into() => Value::from("Ecuador")
|
|
|
|
})
|
|
|
|
.into_untagged_value(),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h1".into() => Value::from("Ecuador"),
|
|
|
|
"h4".into() => Value::from("Ecuador"),
|
|
|
|
})
|
|
|
|
.into_untagged_value(),
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
merge_descriptors(&value),
|
|
|
|
vec![
|
|
|
|
String::from("h1"),
|
|
|
|
String::from("h2"),
|
|
|
|
String::from("h3"),
|
|
|
|
String::from("h4")
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_data_descriptors() {
|
|
|
|
let value = vec![
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h1".into() => Value::from("Ecuador")
|
|
|
|
}),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h2".into() => Value::from("Ecuador")
|
|
|
|
}),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h3".into() => Value::from("Ecuador")
|
|
|
|
}),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"h1".into() => Value::from("Ecuador"),
|
|
|
|
"h4".into() => Value::from("Ecuador"),
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
value
|
|
|
|
.iter()
|
|
|
|
.map(|v| v.data_descriptors().len())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec![1, 1, 1, 2]
|
|
|
|
);
|
|
|
|
}
|
2020-10-09 08:27:01 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decimal_from_float() {
|
|
|
|
assert_eq!(
|
|
|
|
UntaggedValue::from("inf"),
|
|
|
|
UntaggedValue::decimal_from_float(f64::INFINITY, Span::default())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
UntaggedValue::from("-inf"),
|
|
|
|
UntaggedValue::decimal_from_float(f64::NEG_INFINITY, Span::default())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
UntaggedValue::from("NaN"),
|
|
|
|
UntaggedValue::decimal_from_float(f64::NAN, Span::default())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
UntaggedValue::from(5.5),
|
|
|
|
UntaggedValue::decimal_from_float(5.5, Span::default())
|
|
|
|
)
|
|
|
|
}
|
2020-10-26 15:25:06 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_to_string_untagged_value_extension() {
|
|
|
|
assert_eq!(
|
|
|
|
"a_string".to_string().to_string_untagged_value(),
|
|
|
|
UntaggedValue::from("a_string".to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_to_string_value_extension() {
|
|
|
|
let end = "a_string".to_string().len();
|
|
|
|
let the_tag = Tag {
|
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected = Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String("a_string".to_string())),
|
|
|
|
tag: the_tag.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!("a_string".to_string().to_string_value(the_tag), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_to_string_value_create_tag_extension() {
|
|
|
|
let end = "a_string".to_string().len();
|
2020-10-27 08:37:35 +01:00
|
|
|
let tag = Tag {
|
2020-10-26 15:25:06 +01:00
|
|
|
anchor: None,
|
|
|
|
span: Span::new(0, end),
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected = Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String("a_string".to_string())),
|
2020-10-27 08:37:35 +01:00
|
|
|
tag,
|
2020-10-26 15:25:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
"a_string".to_string().to_string_value_create_tag(),
|
|
|
|
expected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_to_pattern_untagged_value() {
|
|
|
|
let a_pattern = r"[a-zA-Z0-9 ]";
|
|
|
|
assert_eq!(
|
|
|
|
a_pattern.to_pattern_untagged_value(),
|
2021-01-08 08:30:41 +01:00
|
|
|
UntaggedValue::glob_pattern(a_pattern)
|
2020-10-26 15:25:06 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_to_column_path_untagged_value() {
|
|
|
|
let a_columnpath = "some_column_path";
|
|
|
|
let a_span = Span::new(0, a_columnpath.len());
|
|
|
|
assert_eq!(
|
|
|
|
a_columnpath.to_column_path_untagged_value(a_span),
|
|
|
|
UntaggedValue::column_path(a_columnpath, a_span)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_str_to_str_untaggged_value_extension() {
|
|
|
|
assert_eq!(
|
|
|
|
"a_str".to_str_untagged_value(),
|
|
|
|
UntaggedValue::from("a_str".to_string())
|
|
|
|
);
|
|
|
|
}
|
2020-10-09 08:27:01 +02:00
|
|
|
}
|