2019-05-11 22:59:57 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-05-10 16:59:12 +00:00
|
|
|
use crate::object::desc::DataDescriptor;
|
2019-05-15 22:23:36 +00:00
|
|
|
use ansi_term::Color;
|
2019-05-15 18:14:51 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use chrono_humanize::Humanize;
|
|
|
|
use std::time::SystemTime;
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-05-15 18:14:51 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-05-10 16:59:12 +00:00
|
|
|
pub enum Primitive {
|
|
|
|
Nothing,
|
|
|
|
Int(i64),
|
2019-05-15 22:58:44 +00:00
|
|
|
#[allow(unused)]
|
2019-05-10 16:59:12 +00:00
|
|
|
Float(f64),
|
2019-05-15 18:14:51 +00:00
|
|
|
Bytes(u128),
|
2019-05-10 16:59:12 +00:00
|
|
|
String(String),
|
|
|
|
Boolean(bool),
|
2019-05-15 18:14:51 +00:00
|
|
|
Date(DateTime<Utc>),
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Primitive {
|
2019-05-15 18:14:51 +00:00
|
|
|
crate fn format(&self, field_name: Option<&str>) -> String {
|
2019-05-10 16:59:12 +00:00
|
|
|
match self {
|
2019-05-15 22:23:36 +00:00
|
|
|
Primitive::Nothing => format!("{}", Color::Black.bold().paint("-")),
|
2019-05-15 18:14:51 +00:00
|
|
|
Primitive::Bytes(b) => {
|
|
|
|
let byte = byte_unit::Byte::from_bytes(*b);
|
2019-05-15 22:23:36 +00:00
|
|
|
|
|
|
|
if byte.get_bytes() == 0u128 {
|
|
|
|
return Color::Black.bold().paint("Empty".to_string()).to_string();
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:14:51 +00:00
|
|
|
let byte = byte.get_appropriate_unit(true);
|
|
|
|
|
|
|
|
match byte.get_unit() {
|
|
|
|
byte_unit::ByteUnit::B => format!("{}", byte.format(0)),
|
|
|
|
_ => format!("{}", byte.format(1)),
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 16:59:12 +00:00
|
|
|
Primitive::Int(i) => format!("{}", i),
|
|
|
|
Primitive::Float(f) => format!("{}", f),
|
2019-05-15 18:14:51 +00:00
|
|
|
Primitive::String(s) => format!("{}", s),
|
|
|
|
Primitive::Boolean(b) => match (b, field_name) {
|
|
|
|
(true, None) => format!("Yes"),
|
|
|
|
(false, None) => format!("No"),
|
|
|
|
(true, Some(s)) => format!("{}", s),
|
2019-05-15 22:58:44 +00:00
|
|
|
(false, Some(_)) => format!(""),
|
2019-05-15 18:14:51 +00:00
|
|
|
},
|
2019-05-15 22:23:36 +00:00
|
|
|
Primitive::Date(d) => format!("{}", d.humanize()),
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Value {
|
|
|
|
Primitive(Primitive),
|
2019-05-15 21:44:06 +00:00
|
|
|
Object(crate::object::Dictionary),
|
2019-05-10 16:59:12 +00:00
|
|
|
List(Vec<Value>),
|
2019-05-15 18:14:51 +00:00
|
|
|
Error(Box<ShellError>),
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
impl Value {
|
|
|
|
crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
|
2019-05-10 16:59:12 +00:00
|
|
|
match self {
|
2019-05-15 22:58:44 +00:00
|
|
|
Value::Primitive(_) => vec![],
|
2019-05-10 16:59:12 +00:00
|
|
|
Value::Object(o) => o.data_descriptors(),
|
2019-05-15 22:58:44 +00:00
|
|
|
Value::List(_) => vec![],
|
|
|
|
Value::Error(_) => vec![],
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value> {
|
2019-05-10 16:59:12 +00:00
|
|
|
match self {
|
2019-05-15 22:58:44 +00:00
|
|
|
Value::Primitive(_) => crate::MaybeOwned::Owned(Value::nothing()),
|
2019-05-10 16:59:12 +00:00
|
|
|
Value::Object(o) => o.get_data(desc),
|
2019-05-15 22:58:44 +00:00
|
|
|
Value::List(_) => crate::MaybeOwned::Owned(Value::nothing()),
|
|
|
|
Value::Error(_) => crate::MaybeOwned::Owned(Value::nothing()),
|
2019-05-15 18:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn copy(&self) -> Value {
|
2019-05-15 18:14:51 +00:00
|
|
|
match self {
|
|
|
|
Value::Primitive(p) => Value::Primitive(p.clone()),
|
2019-05-15 21:44:06 +00:00
|
|
|
Value::Object(o) => Value::Object(o.copy_dict()),
|
2019-05-15 18:14:51 +00:00
|
|
|
Value::List(l) => {
|
|
|
|
let list = l.iter().map(|i| i.copy()).collect();
|
|
|
|
Value::List(list)
|
|
|
|
}
|
|
|
|
Value::Error(e) => Value::Error(Box::new(e.copy_error())),
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 18:14:51 +00:00
|
|
|
|
|
|
|
crate fn format_leaf(&self, field_name: Option<&str>) -> String {
|
2019-05-10 16:59:12 +00:00
|
|
|
match self {
|
2019-05-15 18:14:51 +00:00
|
|
|
Value::Primitive(p) => p.format(field_name),
|
2019-05-15 22:58:44 +00:00
|
|
|
Value::Object(_) => format!("[object Object]"),
|
|
|
|
Value::List(_) => format!("[list List]"),
|
2019-05-15 18:14:51 +00:00
|
|
|
Value::Error(e) => format!("{}", e),
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 22:59:57 +00:00
|
|
|
crate fn as_string(&self) -> Result<String, ShellError> {
|
|
|
|
match self {
|
|
|
|
Value::Primitive(Primitive::String(s)) => Ok(s.to_string()),
|
|
|
|
|
|
|
|
// TODO: this should definitely be more general with better errors
|
2019-05-13 17:30:51 +00:00
|
|
|
other => Err(ShellError::string(format!(
|
|
|
|
"Expected string, got {:?}",
|
|
|
|
other
|
|
|
|
))),
|
2019-05-11 22:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:12:38 +00:00
|
|
|
crate fn as_int(&self) -> Result<i64, ShellError> {
|
|
|
|
match self {
|
|
|
|
Value::Primitive(Primitive::Int(i)) => Ok(*i),
|
|
|
|
// TODO: this should definitely be more general with better errors
|
|
|
|
other => Err(ShellError::string(format!(
|
|
|
|
"Expected integer, got {:?}",
|
|
|
|
other
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:59:12 +00:00
|
|
|
crate fn string(s: impl Into<String>) -> Value {
|
|
|
|
Value::Primitive(Primitive::String(s.into()))
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:14:51 +00:00
|
|
|
crate fn bytes(s: impl Into<u128>) -> Value {
|
|
|
|
Value::Primitive(Primitive::Bytes(s.into()))
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:59:12 +00:00
|
|
|
crate fn int(s: impl Into<i64>) -> Value {
|
|
|
|
Value::Primitive(Primitive::Int(s.into()))
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:58:44 +00:00
|
|
|
#[allow(unused)]
|
2019-05-15 18:14:51 +00:00
|
|
|
crate fn system_date(s: SystemTime) -> Value {
|
|
|
|
Value::Primitive(Primitive::Date(s.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn system_date_result(s: Result<SystemTime, std::io::Error>) -> Value {
|
|
|
|
match s {
|
|
|
|
Ok(time) => Value::Primitive(Primitive::Date(time.into())),
|
|
|
|
Err(err) => Value::Error(Box::new(ShellError::string(format!("{}", err)))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:59:12 +00:00
|
|
|
crate fn boolean(s: impl Into<bool>) -> Value {
|
|
|
|
Value::Primitive(Primitive::Boolean(s.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn nothing() -> Value {
|
|
|
|
Value::Primitive(Primitive::Nothing)
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:58:44 +00:00
|
|
|
#[allow(unused)]
|
2019-05-10 16:59:12 +00:00
|
|
|
crate fn list(values: impl Into<Vec<Value>>) -> Value {
|
|
|
|
Value::List(values.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn select(obj: &Value, fields: &[String]) -> crate::object::Dictionary {
|
2019-05-15 18:14:51 +00:00
|
|
|
let mut out = crate::object::Dictionary::default();
|
|
|
|
|
|
|
|
let descs = obj.data_descriptors();
|
|
|
|
|
|
|
|
for field in fields {
|
|
|
|
match descs.iter().find(|d| d.name == *field) {
|
|
|
|
None => out.add(field.to_string(), Value::nothing()),
|
|
|
|
Some(desc) => out.add(field.to_string(), obj.get_data(desc).borrow().copy()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn reject(obj: &Value, fields: &[String]) -> crate::object::Dictionary {
|
2019-05-15 21:44:06 +00:00
|
|
|
let mut out = crate::object::Dictionary::default();
|
|
|
|
|
|
|
|
let descs = obj.data_descriptors();
|
|
|
|
|
|
|
|
for desc in descs {
|
|
|
|
if fields.contains(&desc.name) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
out.add(desc.name.clone(), obj.get_data(&desc).borrow().copy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|