2019-07-06 00:08:58 +02:00
|
|
|
use crate::object::base as value;
|
|
|
|
use crate::parser::hir;
|
|
|
|
use crate::prelude::*;
|
2019-08-02 21:15:07 +02:00
|
|
|
use log::trace;
|
2019-07-15 23:16:27 +02:00
|
|
|
use std::path::PathBuf;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
pub trait ExtractType: Sized {
|
2019-08-01 03:58:42 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<Self, ShellError>;
|
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError>;
|
2019-07-15 23:16:27 +02:00
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
hir::SyntaxType::Any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl<T> ExtractType for T {
|
2019-08-09 06:51:21 +02:00
|
|
|
default fn extract(_value: &Tagged<Value>) -> Result<T, ShellError> {
|
2019-08-06 18:26:33 +02:00
|
|
|
let name = std::intrinsics::type_name::<T>();
|
2019-08-02 21:15:07 +02:00
|
|
|
Err(ShellError::unimplemented(format!(
|
|
|
|
"<T> ExtractType for {}",
|
|
|
|
name
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
default fn check(_value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
Err(ShellError::unimplemented("ExtractType for T"))
|
|
|
|
}
|
|
|
|
|
|
|
|
default fn syntax_type() -> hir::SyntaxType {
|
|
|
|
hir::SyntaxType::Any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
impl<T: ExtractType> ExtractType for Vec<Tagged<T>> {
|
|
|
|
fn extract(value: &Tagged<Value>) -> Result<Self, ShellError> {
|
2019-08-06 18:26:33 +02:00
|
|
|
let name = std::intrinsics::type_name::<T>();
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("<Vec> Extracting {:?} for Vec<{}>", value, name);
|
|
|
|
|
|
|
|
match value.item() {
|
|
|
|
Value::List(items) => {
|
|
|
|
let mut out = vec![];
|
|
|
|
|
|
|
|
for item in items {
|
2019-08-09 06:51:21 +02:00
|
|
|
out.push(T::extract(item)?.tagged(item.tag()));
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
other => Err(ShellError::type_error(
|
|
|
|
"Vec",
|
2019-08-09 06:51:21 +02:00
|
|
|
other.type_name().tagged(value.tag()),
|
2019-08-02 21:15:07 +02:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
match value.item() {
|
|
|
|
Value::List(_) => Ok(value),
|
|
|
|
other => Err(ShellError::type_error(
|
|
|
|
"Vec",
|
2019-08-09 06:51:21 +02:00
|
|
|
other.type_name().tagged(value.tag()),
|
2019-08-02 21:15:07 +02:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
hir::SyntaxType::List
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ExtractType, U: ExtractType> ExtractType for (T, U) {
|
2019-08-09 06:51:21 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<(T, U), ShellError> {
|
2019-08-06 18:26:33 +02:00
|
|
|
let t_name = std::intrinsics::type_name::<T>();
|
|
|
|
let u_name = std::intrinsics::type_name::<U>();
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
trace!("Extracting {:?} for ({}, {})", value, t_name, u_name);
|
|
|
|
|
|
|
|
match value.item() {
|
|
|
|
Value::List(items) => {
|
|
|
|
if items.len() == 2 {
|
|
|
|
let first = &items[0];
|
|
|
|
let second = &items[1];
|
|
|
|
|
|
|
|
Ok((T::extract(first)?, U::extract(second)?))
|
|
|
|
} else {
|
|
|
|
Err(ShellError::type_error(
|
|
|
|
"two-element-tuple",
|
2019-08-09 06:51:21 +02:00
|
|
|
"not-two".tagged(value.tag()),
|
2019-08-02 21:15:07 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other => Err(ShellError::type_error(
|
|
|
|
"two-element-tuple",
|
2019-08-09 06:51:21 +02:00
|
|
|
other.type_name().tagged(value.tag()),
|
2019-08-02 21:15:07 +02:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ExtractType> ExtractType for Option<T> {
|
2019-08-09 06:51:21 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<Option<T>, ShellError> {
|
2019-08-06 18:26:33 +02:00
|
|
|
let name = std::intrinsics::type_name::<T>();
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("<Option> Extracting {:?} for Option<{}>", value, name);
|
|
|
|
|
|
|
|
let result = match value.item() {
|
|
|
|
Value::Primitive(Primitive::Nothing) => None,
|
|
|
|
_ => Some(T::extract(value)?),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
match value.item() {
|
|
|
|
Value::Primitive(Primitive::Nothing) => Ok(value),
|
|
|
|
_ => T::check(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
T::syntax_type()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
impl<T: ExtractType> ExtractType for Tagged<T> {
|
|
|
|
fn extract(value: &Tagged<Value>) -> Result<Tagged<T>, ShellError> {
|
2019-08-06 18:26:33 +02:00
|
|
|
let name = std::intrinsics::type_name::<T>();
|
2019-08-09 06:51:21 +02:00
|
|
|
trace!("<Tagged> Extracting {:?} for Tagged<{}>", value, name);
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2019-08-06 05:03:13 +02:00
|
|
|
Ok(T::extract(value)?.tagged(value.tag()))
|
2019-07-15 23:16:27 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-07-15 23:16:27 +02:00
|
|
|
T::check(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
T::syntax_type()
|
|
|
|
}
|
2019-07-06 00:08:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl ExtractType for Value {
|
2019-08-09 06:51:21 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<Value, ShellError> {
|
|
|
|
trace!("<Tagged> Extracting {:?} for Value", value);
|
2019-07-06 00:08:58 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
Ok(value.item().clone())
|
|
|
|
}
|
2019-07-08 18:44:53 +02:00
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
SyntaxType::Any
|
2019-07-06 00:08:58 +02:00
|
|
|
}
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl ExtractType for bool {
|
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
hir::SyntaxType::Boolean
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
fn extract(value: &'a Tagged<Value>) -> Result<bool, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("Extracting {:?} for bool", value);
|
|
|
|
|
|
|
|
match &value {
|
2019-08-09 06:51:21 +02:00
|
|
|
Tagged {
|
2019-08-02 21:15:07 +02:00
|
|
|
item: Value::Primitive(Primitive::Boolean(b)),
|
|
|
|
..
|
|
|
|
} => Ok(*b),
|
2019-08-09 06:51:21 +02:00
|
|
|
Tagged {
|
2019-08-02 21:15:07 +02:00
|
|
|
item: Value::Primitive(Primitive::Nothing),
|
|
|
|
..
|
|
|
|
} => Ok(false),
|
2019-08-09 06:51:21 +02:00
|
|
|
other => Err(ShellError::type_error("Boolean", other.tagged_type_name())),
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
match &value {
|
2019-08-09 06:51:21 +02:00
|
|
|
value @ Tagged {
|
2019-08-02 21:15:07 +02:00
|
|
|
item: Value::Primitive(Primitive::Boolean(_)),
|
|
|
|
..
|
|
|
|
} => Ok(value),
|
2019-08-09 06:51:21 +02:00
|
|
|
other => Err(ShellError::type_error("Boolean", other.tagged_type_name())),
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-07-06 00:08:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:16:27 +02:00
|
|
|
impl ExtractType for std::path::PathBuf {
|
|
|
|
fn syntax_type() -> hir::SyntaxType {
|
|
|
|
hir::SyntaxType::Path
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn extract(value: &'a Tagged<Value>) -> Result<std::path::PathBuf, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("Extracting {:?} for PathBuf", value);
|
|
|
|
|
2019-07-15 23:16:27 +02:00
|
|
|
match &value {
|
2019-08-01 03:58:42 +02:00
|
|
|
Tagged {
|
2019-07-15 23:16:27 +02:00
|
|
|
item: Value::Primitive(Primitive::String(p)),
|
2019-07-16 09:08:35 +02:00
|
|
|
..
|
2019-07-15 23:16:27 +02:00
|
|
|
} => Ok(PathBuf::from(p)),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("Path", other.tagged_type_name())),
|
2019-07-15 23:16:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-07-15 23:16:27 +02:00
|
|
|
match &value {
|
2019-08-01 03:58:42 +02:00
|
|
|
v @ Tagged {
|
2019-07-15 23:16:27 +02:00
|
|
|
item: Value::Primitive(Primitive::Path(_)),
|
|
|
|
..
|
|
|
|
} => Ok(v),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("Path", other.tagged_type_name())),
|
2019-07-15 23:16:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
impl ExtractType for i64 {
|
2019-08-01 03:58:42 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<i64, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("Extracting {:?} for i64", value);
|
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
match value {
|
2019-08-01 03:58:42 +02:00
|
|
|
&Tagged {
|
2019-07-08 18:44:53 +02:00
|
|
|
item: Value::Primitive(Primitive::Int(int)),
|
|
|
|
..
|
|
|
|
} => Ok(int),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("Integer", other.tagged_type_name())),
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-07-08 18:44:53 +02:00
|
|
|
match value {
|
2019-08-01 03:58:42 +02:00
|
|
|
v @ Tagged {
|
2019-07-08 18:44:53 +02:00
|
|
|
item: Value::Primitive(Primitive::Int(_)),
|
|
|
|
..
|
|
|
|
} => Ok(v),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("Integer", other.tagged_type_name())),
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExtractType for String {
|
2019-08-01 03:58:42 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<String, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("Extracting {:?} for String", value);
|
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
match value {
|
2019-08-01 03:58:42 +02:00
|
|
|
Tagged {
|
2019-07-08 18:44:53 +02:00
|
|
|
item: Value::Primitive(Primitive::String(string)),
|
|
|
|
..
|
|
|
|
} => Ok(string.clone()),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("String", other.tagged_type_name())),
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-07-06 00:08:58 +02:00
|
|
|
match value {
|
2019-08-01 03:58:42 +02:00
|
|
|
v @ Tagged {
|
2019-07-09 06:31:26 +02:00
|
|
|
item: Value::Primitive(Primitive::String(_)),
|
2019-07-08 18:44:53 +02:00
|
|
|
..
|
|
|
|
} => Ok(v),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("String", other.tagged_type_name())),
|
2019-07-06 00:08:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
impl ExtractType for value::Block {
|
2019-08-01 03:58:42 +02:00
|
|
|
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
|
2019-08-02 21:15:07 +02:00
|
|
|
trace!("Extracting {:?} for Block", value);
|
|
|
|
|
2019-07-06 00:08:58 +02:00
|
|
|
match value {
|
2019-08-01 03:58:42 +02:00
|
|
|
v @ Tagged {
|
2019-07-06 00:08:58 +02:00
|
|
|
item: Value::Block(_),
|
|
|
|
..
|
|
|
|
} => Ok(v),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("Block", other.tagged_type_name())),
|
2019-07-06 00:08:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn extract(value: &Tagged<Value>) -> Result<value::Block, ShellError> {
|
2019-07-08 18:44:53 +02:00
|
|
|
match value {
|
2019-08-01 03:58:42 +02:00
|
|
|
Tagged {
|
2019-07-08 18:44:53 +02:00
|
|
|
item: Value::Block(block),
|
|
|
|
..
|
|
|
|
} => Ok(block.clone()),
|
2019-08-01 03:58:42 +02:00
|
|
|
other => Err(ShellError::type_error("Block", other.tagged_type_name())),
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|