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-10-12 21:54:28 +00:00
|
|
|
use std::collections::HashMap;
|
2021-10-09 15:32:56 +00:00
|
|
|
use std::{cmp::Ordering, fmt::Debug};
|
2021-08-15 22:33:34 +00:00
|
|
|
|
2021-10-09 10:29:59 +00:00
|
|
|
use crate::ast::{CellPath, PathMember};
|
2021-10-12 21:54:28 +00:00
|
|
|
use crate::{span, BlockId, Span, Spanned, 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-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()),
|
2021-10-11 18:45:31 +00:00
|
|
|
_ => Err(ShellError::CantConvert("string".into(), self.span()?)),
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Get the span for the current value
|
2021-10-11 18:45:31 +00:00
|
|
|
pub fn span(&self) -> Result<Span, ShellError> {
|
2021-08-15 22:33:34 +00:00
|
|
|
match self {
|
2021-10-11 18:45:31 +00:00
|
|
|
Value::Error { error } => Err(error.clone()),
|
|
|
|
Value::Bool { span, .. } => Ok(*span),
|
|
|
|
Value::Int { span, .. } => Ok(*span),
|
|
|
|
Value::Float { span, .. } => Ok(*span),
|
|
|
|
Value::Filesize { span, .. } => Ok(*span),
|
|
|
|
Value::Duration { span, .. } => Ok(*span),
|
|
|
|
Value::Date { span, .. } => Ok(*span),
|
|
|
|
Value::Range { span, .. } => Ok(*span),
|
|
|
|
Value::String { span, .. } => Ok(*span),
|
|
|
|
Value::Record { span, .. } => Ok(*span),
|
|
|
|
Value::List { span, .. } => Ok(*span),
|
|
|
|
Value::Block { span, .. } => Ok(*span),
|
|
|
|
Value::Nothing { span, .. } => Ok(*span),
|
|
|
|
Value::Binary { span, .. } => Ok(*span),
|
|
|
|
Value::CellPath { span, .. } => Ok(*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-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-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-10-25 22:24:10 +00:00
|
|
|
Value::Range { val, .. } => {
|
|
|
|
format!("{}..{}", val.from.into_string(), val.to.into_string())
|
|
|
|
}
|
2021-09-01 21:20:53 +00:00
|
|
|
Value::String { val, .. } => val,
|
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-25 22:24:10 +00:00
|
|
|
Value::Range { val, .. } => {
|
|
|
|
format!("{}..{}", val.from.into_string(), val.to.into_string())
|
|
|
|
}
|
2021-10-01 06:01:22 +00:00
|
|
|
Value::String { val, .. } => val,
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-10-11 19:51:54 +00:00
|
|
|
Value::Record { cols, vals, span } => {
|
|
|
|
let span = *span;
|
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 {
|
2021-10-11 19:51:54 +00:00
|
|
|
return Err(ShellError::CantFindColumn(*origin_span, span));
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals, span } => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for val in vals {
|
2021-10-11 19:51:54 +00:00
|
|
|
output.push(val.clone().follow_cell_path(&[PathMember::String {
|
|
|
|
val: column_name.clone(),
|
|
|
|
span: *origin_span,
|
|
|
|
}])?);
|
|
|
|
// 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,
|
|
|
|
};
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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-10-09 06:20:32 +00:00
|
|
|
|
2021-10-20 05:58:25 +00:00
|
|
|
pub fn string(val: impl Into<String>, span: Span) -> Value {
|
|
|
|
Value::String {
|
|
|
|
val: val.into(),
|
|
|
|
span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn int(val: i64, span: Span) -> Value {
|
|
|
|
Value::Int { val, span }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only use these for test data. Span::unknown() should not be used in user data
|
|
|
|
pub fn test_string(s: impl Into<String>) -> Value {
|
|
|
|
Value::String {
|
|
|
|
val: s.into(),
|
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only use these for test data. Span::unknown() should not be used in user data
|
|
|
|
pub fn test_int(val: i64) -> Value {
|
|
|
|
Value::Int {
|
|
|
|
val,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 04:01:02 +00:00
|
|
|
impl Default for Value {
|
|
|
|
fn default() -> Self {
|
|
|
|
Value::nothing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
impl PartialOrd for Value {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
// Compare two floating point numbers. The decision interval for equality is dynamically
|
|
|
|
// scaled as the value being compared increases in magnitude.
|
|
|
|
fn compare_floats(val: f64, other: f64) -> Option<Ordering> {
|
|
|
|
let prec = f64::EPSILON.max(val.abs() * f64::EPSILON);
|
|
|
|
|
|
|
|
if (other - val).abs() < prec {
|
|
|
|
return Some(Ordering::Equal);
|
|
|
|
}
|
|
|
|
|
|
|
|
val.partial_cmp(&other)
|
|
|
|
}
|
2021-10-09 13:10:10 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match (self, other) {
|
|
|
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs.partial_cmp(rhs),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs.partial_cmp(rhs),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
|
|
|
compare_floats(*lhs, *rhs)
|
|
|
|
}
|
2021-10-31 06:54:51 +00:00
|
|
|
(Value::Date { val: lhs, .. }, Value::Date { val: rhs, .. }) => {
|
|
|
|
lhs.date().to_string().partial_cmp(&rhs.date().to_string())
|
|
|
|
}
|
2021-10-09 15:32:56 +00:00
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
|
|
|
|
lhs.partial_cmp(rhs)
|
|
|
|
}
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
|
|
|
compare_floats(*lhs as f64, *rhs)
|
|
|
|
}
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
compare_floats(*lhs, *rhs as f64)
|
|
|
|
}
|
|
|
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
|
|
|
lhs.partial_cmp(rhs)
|
|
|
|
}
|
|
|
|
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
|
|
|
lhs.partial_cmp(rhs)
|
|
|
|
}
|
|
|
|
(Value::Block { val: b1, .. }, Value::Block { val: b2, .. }) if b1 == b2 => {
|
|
|
|
Some(Ordering::Equal)
|
|
|
|
}
|
2021-10-09 17:11:15 +00:00
|
|
|
(Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => lhs.partial_cmp(rhs),
|
2021-10-09 13:10:10 +00:00
|
|
|
(
|
|
|
|
Value::Record {
|
2021-10-09 15:32:56 +00:00
|
|
|
vals: lhs,
|
|
|
|
cols: lhs_headers,
|
2021-10-09 13:10:10 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
Value::Record {
|
2021-10-09 15:32:56 +00:00
|
|
|
vals: rhs,
|
|
|
|
cols: rhs_headers,
|
2021-10-09 13:10:10 +00:00
|
|
|
..
|
|
|
|
},
|
2021-10-09 15:32:56 +00:00
|
|
|
) if lhs_headers == rhs_headers && lhs == rhs => Some(Ordering::Equal),
|
2021-10-11 01:56:19 +00:00
|
|
|
(Value::Binary { val: lhs, .. }, Value::Binary { val: rhs, .. }) => {
|
|
|
|
lhs.partial_cmp(rhs)
|
|
|
|
}
|
2021-10-09 15:32:56 +00:00
|
|
|
(_, _) => None,
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.partial_cmp(other).map_or(false, Ordering::is_eq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
impl Value {
|
|
|
|
pub fn add(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-15 22:33:34 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
2021-10-20 05:58:25 +00:00
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
if let Some(val) = lhs.checked_add(*rhs) {
|
|
|
|
Ok(Value::Int { val, span })
|
|
|
|
} else {
|
|
|
|
Err(ShellError::OperatorOverflow(
|
|
|
|
"add operation overflowed".into(),
|
|
|
|
span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
(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(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-15 22:33:34 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-15 22:33:34 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
pub fn sub(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +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(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn mul(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +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(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn div(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +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(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn lt(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match self.partial_cmp(rhs) {
|
|
|
|
Some(ordering) => Ok(Value::Bool {
|
|
|
|
val: matches!(ordering, Ordering::Less),
|
2021-08-25 19:29:36 +00:00
|
|
|
span,
|
|
|
|
}),
|
2021-10-09 15:32:56 +00:00
|
|
|
None => Err(ShellError::OperatorMismatch {
|
2021-08-25 19:29:36 +00:00
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn lte(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match self.partial_cmp(rhs) {
|
|
|
|
Some(ordering) => Ok(Value::Bool {
|
|
|
|
val: matches!(ordering, Ordering::Less | Ordering::Equal),
|
2021-08-25 19:29:36 +00:00
|
|
|
span,
|
|
|
|
}),
|
2021-10-09 15:32:56 +00:00
|
|
|
None => Err(ShellError::OperatorMismatch {
|
2021-08-25 19:29:36 +00:00
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn gt(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match self.partial_cmp(rhs) {
|
|
|
|
Some(ordering) => Ok(Value::Bool {
|
|
|
|
val: matches!(ordering, Ordering::Greater),
|
2021-08-25 19:29:36 +00:00
|
|
|
span,
|
|
|
|
}),
|
2021-10-09 15:32:56 +00:00
|
|
|
None => Err(ShellError::OperatorMismatch {
|
2021-08-25 19:29:36 +00:00
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn gte(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match self.partial_cmp(rhs) {
|
|
|
|
Some(ordering) => Ok(Value::Bool {
|
|
|
|
val: matches!(ordering, Ordering::Greater | Ordering::Equal),
|
2021-08-25 19:29:36 +00:00
|
|
|
span,
|
|
|
|
}),
|
2021-10-09 15:32:56 +00:00
|
|
|
None => Err(ShellError::OperatorMismatch {
|
2021-08-25 19:29:36 +00:00
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn eq(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match self.partial_cmp(rhs) {
|
|
|
|
Some(ordering) => Ok(Value::Bool {
|
|
|
|
val: matches!(ordering, Ordering::Equal),
|
2021-09-04 06:52:28 +00:00
|
|
|
span,
|
|
|
|
}),
|
2021-10-09 15:32:56 +00:00
|
|
|
None => Err(ShellError::OperatorMismatch {
|
2021-08-25 19:29:36 +00:00
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn ne(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
2021-10-09 15:32:56 +00:00
|
|
|
match self.partial_cmp(rhs) {
|
|
|
|
Some(ordering) => Ok(Value::Bool {
|
2021-10-09 15:58:58 +00:00
|
|
|
val: !matches!(ordering, Ordering::Equal),
|
2021-08-25 19:29:36 +00:00
|
|
|
span,
|
|
|
|
}),
|
2021-10-09 15:32:56 +00:00
|
|
|
None => Err(ShellError::OperatorMismatch {
|
2021-08-25 19:29:36 +00:00
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-08-25 19:29:36 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 22:53:28 +00:00
|
|
|
|
|
|
|
pub fn r#in(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-10-08 22:53:28 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(lhs, Value::Range { val: rhs, .. }) => Ok(Value::Bool {
|
2021-10-09 15:32:56 +00:00
|
|
|
val: rhs.contains(lhs),
|
2021-10-08 22:53:28 +00:00
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: rhs.contains(lhs),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(lhs, Value::List { vals: rhs, .. }) => Ok(Value::Bool {
|
2021-10-09 15:32:56 +00:00
|
|
|
val: rhs.contains(lhs),
|
2021-10-09 09:23:32 +00:00
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::Record { cols: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: rhs.contains(lhs),
|
|
|
|
span,
|
|
|
|
}),
|
2021-10-08 22:53:28 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-10-08 22:53:28 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-10-08 22:53:28 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 15:58:58 +00:00
|
|
|
|
|
|
|
pub fn not_in(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
2021-10-09 15:58:58 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(lhs, Value::Range { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: !rhs.contains(lhs),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: !rhs.contains(lhs),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(lhs, Value::List { vals: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: !rhs.contains(lhs),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::Record { cols: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: !rhs.contains(lhs),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
lhs_span: self.span()?,
|
2021-10-09 15:58:58 +00:00
|
|
|
rhs_ty: rhs.get_type(),
|
2021-10-11 18:45:31 +00:00
|
|
|
rhs_span: rhs.span()?,
|
2021-10-09 15:58:58 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 20:35:12 +00:00
|
|
|
|
|
|
|
pub fn contains(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs.contains(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 not_contains(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: !lhs.contains(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 modulo(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0 {
|
|
|
|
Ok(Value::Int {
|
|
|
|
val: lhs % rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} 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 and(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
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 or(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
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 pow(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
let span = span(&[self.span()?, rhs.span()?]);
|
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs.pow(*rhs as u32),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: (*lhs as f64).powf(*rhs),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs.powf(*rhs as f64),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs.powf(*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()?,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
2021-10-05 02:27:39 +00:00
|
|
|
|
2021-10-12 21:54:28 +00:00
|
|
|
/// Create a Value::Record from a spanned hashmap
|
|
|
|
impl From<Spanned<HashMap<String, Value>>> for Value {
|
|
|
|
fn from(input: Spanned<HashMap<String, Value>>) -> Self {
|
|
|
|
let span = input.span;
|
|
|
|
let (cols, vals) = input
|
|
|
|
.item
|
|
|
|
.into_iter()
|
|
|
|
.fold((vec![], vec![]), |mut acc, (k, v)| {
|
|
|
|
acc.0.push(k);
|
|
|
|
acc.1.push(v);
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
|
|
|
|
Value::Record { cols, vals, span }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|