nushell/src/object/types.rs

288 lines
8.6 KiB
Rust
Raw Normal View History

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 {
fn extract(value: &Spanned<Value>) -> Result<Self, ShellError>;
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<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 {
default fn extract(_value: &Spanned<Value>) -> Result<T, ShellError> {
let name = unsafe { std::intrinsics::type_name::<T>() };
Err(ShellError::unimplemented(format!(
"<T> ExtractType for {}",
name
)))
}
default fn check(_value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
Err(ShellError::unimplemented("ExtractType for T"))
}
default fn syntax_type() -> hir::SyntaxType {
hir::SyntaxType::Any
}
}
impl<T: ExtractType> ExtractType for Vec<Spanned<T>> {
fn extract(value: &Spanned<Value>) -> Result<Self, ShellError> {
let name = unsafe { std::intrinsics::type_name::<T>() };
trace!("<Vec> Extracting {:?} for Vec<{}>", value, name);
match value.item() {
Value::List(items) => {
let mut out = vec![];
for item in items {
out.push(T::extract(item)?.spanned(item.span));
}
Ok(out)
}
other => Err(ShellError::type_error(
"Vec",
other.type_name().spanned(value.span),
)),
}
}
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
match value.item() {
Value::List(_) => Ok(value),
other => Err(ShellError::type_error(
"Vec",
other.type_name().spanned(value.span),
)),
}
}
fn syntax_type() -> hir::SyntaxType {
hir::SyntaxType::List
}
}
impl<T: ExtractType, U: ExtractType> ExtractType for (T, U) {
fn extract(value: &Spanned<Value>) -> Result<(T, U), ShellError> {
let t_name = unsafe { std::intrinsics::type_name::<T>() };
let u_name = unsafe { std::intrinsics::type_name::<U>() };
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",
"not-two".spanned(value.span),
))
}
}
other => Err(ShellError::type_error(
"two-element-tuple",
other.type_name().spanned(value.span),
)),
}
}
}
impl<T: ExtractType> ExtractType for Option<T> {
fn extract(value: &Spanned<Value>) -> Result<Option<T>, ShellError> {
let name = unsafe { std::intrinsics::type_name::<T>() };
trace!("<Option> Extracting {:?} for Option<{}>", value, name);
let result = match value.item() {
Value::Primitive(Primitive::Nothing) => None,
_ => Some(T::extract(value)?),
};
Ok(result)
}
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
match value.item() {
Value::Primitive(Primitive::Nothing) => Ok(value),
_ => T::check(value),
}
}
fn syntax_type() -> hir::SyntaxType {
T::syntax_type()
}
}
2019-07-15 23:16:27 +02:00
impl<T: ExtractType> ExtractType for Spanned<T> {
fn extract(value: &Spanned<Value>) -> Result<Spanned<T>, ShellError> {
2019-08-02 21:15:07 +02:00
let name = unsafe { std::intrinsics::type_name::<T>() };
trace!("<Spanned> Extracting {:?} for Spanned<{}>", value, name);
2019-07-15 23:16:27 +02:00
Ok(T::extract(value)?.spanned(value.span))
}
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
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 {
fn extract(value: &Spanned<Value>) -> Result<Value, ShellError> {
trace!("<Spanned> 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-02 21:15:07 +02:00
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
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
}
fn extract(value: &'a Spanned<Value>) -> Result<bool, ShellError> {
trace!("Extracting {:?} for bool", value);
match &value {
Spanned {
item: Value::Primitive(Primitive::Boolean(b)),
..
} => Ok(*b),
Spanned {
item: Value::Primitive(Primitive::Nothing),
..
} => Ok(false),
other => Err(ShellError::type_error("Boolean", other.spanned_type_name())),
}
2019-07-08 18:44:53 +02:00
}
2019-07-09 06:31:26 +02:00
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
2019-08-02 21:15:07 +02:00
match &value {
value @ Spanned {
item: Value::Primitive(Primitive::Boolean(_)),
..
} => Ok(value),
other => Err(ShellError::type_error("Boolean", other.spanned_type_name())),
}
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
}
fn extract(value: &'a Spanned<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 {
Spanned {
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)),
other => Err(ShellError::type_error("Path", other.spanned_type_name())),
}
}
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
match &value {
v @ Spanned {
item: Value::Primitive(Primitive::Path(_)),
..
} => Ok(v),
other => Err(ShellError::type_error("Path", other.spanned_type_name())),
}
}
}
2019-07-08 18:44:53 +02:00
impl ExtractType for i64 {
fn extract(value: &Spanned<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 {
&Spanned {
item: Value::Primitive(Primitive::Int(int)),
..
} => Ok(int),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
}
}
2019-07-09 06:31:26 +02:00
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
2019-07-08 18:44:53 +02:00
match value {
v @ Spanned {
item: Value::Primitive(Primitive::Int(_)),
..
} => Ok(v),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
}
}
}
impl ExtractType for String {
fn extract(value: &Spanned<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 {
Spanned {
item: Value::Primitive(Primitive::String(string)),
..
} => Ok(string.clone()),
other => Err(ShellError::type_error("String", other.spanned_type_name())),
}
}
2019-07-09 06:31:26 +02:00
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
2019-07-06 00:08:58 +02:00
match value {
2019-07-08 18:44:53 +02:00
v @ Spanned {
2019-07-09 06:31:26 +02:00
item: Value::Primitive(Primitive::String(_)),
2019-07-08 18:44:53 +02:00
..
} => Ok(v),
2019-07-09 06:31:26 +02:00
other => Err(ShellError::type_error("String", other.spanned_type_name())),
2019-07-06 00:08:58 +02:00
}
}
}
2019-07-09 06:31:26 +02:00
impl ExtractType for value::Block {
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<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 {
v @ Spanned {
item: Value::Block(_),
..
} => Ok(v),
other => Err(ShellError::type_error("Block", other.spanned_type_name())),
}
}
2019-07-08 18:44:53 +02:00
fn extract(value: &Spanned<Value>) -> Result<value::Block, ShellError> {
match value {
Spanned {
item: Value::Block(block),
..
} => Ok(block.clone()),
other => Err(ShellError::type_error("Block", other.spanned_type_name())),
}
}
2019-05-15 18:12:38 +02:00
}