Finish removing arg deserialization (#3552)

* WIP remove process

* WIP

* WIP

* Finish removing arg deserialization
This commit is contained in:
JT
2021-06-04 18:23:57 +12:00
committed by GitHub
parent fcd94efbd6
commit 131b5b56d7
73 changed files with 483 additions and 1216 deletions

View File

@ -422,6 +422,20 @@ impl Number {
)),
}
}
pub fn to_u64(&self) -> Result<u64, ShellError> {
match self {
Number::BigInt(bi) => match bi.to_u64() {
Some(i) => Ok(i),
None => Err(ShellError::untagged_runtime_error(
"Cannot convert bigint to u64, too large",
)),
},
Number::Int(i) => Ok(*i as u64),
Number::Decimal(_) => Err(ShellError::untagged_runtime_error(
"Cannont convert decimal to u64",
)),
}
}
}
impl PrettyDebug for Number {
@ -573,21 +587,21 @@ impl Unit {
Unit::Tebibyte => filesize(size * 1024 * 1024 * 1024 * 1024),
Unit::Pebibyte => filesize(size * 1024 * 1024 * 1024 * 1024 * 1024),
Unit::Nanosecond => duration(size.to_bigint().expect("Conversion should never fail.")),
Unit::Nanosecond => duration(size.to_i64().expect("Conversion should never fail.")),
Unit::Microsecond => {
duration(size.to_bigint().expect("Conversion should never fail.") * 1000)
duration(size.to_i64().expect("Conversion should never fail.") * 1000)
}
Unit::Millisecond => {
duration(size.to_bigint().expect("Conversion should never fail.") * 1000 * 1000)
duration(size.to_i64().expect("Conversion should never fail.") * 1000 * 1000)
}
Unit::Second => {
duration(size.to_i64().expect("Conversion should never fail.") * 1000 * 1000 * 1000)
}
Unit::Second => duration(
size.to_bigint().expect("Conversion should never fail.") * 1000 * 1000 * 1000,
),
Unit::Minute => duration(
size.to_bigint().expect("Conversion should never fail.") * 60 * 1000 * 1000 * 1000,
size.to_i64().expect("Conversion should never fail.") * 60 * 1000 * 1000 * 1000,
),
Unit::Hour => duration(
size.to_bigint().expect("Conversion should never fail.")
size.to_i64().expect("Conversion should never fail.")
* 60
* 60
* 1000
@ -595,7 +609,7 @@ impl Unit {
* 1000,
),
Unit::Day => duration(
size.to_bigint().expect("Conversion should never fail.")
size.to_i64().expect("Conversion should never fail.")
* 24
* 60
* 60
@ -604,7 +618,7 @@ impl Unit {
* 1000,
),
Unit::Week => duration(
size.to_bigint().expect("Conversion should never fail.")
size.to_i64().expect("Conversion should never fail.")
* 7
* 24
* 60
@ -632,7 +646,7 @@ pub fn filesize(size_in_bytes: Number) -> UntaggedValue {
}
}
pub fn duration(nanos: BigInt) -> UntaggedValue {
pub fn duration(nanos: i64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(nanos))
}

View File

@ -250,8 +250,8 @@ impl UntaggedValue {
}
/// Helper for creating date duration values
pub fn duration(nanos: impl Into<BigInt>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(nanos.into()))
pub fn duration(nanos: i64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(nanos))
}
/// Helper for creating datatime values
@ -369,10 +369,10 @@ impl Value {
}
}
/// View the Value as a Duration (BigInt), if possible
pub fn as_duration(&self) -> Result<BigInt, ShellError> {
/// View the Value as a Duration (i64), if possible
pub fn as_duration(&self) -> Result<i64, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Duration(dur)) => Ok(dur.clone()),
UntaggedValue::Primitive(Primitive::Duration(dur)) => Ok(*dur),
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
}
}
@ -818,51 +818,10 @@ impl StrExt for &str {
}
}
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 {
value: UntaggedValue::Primitive(Primitive::Int(*self as i64)),
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 {
value: UntaggedValue::Primitive(Primitive::Int(*self as i64)),
tag: Tag {
anchor: None,
span: Span::new(0, end),
},
}
}
fn to_untagged_value(&self) -> UntaggedValue {
UntaggedValue::int(*self as i64)
}
fn to_duration_untagged_value(&self) -> UntaggedValue {
UntaggedValue::duration(BigInt::from(*self))
}
}
pub trait I64Ext {
fn to_untagged_value(&self) -> UntaggedValue;
fn to_duration_value(&self, tag: Tag) -> Value;
fn to_duration_untagged_value(&self) -> UntaggedValue;
fn to_value(&self, tag: Tag) -> Value;
fn to_value_create_tag(&self) -> Value;
}
@ -875,6 +834,17 @@ impl I64Ext for i64 {
}
}
fn to_duration_value(&self, the_tag: Tag) -> Value {
Value {
value: UntaggedValue::Primitive(Primitive::Duration(*self)),
tag: the_tag,
}
}
fn to_duration_untagged_value(&self) -> UntaggedValue {
UntaggedValue::duration(*self)
}
fn to_value_create_tag(&self) -> Value {
let end = self.to_string().len();
Value {

View File

@ -45,8 +45,7 @@ pub enum Primitive {
/// A date value
Date(DateTime<FixedOffset>),
/// A count in the number of nanoseconds
#[serde(with = "serde_bigint")]
Duration(BigInt),
Duration(i64),
/// A range of values
Range(Box<Range>),
/// A file path
@ -172,6 +171,7 @@ impl Primitive {
"converting a decimal into a signed 64-bit integer",
)
}),
Primitive::Duration(duration) => Ok(*duration),
other => Err(ShellError::type_error(
"number",
other.type_name().spanned(span),
@ -277,7 +277,7 @@ impl Primitive {
match self {
Primitive::Duration(duration) => {
// Divide into seconds because BigInt can be larger than i64
let (secs, nanos) = duration.div_rem(&BigInt::from(NANOS_PER_SEC));
let (secs, nanos) = duration.div_rem(&(NANOS_PER_SEC as i64));
let secs = match secs.to_i64() {
Some(secs) => secs,
None => {
@ -396,7 +396,7 @@ impl From<chrono::Duration> for Primitive {
.expect("Unexpected overflow")
.num_nanoseconds()
.expect("Unexpected overflow") as u32;
Primitive::Duration(BigInt::from(secs) * NANOS_PER_SEC + nanos)
Primitive::Duration(secs * NANOS_PER_SEC as i64 + nanos as i64)
}
}
@ -513,24 +513,20 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
}
/// Format a duration in nanoseconds into a string
pub fn format_duration(duration: &BigInt) -> String {
pub fn format_duration(duration: &i64) -> String {
let is_zero = duration.is_zero();
// FIXME: This involves a lot of allocation, but it seems inevitable with BigInt.
let big_int_1000 = BigInt::from(1000);
let big_int_60 = BigInt::from(60);
let big_int_24 = BigInt::from(24);
// We only want the biggest subdivision to have the negative sign.
let (sign, duration) = if duration.is_zero() || duration.is_positive() {
(1, duration.clone())
(1, *duration)
} else {
(-1, -duration)
};
let (micros, nanos): (BigInt, BigInt) = duration.div_rem(&big_int_1000);
let (millis, micros): (BigInt, BigInt) = micros.div_rem(&big_int_1000);
let (secs, millis): (BigInt, BigInt) = millis.div_rem(&big_int_1000);
let (mins, secs): (BigInt, BigInt) = secs.div_rem(&big_int_60);
let (hours, mins): (BigInt, BigInt) = mins.div_rem(&big_int_60);
let (days, hours): (BigInt, BigInt) = hours.div_rem(&big_int_24);
let (micros, nanos): (i64, i64) = duration.div_rem(&1000);
let (millis, micros): (i64, i64) = micros.div_rem(&1000);
let (secs, millis): (i64, i64) = millis.div_rem(&1000);
let (mins, secs): (i64, i64) = secs.div_rem(&60);
let (hours, mins): (i64, i64) = mins.div_rem(&60);
let (days, hours): (i64, i64) = hours.div_rem(&24);
let mut output_prep = vec![];