2021-09-08 02:26:57 +00:00
|
|
|
mod range;
|
|
|
|
mod row;
|
|
|
|
mod stream;
|
2021-10-05 02:27:39 +00:00
|
|
|
mod unit;
|
2021-09-08 02:26:57 +00:00
|
|
|
|
2021-10-05 02:27:39 +00:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
|
|
|
use chrono_humanize::HumanTime;
|
2021-09-08 02:26:57 +00:00
|
|
|
pub use range::*;
|
|
|
|
pub use row::*;
|
2021-10-01 05:11:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-09-08 02:26:57 +00:00
|
|
|
pub use stream::*;
|
2021-10-05 02:27:39 +00:00
|
|
|
pub use unit::*;
|
2021-09-08 02:26:57 +00:00
|
|
|
|
2021-09-08 02:32:28 +00:00
|
|
|
use std::fmt::Debug;
|
2021-08-15 22:33:34 +00:00
|
|
|
|
2021-10-02 02:59:11 +00:00
|
|
|
use crate::ast::{CellPath, PathMember};
|
2021-09-02 01:29:43 +00:00
|
|
|
use crate::{span, BlockId, Span, Type};
|
2021-08-15 22:33:34 +00:00
|
|
|
|
|
|
|
use crate::ShellError;
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Core structured values that pass through the pipeline in engine-q
|
2021-10-01 05:11:49 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-08-15 22:33:34 +00:00
|
|
|
pub enum Value {
|
2021-08-28 19:17:30 +00:00
|
|
|
Bool {
|
|
|
|
val: bool,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Int {
|
|
|
|
val: i64,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-10-01 06:53:47 +00:00
|
|
|
Filesize {
|
2021-10-05 02:27:39 +00:00
|
|
|
val: i64,
|
2021-10-01 06:53:47 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
2021-10-01 21:53:13 +00:00
|
|
|
Duration {
|
2021-10-05 02:27:39 +00:00
|
|
|
val: i64,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Date {
|
|
|
|
val: DateTime<FixedOffset>,
|
2021-10-01 21:53:13 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-04 21:52:57 +00:00
|
|
|
Range {
|
|
|
|
val: Box<Range>,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-08-28 19:17:30 +00:00
|
|
|
Float {
|
|
|
|
val: f64,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
String {
|
|
|
|
val: String,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-07 07:07:11 +00:00
|
|
|
Record {
|
|
|
|
cols: Vec<String>,
|
|
|
|
vals: Vec<Value>,
|
2021-09-04 06:52:28 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-07 07:35:59 +00:00
|
|
|
Stream {
|
2021-09-07 07:07:11 +00:00
|
|
|
stream: ValueStream,
|
2021-09-04 06:52:28 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
2021-08-28 19:17:30 +00:00
|
|
|
List {
|
2021-09-07 07:07:11 +00:00
|
|
|
vals: Vec<Value>,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Block {
|
|
|
|
val: BlockId,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Nothing {
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-05 23:16:27 +00:00
|
|
|
Error {
|
2021-09-06 04:07:48 +00:00
|
|
|
error: ShellError,
|
2021-09-05 23:16:27 +00:00
|
|
|
},
|
2021-09-23 16:42:03 +00:00
|
|
|
Binary {
|
|
|
|
val: Vec<u8>,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-10-02 02:59:11 +00:00
|
|
|
CellPath {
|
|
|
|
val: CellPath,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn as_string(&self) -> Result<String, ShellError> {
|
|
|
|
match self {
|
|
|
|
Value::String { val, .. } => Ok(val.to_string()),
|
|
|
|
_ => Err(ShellError::CantConvert("string".into(), self.span())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Get the span for the current value
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
2021-09-08 02:26:57 +00:00
|
|
|
Value::Error { .. } => Span::unknown(),
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Bool { span, .. } => *span,
|
|
|
|
Value::Int { span, .. } => *span,
|
|
|
|
Value::Float { span, .. } => *span,
|
2021-10-01 06:53:47 +00:00
|
|
|
Value::Filesize { span, .. } => *span,
|
2021-10-01 21:53:13 +00:00
|
|
|
Value::Duration { span, .. } => *span,
|
2021-10-05 02:27:39 +00:00
|
|
|
Value::Date { span, .. } => *span,
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::String { span, .. } => *span,
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { span, .. } => *span,
|
|
|
|
Value::Block { span, .. } => *span,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Nothing { span, .. } => *span,
|
2021-09-23 16:42:03 +00:00
|
|
|
Value::Binary { span, .. } => *span,
|
2021-10-02 02:59:11 +00:00
|
|
|
Value::CellPath { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Update the value with a new span
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn with_span(mut self, new_span: Span) -> Value {
|
|
|
|
match &mut self {
|
|
|
|
Value::Bool { span, .. } => *span = new_span,
|
|
|
|
Value::Int { span, .. } => *span = new_span,
|
|
|
|
Value::Float { span, .. } => *span = new_span,
|
2021-10-01 06:53:47 +00:00
|
|
|
Value::Filesize { span, .. } => *span = new_span,
|
2021-10-01 21:53:13 +00:00
|
|
|
Value::Duration { span, .. } => *span = new_span,
|
2021-10-05 02:27:39 +00:00
|
|
|
Value::Date { span, .. } => *span = new_span,
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::String { span, .. } => *span = new_span,
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { span, .. } => *span = new_span,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { span, .. } => *span = new_span,
|
|
|
|
Value::Block { span, .. } => *span = new_span,
|
|
|
|
Value::Nothing { span, .. } => *span = new_span,
|
2021-09-05 23:16:27 +00:00
|
|
|
Value::Error { .. } => {}
|
2021-09-23 16:42:03 +00:00
|
|
|
Value::Binary { span, .. } => *span = new_span,
|
2021-10-02 02:59:11 +00:00
|
|
|
Value::CellPath { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Get the type of the current Value
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn get_type(&self) -> Type {
|
|
|
|
match self {
|
|
|
|
Value::Bool { .. } => Type::Bool,
|
|
|
|
Value::Int { .. } => Type::Int,
|
|
|
|
Value::Float { .. } => Type::Float,
|
2021-10-01 06:53:47 +00:00
|
|
|
Value::Filesize { .. } => Type::Filesize,
|
2021-10-01 21:53:13 +00:00
|
|
|
Value::Duration { .. } => Type::Duration,
|
2021-10-05 02:27:39 +00:00
|
|
|
Value::Date { .. } => Type::Date,
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { .. } => Type::Range,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::String { .. } => Type::String,
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { cols, vals, .. } => {
|
|
|
|
Type::Record(cols.clone(), vals.iter().map(|x| x.get_type()).collect())
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
|
|
|
|
Value::Nothing { .. } => Type::Nothing,
|
|
|
|
Value::Block { .. } => Type::Block,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { .. } => Type::ValueStream,
|
2021-09-05 23:16:27 +00:00
|
|
|
Value::Error { .. } => Type::Error,
|
2021-09-23 16:42:03 +00:00
|
|
|
Value::Binary { .. } => Type::Binary,
|
2021-10-02 02:59:11 +00:00
|
|
|
Value::CellPath { .. } => Type::CellPath,
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 21:20:53 +00:00
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Convert Value into string. Note that Streams will be consumed.
|
2021-09-01 21:20:53 +00:00
|
|
|
pub fn into_string(self) -> String {
|
|
|
|
match self {
|
|
|
|
Value::Bool { val, .. } => val.to_string(),
|
|
|
|
Value::Int { val, .. } => val.to_string(),
|
|
|
|
Value::Float { val, .. } => val.to_string(),
|
2021-10-05 02:27:39 +00:00
|
|
|
Value::Filesize { val, .. } => format_filesize(val),
|
|
|
|
Value::Duration { val, .. } => format_duration(val),
|
|
|
|
Value::Date { val, .. } => HumanTime::from(val).to_string(),
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { val, .. } => {
|
|
|
|
format!(
|
|
|
|
"range: [{}]",
|
2021-09-11 11:13:04 +00:00
|
|
|
val.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
2021-09-04 21:52:57 +00:00
|
|
|
.collect::<Vec<String>>()
|
2021-09-04 22:40:15 +00:00
|
|
|
.join(", ")
|
2021-09-04 21:52:57 +00:00
|
|
|
)
|
2021-09-04 22:35:08 +00:00
|
|
|
}
|
2021-09-01 21:20:53 +00:00
|
|
|
Value::String { val, .. } => val,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { stream, .. } => stream.into_string(),
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals: val, .. } => format!(
|
2021-09-06 22:02:24 +00:00
|
|
|
"[{}]",
|
|
|
|
val.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { cols, vals, .. } => format!(
|
|
|
|
"{{{}}}",
|
|
|
|
cols.iter()
|
|
|
|
.zip(vals.iter())
|
|
|
|
.map(|(x, y)| format!("{}: {}", x, y.clone().into_string()))
|
2021-09-06 22:02:24 +00:00
|
|
|
.collect::<Vec<_>>()
|
2021-09-07 07:07:11 +00:00
|
|
|
.join(", ")
|
2021-09-06 22:02:24 +00:00
|
|
|
),
|
2021-09-01 21:20:53 +00:00
|
|
|
Value::Block { val, .. } => format!("<Block {}>", val),
|
2021-09-02 01:29:43 +00:00
|
|
|
Value::Nothing { .. } => String::new(),
|
2021-09-06 04:07:48 +00:00
|
|
|
Value::Error { error } => format!("{:?}", error),
|
2021-09-23 16:42:03 +00:00
|
|
|
Value::Binary { val, .. } => format!("{:?}", val),
|
2021-10-02 04:55:05 +00:00
|
|
|
Value::CellPath { val, .. } => val.into_string(),
|
2021-10-01 06:01:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collect_string(self) -> String {
|
|
|
|
match self {
|
|
|
|
Value::Bool { val, .. } => val.to_string(),
|
|
|
|
Value::Int { val, .. } => val.to_string(),
|
|
|
|
Value::Float { val, .. } => val.to_string(),
|
2021-10-01 06:53:47 +00:00
|
|
|
Value::Filesize { val, .. } => format!("{} bytes", val),
|
2021-10-01 21:53:13 +00:00
|
|
|
Value::Duration { val, .. } => format!("{} ns", val),
|
2021-10-05 02:27:39 +00:00
|
|
|
Value::Date { val, .. } => format!("{:?}", val),
|
2021-10-01 06:01:22 +00:00
|
|
|
Value::Range { val, .. } => val
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", "),
|
|
|
|
Value::String { val, .. } => val,
|
|
|
|
Value::Stream { stream, .. } => stream.collect_string(),
|
|
|
|
Value::List { vals: val, .. } => val
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x.collect_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n"),
|
|
|
|
Value::Record { vals, .. } => vals
|
|
|
|
.into_iter()
|
|
|
|
.map(|y| y.collect_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n"),
|
|
|
|
Value::Block { val, .. } => format!("<Block {}>", val),
|
|
|
|
Value::Nothing { .. } => String::new(),
|
|
|
|
Value::Error { error } => format!("{:?}", error),
|
|
|
|
Value::Binary { val, .. } => format!("{:?}", val),
|
2021-10-02 04:55:05 +00:00
|
|
|
Value::CellPath { .. } => self.into_string(),
|
2021-09-01 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 02:15:01 +00:00
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Create a new `Nothing` value
|
2021-09-03 02:15:01 +00:00
|
|
|
pub fn nothing() -> Value {
|
|
|
|
Value::Nothing {
|
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 22:02:24 +00:00
|
|
|
|
2021-09-27 12:10:18 +00:00
|
|
|
/// Follow a given column path into the value: for example accessing nth elements in a stream or list
|
2021-09-26 18:39:19 +00:00
|
|
|
pub fn follow_cell_path(self, cell_path: &[PathMember]) -> Result<Value, ShellError> {
|
2021-09-06 22:02:24 +00:00
|
|
|
let mut current = self;
|
2021-09-26 18:39:19 +00:00
|
|
|
for member in cell_path {
|
2021-09-06 22:02:24 +00:00
|
|
|
// FIXME: this uses a few extra clones for simplicity, but there may be a way
|
|
|
|
// to traverse the path without them
|
|
|
|
match member {
|
|
|
|
PathMember::Int {
|
|
|
|
val: count,
|
|
|
|
span: origin_span,
|
|
|
|
} => {
|
|
|
|
// Treat a numeric path member as `nth <val>`
|
|
|
|
match &mut current {
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals: val, .. } => {
|
2021-09-06 22:02:24 +00:00
|
|
|
if let Some(item) = val.get(*count) {
|
|
|
|
current = item.clone();
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::AccessBeyondEnd(val.len(), *origin_span));
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { stream, .. } => {
|
2021-09-06 22:02:24 +00:00
|
|
|
if let Some(item) = stream.nth(*count) {
|
|
|
|
current = item;
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::AccessBeyondEndOfStream(*origin_span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => {
|
|
|
|
return Err(ShellError::IncompatiblePathAccess(
|
|
|
|
format!("{}", x.get_type()),
|
|
|
|
*origin_span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PathMember::String {
|
2021-09-07 07:07:11 +00:00
|
|
|
val: column_name,
|
2021-09-06 22:02:24 +00:00
|
|
|
span: origin_span,
|
|
|
|
} => match &mut current {
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { cols, vals, .. } => {
|
2021-09-06 22:02:24 +00:00
|
|
|
let mut found = false;
|
2021-09-07 07:07:11 +00:00
|
|
|
for col in cols.iter().zip(vals.iter()) {
|
|
|
|
if col.0 == column_name {
|
|
|
|
current = col.1.clone();
|
2021-09-06 22:02:24 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return Err(ShellError::CantFindColumn(*origin_span));
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals, span } => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for val in vals {
|
|
|
|
if let Value::Record { cols, vals, .. } = val {
|
|
|
|
for col in cols.iter().enumerate() {
|
|
|
|
if col.1 == column_name {
|
|
|
|
output.push(vals[col.0].clone());
|
|
|
|
}
|
2021-09-07 07:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current = Value::List {
|
|
|
|
vals: output,
|
|
|
|
span: *span,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Value::Stream { stream, span } => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for val in stream {
|
|
|
|
if let Value::Record { cols, vals, .. } = val {
|
|
|
|
for col in cols.iter().enumerate() {
|
|
|
|
if col.1 == column_name {
|
|
|
|
output.push(vals[col.0].clone());
|
|
|
|
}
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 07:07:11 +00:00
|
|
|
current = Value::List {
|
|
|
|
vals: output,
|
|
|
|
span: *span,
|
|
|
|
};
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
|
|
|
x => {
|
|
|
|
return Err(ShellError::IncompatiblePathAccess(
|
|
|
|
format!("{}", x.get_type()),
|
|
|
|
*origin_span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(current)
|
|
|
|
}
|
2021-09-09 21:47:20 +00:00
|
|
|
|
2021-09-10 01:06:44 +00:00
|
|
|
pub fn string(s: &str, span: Span) -> Value {
|
|
|
|
Value::String {
|
|
|
|
val: s.into(),
|
|
|
|
span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:47:20 +00:00
|
|
|
pub fn is_true(&self) -> bool {
|
|
|
|
matches!(self, Value::Bool { val: true, .. })
|
|
|
|
}
|
2021-09-10 02:27:12 +00:00
|
|
|
|
|
|
|
pub fn columns(&self) -> Vec<String> {
|
|
|
|
match self {
|
|
|
|
Value::Record { cols, .. } => cols.clone(),
|
|
|
|
_ => vec![],
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Block { val: b1, .. }, Value::Block { val: b2, .. }) => b1 == b2,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn add(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-15 22:33:34 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs + rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 + *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs + *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs + rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::String {
|
|
|
|
val: lhs.to_string() + rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Duration {
|
|
|
|
val: *lhs + *rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Filesize {
|
|
|
|
val: *lhs + *rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
|
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
pub fn sub(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs - rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 - *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs - *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs - rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Duration {
|
|
|
|
val: *lhs - *rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Filesize {
|
|
|
|
val: *lhs - *rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn mul(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs * rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 * *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs * *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs * rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
|
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn div(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0 {
|
2021-09-06 05:35:58 +00:00
|
|
|
if lhs % rhs == 0 {
|
|
|
|
Ok(Value::Int {
|
|
|
|
val: lhs / rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: (*lhs as f64) / (*rhs as f64),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0.0 {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: *lhs as f64 / *rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0 {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: *lhs / *rhs as f64,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0.0 {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: lhs / rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn lt(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs < rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) < *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs < *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs < rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs < rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs < rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn lte(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs <= rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) <= *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs <= *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs <= rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs <= rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs <= rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn gt(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs > rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) > *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs > *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs > rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs > rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs > rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn gte(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs >= rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) >= *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs >= *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs >= rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs >= rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs >= rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn eq(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) == *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs == *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
(Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => Ok(Value::Bool {
|
2021-09-04 06:52:28 +00:00
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: lhs,
|
|
|
|
cols: lhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: rhs,
|
|
|
|
cols: rhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
) => Ok(Value::Bool {
|
|
|
|
val: lhs_headers == rhs_headers && lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn ne(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) != *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs != *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-05 02:27:39 +00:00
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
(Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => Ok(Value::Bool {
|
2021-09-04 06:52:28 +00:00
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: lhs,
|
|
|
|
cols: lhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: rhs,
|
|
|
|
cols: rhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
) => Ok(Value::Bool {
|
|
|
|
val: lhs_headers != rhs_headers || lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
2021-10-05 02:27:39 +00:00
|
|
|
|
|
|
|
/// Format a duration in nanoseconds into a string
|
|
|
|
pub fn format_duration(duration: i64) -> String {
|
|
|
|
let (sign, duration) = if duration >= 0 {
|
|
|
|
(1, duration)
|
|
|
|
} else {
|
|
|
|
(-1, -duration)
|
|
|
|
};
|
|
|
|
let (micros, nanos): (i64, i64) = (duration / 1000, duration % 1000);
|
|
|
|
let (millis, micros): (i64, i64) = (micros / 1000, micros % 1000);
|
|
|
|
let (secs, millis): (i64, i64) = (millis / 1000, millis % 1000);
|
|
|
|
let (mins, secs): (i64, i64) = (secs / 60, secs % 60);
|
|
|
|
let (hours, mins): (i64, i64) = (mins / 60, mins % 60);
|
|
|
|
let (days, hours): (i64, i64) = (hours / 24, hours % 24);
|
|
|
|
|
|
|
|
let mut output_prep = vec![];
|
|
|
|
|
|
|
|
if days != 0 {
|
|
|
|
output_prep.push(format!("{}day", days));
|
|
|
|
}
|
|
|
|
|
|
|
|
if hours != 0 {
|
|
|
|
output_prep.push(format!("{}hr", hours));
|
|
|
|
}
|
|
|
|
|
|
|
|
if mins != 0 {
|
|
|
|
output_prep.push(format!("{}min", mins));
|
|
|
|
}
|
|
|
|
// output 0sec for zero duration
|
|
|
|
if duration == 0 || secs != 0 {
|
|
|
|
output_prep.push(format!("{}sec", secs));
|
|
|
|
}
|
|
|
|
|
|
|
|
if millis != 0 {
|
|
|
|
output_prep.push(format!("{}ms", millis));
|
|
|
|
}
|
|
|
|
|
|
|
|
if micros != 0 {
|
|
|
|
output_prep.push(format!("{}us", micros));
|
|
|
|
}
|
|
|
|
|
|
|
|
if nanos != 0 {
|
|
|
|
output_prep.push(format!("{}ns", nanos));
|
|
|
|
}
|
|
|
|
|
|
|
|
format!(
|
|
|
|
"{}{}",
|
|
|
|
if sign == -1 { "-" } else { "" },
|
|
|
|
output_prep.join(" ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_filesize(num_bytes: i64) -> String {
|
|
|
|
let byte = byte_unit::Byte::from_bytes(num_bytes as u128);
|
|
|
|
|
|
|
|
if byte.get_bytes() == 0u128 {
|
|
|
|
return "—".to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
let byte = byte.get_appropriate_unit(false);
|
|
|
|
|
|
|
|
match byte.get_unit() {
|
|
|
|
byte_unit::ByteUnit::B => format!("{} B ", byte.get_value()),
|
|
|
|
_ => byte.format(1),
|
|
|
|
}
|
|
|
|
}
|