nushell/src/object/types.rs

175 lines
4.9 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::*;
use derive_new::new;
2019-07-08 18:44:53 +02:00
use serde_derive::Deserialize;
2019-05-10 18:59:12 +02:00
2019-07-06 00:08:58 +02:00
pub trait Type: std::fmt::Debug + Send {
2019-07-08 18:44:53 +02:00
type Extractor: ExtractType;
2019-07-06 00:08:58 +02:00
fn name(&self) -> &'static str;
2019-07-08 18:44:53 +02:00
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError>;
2019-07-06 00:08:58 +02:00
fn coerce(&self) -> Option<hir::ExpressionKindHint> {
None
}
}
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-06 00:08:58 +02:00
}
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, new)]
pub struct Any;
impl Type for Any {
2019-07-08 18:44:53 +02:00
type Extractor = Spanned<Value>;
2019-07-06 00:08:58 +02:00
fn name(&self) -> &'static str {
"Any"
}
2019-07-08 18:44:53 +02:00
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
Ok(value)
}
}
impl ExtractType for Spanned<Value> {
fn extract(value: &Spanned<Value>) -> Result<Self, ShellError> {
Ok(value.clone())
}
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
2019-07-06 00:08:58 +02:00
Ok(value)
}
}
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, new)]
pub struct Integer;
impl Type for Integer {
2019-07-08 18:44:53 +02:00
type Extractor = i64;
fn name(&self) -> &'static str {
"Integer"
}
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
match value {
v @ Spanned {
item: Value::Primitive(Primitive::Int(_)),
..
} => Ok(v),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
}
}
}
impl ExtractType for i64 {
fn extract(value: &Spanned<Value>) -> Result<i64, ShellError> {
match value {
&Spanned {
item: Value::Primitive(Primitive::Int(int)),
..
} => Ok(int),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
}
}
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
match value {
v @ Spanned {
item: Value::Primitive(Primitive::Int(_)),
..
} => Ok(v),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
}
}
}
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, new)]
pub struct NuString;
impl Type for NuString {
type Extractor = String;
2019-07-06 00:08:58 +02:00
fn name(&self) -> &'static str {
"Integer"
}
2019-07-08 18:44:53 +02:00
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
2019-07-06 00:08:58 +02:00
match value {
v @ Spanned {
item: Value::Primitive(Primitive::Int(_)),
..
} => Ok(v),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
}
}
}
2019-07-08 18:44:53 +02:00
impl ExtractType for String {
fn extract(value: &Spanned<Value>) -> Result<String, ShellError> {
match value {
Spanned {
item: Value::Primitive(Primitive::String(string)),
..
} => Ok(string.clone()),
other => Err(ShellError::type_error("String", other.spanned_type_name())),
}
}
fn check(&self, 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 {
item: Value::Primitive(Primitive::Int(_)),
..
} => Ok(v),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
2019-07-06 00:08:58 +02:00
}
}
}
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, new)]
pub struct Block;
impl Type for Block {
2019-07-08 18:44:53 +02:00
type Extractor = value::Block;
2019-07-06 00:08:58 +02:00
fn name(&self) -> &'static str {
"Block"
}
2019-07-08 18:44:53 +02:00
fn check(&self, value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
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
impl ExtractType for value::Block {
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())),
}
}
fn check(&self, 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 {
item: Value::Block(_),
..
} => Ok(v),
other => Err(ShellError::type_error("Block", other.spanned_type_name())),
2019-07-06 00:08:58 +02:00
}
}
2019-05-15 18:12:38 +02:00
}