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 ;
use crate ::value ::column_path ::ColumnPath ;
2019-12-04 22:14:52 +01:00
use crate ::value ::range ::Range ;
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 ::{ serde_bigdecimal , serde_bigint } ;
use bigdecimal ::BigDecimal ;
use chrono ::{ DateTime , Utc } ;
2019-12-04 22:14:52 +01:00
use nu_errors ::{ ExpectedRange , ShellError } ;
use nu_source ::{ PrettyDebug , Span , SpannedItem } ;
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 num_bigint ::BigInt ;
2019-12-04 22:14:52 +01:00
use num_traits ::cast ::{ FromPrimitive , 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 } ;
use std ::path ::PathBuf ;
2020-01-15 17:28:31 +01:00
/// The most fundamental of structured values in Nu are the Primitive values. These values represent types like integers, strings, booleans, dates, etc that are then used
/// as the buildig blocks to build up more complex structures.
///
/// Primitives also include marker values BeginningOfStream and EndOfStream which denote a change of condition in the stream
2019-12-31 05:05:02 +01:00
#[ derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Deserialize, Serialize) ]
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 Primitive {
2020-01-15 17:28:31 +01:00
/// An empty 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
Nothing ,
2020-01-15 17:28:31 +01:00
/// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit)
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
#[ serde(with = " serde_bigint " ) ]
Int ( BigInt ) ,
2020-01-15 17:28:31 +01:00
/// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit)
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
#[ serde(with = " serde_bigdecimal " ) ]
Decimal ( BigDecimal ) ,
2020-01-15 17:28:31 +01:00
/// A count in the number of bytes, used as a filesize
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
Bytes ( u64 ) ,
2020-01-15 17:28:31 +01:00
/// A string 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
String ( String ) ,
2020-01-15 17:28:31 +01:00
/// A string value with an implied carriage return (or cr/lf) ending
2019-12-03 07:44:59 +01:00
Line ( String ) ,
2020-01-15 17:28:31 +01:00
/// A path to travel to reach a value in a 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
ColumnPath ( ColumnPath ) ,
2020-01-15 17:28:31 +01:00
/// A glob pattern, eg foo*
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
Pattern ( String ) ,
2020-01-15 17:28:31 +01:00
/// A boolean 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
Boolean ( bool ) ,
2020-01-15 17:28:31 +01:00
/// A date value, in UTC
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
Date ( DateTime < Utc > ) ,
2020-01-15 17:28:31 +01:00
/// A count in the number of seconds
Duration ( u64 ) ,
/// A range of values
2019-12-04 22:14:52 +01:00
Range ( Box < Range > ) ,
2020-01-15 17:28:31 +01:00
/// A file path
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
Path ( PathBuf ) ,
2020-01-15 17:28:31 +01:00
/// A vector of raw binary data
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
#[ serde(with = " serde_bytes " ) ]
Binary ( Vec < u8 > ) ,
2020-01-15 17:28:31 +01:00
/// Beginning of stream marker, a pseudo-value not intended for tables
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
BeginningOfStream ,
2020-01-15 17:28:31 +01:00
/// End of stream marker, a pseudo-value not intended for tables
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
EndOfStream ,
}
2019-12-04 22:14:52 +01:00
impl Primitive {
2020-01-15 17:28:31 +01:00
/// Converts a primitive value to a u64, if possible. Uses a span to build an error if the conversion isn't possible.
2019-12-04 22:14:52 +01:00
pub fn as_u64 ( & self , span : Span ) -> Result < u64 , ShellError > {
match self {
Primitive ::Int ( int ) = > match int . to_u64 ( ) {
None = > Err ( ShellError ::range_error (
ExpectedRange ::U64 ,
& format! ( " {} " , int ) . spanned ( span ) ,
" converting an integer into a 64-bit integer " ,
) ) ,
Some ( num ) = > Ok ( num ) ,
} ,
other = > Err ( ShellError ::type_error (
" integer " ,
other . type_name ( ) . spanned ( span ) ,
) ) ,
}
}
}
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 From < BigDecimal > for Primitive {
2020-01-15 17:28:31 +01:00
/// Helper to convert from decimals to a Primitive 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 from ( decimal : BigDecimal ) -> Primitive {
Primitive ::Decimal ( decimal )
}
}
impl From < f64 > for Primitive {
2020-01-15 17:28:31 +01:00
/// Helper to convert from 64-bit float to a Primitive 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 from ( float : f64 ) -> Primitive {
2020-01-04 07:44:17 +01:00
if let Some ( f ) = BigDecimal ::from_f64 ( float ) {
Primitive ::Decimal ( f )
} else {
unreachable! ( " Internal error: protocol did not use f64-compatible decimal " )
}
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 ShellTypeName for Primitive {
2020-01-15 17:28:31 +01:00
/// Get the name of the type of a Primitive 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 {
match self {
Primitive ::Nothing = > " nothing " ,
Primitive ::Int ( _ ) = > " integer " ,
2019-12-04 22:14:52 +01:00
Primitive ::Range ( _ ) = > " range " ,
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 ::Decimal ( _ ) = > " decimal " ,
Primitive ::Bytes ( _ ) = > " bytes " ,
Primitive ::String ( _ ) = > " string " ,
2019-12-03 07:44:59 +01:00
Primitive ::Line ( _ ) = > " line " ,
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 ::ColumnPath ( _ ) = > " column path " ,
Primitive ::Pattern ( _ ) = > " pattern " ,
Primitive ::Boolean ( _ ) = > " boolean " ,
Primitive ::Date ( _ ) = > " date " ,
Primitive ::Duration ( _ ) = > " duration " ,
Primitive ::Path ( _ ) = > " file path " ,
Primitive ::Binary ( _ ) = > " binary " ,
Primitive ::BeginningOfStream = > " marker<beginning of stream> " ,
Primitive ::EndOfStream = > " marker<end of stream> " ,
}
}
}
2019-12-09 19:39:51 +01:00
2020-01-15 17:28:31 +01:00
/// Format a Primitive value into a string
2019-12-09 19:39:51 +01:00
pub fn format_primitive ( primitive : & Primitive , field_name : Option < & String > ) -> String {
match primitive {
Primitive ::Nothing = > String ::new ( ) ,
Primitive ::BeginningOfStream = > String ::new ( ) ,
Primitive ::EndOfStream = > String ::new ( ) ,
Primitive ::Path ( p ) = > format! ( " {} " , p . display ( ) ) ,
Primitive ::Bytes ( b ) = > {
let byte = byte_unit ::Byte ::from_bytes ( * b as u128 ) ;
if byte . get_bytes ( ) = = 0 u128 {
return " — " . to_string ( ) ;
}
let byte = byte . get_appropriate_unit ( false ) ;
match byte . get_unit ( ) {
byte_unit ::ByteUnit ::B = > format! ( " {} B " , byte . get_value ( ) ) ,
2019-12-31 08:36:08 +01:00
_ = > byte . format ( 1 ) ,
2019-12-09 19:39:51 +01:00
}
}
Primitive ::Duration ( sec ) = > format_duration ( * sec ) ,
Primitive ::Int ( i ) = > i . to_string ( ) ,
2019-12-24 02:26:47 +01:00
Primitive ::Decimal ( decimal ) = > format! ( " {:.4} " , decimal ) ,
2019-12-04 22:14:52 +01:00
Primitive ::Range ( range ) = > format! (
" {}..{} " ,
format_primitive ( & range . from . 0. item , None ) ,
format_primitive ( & range . to . 0. item , None )
) ,
2019-12-09 19:39:51 +01:00
Primitive ::Pattern ( s ) = > s . to_string ( ) ,
Primitive ::String ( s ) = > s . to_owned ( ) ,
Primitive ::Line ( s ) = > s . to_owned ( ) ,
Primitive ::ColumnPath ( p ) = > {
let mut members = p . iter ( ) ;
let mut f = String ::new ( ) ;
f . push_str (
& members
. next ( )
. expect ( " BUG: column path with zero members " )
. display ( ) ,
) ;
for member in members {
f . push_str ( " . " ) ;
f . push_str ( & member . display ( ) )
}
f
}
Primitive ::Boolean ( b ) = > match ( b , field_name ) {
( true , None ) = > " Yes " ,
( false , None ) = > " No " ,
( true , Some ( s ) ) if ! s . is_empty ( ) = > s ,
( false , Some ( s ) ) if ! s . is_empty ( ) = > " " ,
( true , Some ( _ ) ) = > " Yes " ,
( false , Some ( _ ) ) = > " No " ,
}
. to_owned ( ) ,
Primitive ::Binary ( _ ) = > " <binary> " . to_owned ( ) ,
2020-01-11 21:49:20 +01:00
Primitive ::Date ( d ) = > format_date ( d ) ,
2019-12-09 19:39:51 +01:00
}
}
2019-12-04 22:14:52 +01:00
2020-01-15 17:28:31 +01:00
/// Format a duration in seconds into a string
2019-12-04 22:14:52 +01:00
pub fn format_duration ( sec : u64 ) -> String {
2019-12-09 19:39:51 +01:00
let ( minutes , seconds ) = ( sec / 60 , sec % 60 ) ;
let ( hours , minutes ) = ( minutes / 60 , minutes % 60 ) ;
let ( days , hours ) = ( hours / 24 , hours % 24 ) ;
match ( days , hours , minutes , seconds ) {
( 0 , 0 , 0 , 1 ) = > " 1 sec " . to_owned ( ) ,
( 0 , 0 , 0 , s ) = > format! ( " {} secs " , s ) ,
( 0 , 0 , m , s ) = > format! ( " {} : {:02} " , m , s ) ,
( 0 , h , m , s ) = > format! ( " {} : {:02} : {:02} " , h , m , s ) ,
( d , h , m , s ) = > format! ( " {} : {:02} : {:02} : {:02} " , d , h , m , s ) ,
}
}
2020-01-11 21:49:20 +01:00
2020-01-15 17:28:31 +01:00
/// Format a UTC date value into a humanized string (eg "1 week ago" instead of a formal date string)
2020-01-11 21:49:20 +01:00
pub fn format_date ( d : & DateTime < Utc > ) -> String {
let utc : DateTime < Utc > = Utc ::now ( ) ;
let duration = utc . signed_duration_since ( * d ) ;
if duration . num_weeks ( ) > = 52 {
let num_years = duration . num_weeks ( ) / 52 ;
format! (
" {} year{} ago " ,
num_years ,
if num_years = = 1 { " " } else { " s " }
)
} else if duration . num_weeks ( ) > = 4 {
let num_months = duration . num_weeks ( ) / 4 ;
format! (
" {} month{} ago " ,
num_months ,
if num_months = = 1 { " " } else { " s " }
)
} else if duration . num_weeks ( ) > = 1 {
let num_weeks = duration . num_weeks ( ) ;
format! (
" {} week{} ago " ,
num_weeks ,
if num_weeks = = 1 { " " } else { " s " }
)
} else if duration . num_days ( ) > = 1 {
let num_days = duration . num_days ( ) ;
format! (
" {} day{} ago " ,
num_days ,
if num_days = = 1 { " " } else { " s " }
)
} else if duration . num_hours ( ) > = 1 {
let num_hours = duration . num_hours ( ) ;
format! (
" {} hour{} ago " ,
num_hours ,
if num_hours = = 1 { " " } else { " s " }
)
} else if duration . num_minutes ( ) > = 1 {
let num_minutes = duration . num_minutes ( ) ;
format! (
" {} min{} ago " ,
num_minutes ,
if num_minutes = = 1 { " " } else { " s " }
)
} else {
let num_seconds = duration . num_seconds ( ) ;
format! (
" {} sec{} ago " ,
num_seconds ,
if num_seconds = = 1 { " " } else { " s " }
)
}
}