nushell/src/data/types.rs
Yehuda Katz f70c6d5d48 Extract nu_source into a crate
This commit extracts Tag, Span, Text, as well as source-related debug
facilities into a new crate called nu_source.

This change is much bigger than one might have expected because the
previous code relied heavily on implementing inherent methods on
`Tagged<T>` and `Spanned<T>`, which is no longer possible.

As a result, this change creates more concrete types instead of using
`Tagged<T>`. One notable example: Tagged<Value> became Value, and Value
became UntaggedValue.

This change clarifies the intent of the code in many places, but it does
make it a big change.
2019-11-25 07:37:33 -08:00

106 lines
3.0 KiB
Rust

use crate::prelude::*;
use log::trace;
use nu_source::Tagged;
pub trait ExtractType: Sized {
fn extract(value: &Value) -> Result<Self, ShellError>;
}
impl<T: ExtractType> ExtractType for Tagged<T> {
fn extract(value: &Value) -> Result<Tagged<T>, ShellError> {
let name = std::any::type_name::<T>();
trace!("<Tagged> Extracting {:?} for Tagged<{}>", value, name);
Ok(T::extract(value)?.tagged(value.tag()))
}
}
impl ExtractType for bool {
fn extract(value: &Value) -> Result<bool, ShellError> {
trace!("Extracting {:?} for bool", value);
match &value {
Value {
value: UntaggedValue::Primitive(Primitive::Boolean(b)),
..
} => Ok(*b),
Value {
value: UntaggedValue::Primitive(Primitive::Nothing),
..
} => Ok(false),
other => Err(ShellError::type_error(
"Boolean",
other.type_name().spanned(other.span()),
)),
}
}
}
impl ExtractType for std::path::PathBuf {
fn extract(value: &Value) -> Result<std::path::PathBuf, ShellError> {
trace!("Extracting {:?} for PathBuf", value);
match &value {
Value {
value: UntaggedValue::Primitive(Primitive::Path(p)),
..
} => Ok(p.clone()),
other => Err(ShellError::type_error(
"Path",
other.type_name().spanned(other.span()),
)),
}
}
}
impl ExtractType for i64 {
fn extract(value: &Value) -> Result<i64, ShellError> {
trace!("Extracting {:?} for i64", value);
match &value {
&Value {
value: UntaggedValue::Primitive(Primitive::Int(int)),
..
} => Ok(int.tagged(&value.tag).coerce_into("converting to i64")?),
other => Err(ShellError::type_error(
"Integer",
other.type_name().spanned(other.span()),
)),
}
}
}
impl ExtractType for u64 {
fn extract(value: &Value) -> Result<u64, ShellError> {
trace!("Extracting {:?} for u64", value);
match &value {
&Value {
value: UntaggedValue::Primitive(Primitive::Int(int)),
..
} => Ok(int.tagged(&value.tag).coerce_into("converting to u64")?),
other => Err(ShellError::type_error(
"Integer",
other.type_name().spanned(other.span()),
)),
}
}
}
impl ExtractType for String {
fn extract(value: &Value) -> Result<String, ShellError> {
trace!("Extracting {:?} for String", value);
match value {
Value {
value: UntaggedValue::Primitive(Primitive::String(string)),
..
} => Ok(string.clone()),
other => Err(ShellError::type_error(
"String",
other.type_name().spanned(other.span()),
)),
}
}
}