2021-11-23 08:14:40 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-12-02 05:42:56 +00:00
|
|
|
use crate::ast::{CellPath, PathMember};
|
2022-01-12 04:06:56 +00:00
|
|
|
use crate::engine::CaptureBlock;
|
2021-12-02 05:42:56 +00:00
|
|
|
use crate::ShellError;
|
|
|
|
use crate::{Range, Spanned, Value};
|
2022-01-16 13:55:56 +00:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
2021-10-01 21:53:13 +00:00
|
|
|
|
|
|
|
pub trait FromValue: Sized {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Value {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
Ok(v.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<i64> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Int { val, span } => Ok(Spanned {
|
|
|
|
item: *val,
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
Value::Filesize { val, span } => Ok(Spanned {
|
|
|
|
item: *val as i64,
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
Value::Duration { val, span } => Ok(Spanned {
|
|
|
|
item: *val as i64,
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"integer".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for i64 {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Int { val, .. } => Ok(*val),
|
2021-10-14 04:43:49 +00:00
|
|
|
Value::Filesize { val, .. } => Ok(*val as i64),
|
|
|
|
Value::Duration { val, .. } => Ok(*val as i64),
|
2021-10-01 21:53:13 +00:00
|
|
|
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"integer".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<f64> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Int { val, span } => Ok(Spanned {
|
|
|
|
item: *val as f64,
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
Value::Float { val, span } => Ok(Spanned {
|
|
|
|
item: *val,
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"float".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for f64 {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Float { val, .. } => Ok(*val),
|
|
|
|
Value::Int { val, .. } => Ok(*val as f64),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"float".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 08:14:40 +00:00
|
|
|
impl FromValue for Spanned<usize> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
2022-02-20 20:20:41 +00:00
|
|
|
Value::Int { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(Spanned {
|
|
|
|
item: *val as usize,
|
|
|
|
span: *span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Filesize { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(Spanned {
|
|
|
|
item: *val as usize,
|
|
|
|
span: *span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Duration { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(Spanned {
|
|
|
|
item: *val as usize,
|
|
|
|
span: *span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 08:14:40 +00:00
|
|
|
|
|
|
|
v => Err(ShellError::CantConvert(
|
2022-02-20 20:20:41 +00:00
|
|
|
"non-negative integer".into(),
|
2021-11-23 08:14:40 +00:00
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-23 08:14:40 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for usize {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
2022-02-20 20:20:41 +00:00
|
|
|
Value::Int { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(*val as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Filesize { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(*val as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Duration { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(*val as usize)
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 08:14:40 +00:00
|
|
|
|
|
|
|
v => Err(ShellError::CantConvert(
|
2022-02-20 20:20:41 +00:00
|
|
|
"non-negative integer".into(),
|
2021-11-23 08:14:40 +00:00
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-23 08:14:40 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 21:53:13 +00:00
|
|
|
impl FromValue for String {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
// FIXME: we may want to fail a little nicer here
|
2021-11-14 19:25:57 +00:00
|
|
|
match v {
|
|
|
|
Value::CellPath { val, .. } => Ok(val.into_string()),
|
|
|
|
Value::String { val, .. } => Ok(val.clone()),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"string".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-14 19:25:57 +00:00
|
|
|
)),
|
|
|
|
}
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<String> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
Ok(Spanned {
|
2021-11-14 19:25:57 +00:00
|
|
|
item: match v {
|
|
|
|
Value::CellPath { val, .. } => val.into_string(),
|
|
|
|
Value::String { val, .. } => val.clone(),
|
|
|
|
v => {
|
|
|
|
return Err(ShellError::CantConvert(
|
|
|
|
"string".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-14 19:25:57 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
2021-10-11 18:45:31 +00:00
|
|
|
span: v.span()?,
|
2021-10-01 21:53:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 08:14:40 +00:00
|
|
|
impl FromValue for Vec<String> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
// FIXME: we may want to fail a little nicer here
|
|
|
|
match v {
|
|
|
|
Value::List { vals, .. } => vals
|
|
|
|
.iter()
|
|
|
|
.map(|val| match val {
|
|
|
|
Value::String { val, .. } => Ok(val.clone()),
|
|
|
|
c => Err(ShellError::CantConvert(
|
|
|
|
"string".into(),
|
|
|
|
c.get_type().to_string(),
|
|
|
|
c.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-23 08:14:40 +00:00
|
|
|
)),
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<String>, ShellError>>(),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"string".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-23 08:14:40 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 20:58:28 +00:00
|
|
|
impl FromValue for Vec<Spanned<String>> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
// FIXME: we may want to fail a little nicer here
|
|
|
|
match v {
|
|
|
|
Value::List { vals, .. } => vals
|
|
|
|
.iter()
|
|
|
|
.map(|val| match val {
|
|
|
|
Value::String { val, span } => Ok(Spanned {
|
|
|
|
item: val.clone(),
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
c => Err(ShellError::CantConvert(
|
|
|
|
"string".into(),
|
|
|
|
c.get_type().to_string(),
|
|
|
|
c.span()?,
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<Spanned<String>>, ShellError>>(),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"string".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 07:27:43 +00:00
|
|
|
impl FromValue for Vec<bool> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::List { vals, .. } => vals
|
|
|
|
.iter()
|
|
|
|
.map(|val| match val {
|
|
|
|
Value::Bool { val, .. } => Ok(*val),
|
|
|
|
c => Err(ShellError::CantConvert(
|
|
|
|
"bool".into(),
|
|
|
|
c.get_type().to_string(),
|
|
|
|
c.span()?,
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<bool>, ShellError>>(),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"bool".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 02:59:11 +00:00
|
|
|
impl FromValue for CellPath {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
2021-10-11 18:45:31 +00:00
|
|
|
let span = v.span()?;
|
2021-10-02 02:59:11 +00:00
|
|
|
match v {
|
|
|
|
Value::CellPath { val, .. } => Ok(val.clone()),
|
2021-10-02 05:43:43 +00:00
|
|
|
Value::String { val, .. } => Ok(CellPath {
|
|
|
|
members: vec![PathMember::String {
|
|
|
|
val: val.clone(),
|
|
|
|
span,
|
|
|
|
}],
|
|
|
|
}),
|
2022-02-20 20:20:41 +00:00
|
|
|
Value::Int { val, span } => {
|
|
|
|
if val.is_negative() {
|
|
|
|
Err(ShellError::NeedsPositiveValue(*span))
|
|
|
|
} else {
|
|
|
|
Ok(CellPath {
|
|
|
|
members: vec![PathMember::Int {
|
|
|
|
val: *val as usize,
|
|
|
|
span: *span,
|
|
|
|
}],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 05:50:33 +00:00
|
|
|
x => Err(ShellError::CantConvert(
|
|
|
|
"cell path".into(),
|
|
|
|
x.get_type().to_string(),
|
|
|
|
span,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-02 02:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 21:53:13 +00:00
|
|
|
impl FromValue for bool {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
2021-10-02 02:59:11 +00:00
|
|
|
Value::Bool { val, .. } => Ok(*val),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"bool".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<bool> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Bool { val, span } => Ok(Spanned {
|
|
|
|
item: *val,
|
|
|
|
span: *span,
|
|
|
|
}),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"bool".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 05:58:25 +00:00
|
|
|
impl FromValue for DateTime<FixedOffset> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Date { val, .. } => Ok(*val),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"date".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-20 05:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<DateTime<FixedOffset>> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Date { val, span } => Ok(Spanned {
|
|
|
|
item: *val,
|
|
|
|
span: *span,
|
|
|
|
}),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"date".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-20 05:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 21:53:13 +00:00
|
|
|
|
|
|
|
impl FromValue for Range {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Range { val, .. } => Ok((**val).clone()),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"range".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<Range> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Range { val, span } => Ok(Spanned {
|
|
|
|
item: (**val).clone(),
|
|
|
|
span: *span,
|
|
|
|
}),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"range".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 05:58:25 +00:00
|
|
|
impl FromValue for Vec<u8> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Binary { val, .. } => Ok(val.clone()),
|
|
|
|
Value::String { val, .. } => Ok(val.bytes().collect()),
|
2021-11-06 05:50:33 +00:00
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"binary data".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-06 05:50:33 +00:00
|
|
|
)),
|
2021-10-20 05:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 21:53:13 +00:00
|
|
|
|
2022-07-11 11:26:00 +00:00
|
|
|
impl FromValue for Spanned<Vec<u8>> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Binary { val, span } => Ok(Spanned {
|
|
|
|
item: val.clone(),
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
Value::String { val, span } => Ok(Spanned {
|
|
|
|
item: val.bytes().collect(),
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"binary data".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 08:14:40 +00:00
|
|
|
impl FromValue for Spanned<PathBuf> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::String { val, span } => Ok(Spanned {
|
|
|
|
item: PathBuf::from_str(val)
|
|
|
|
.map_err(|err| ShellError::FileNotFoundCustom(err.to_string(), *span))?,
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"range".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-11-23 08:14:40 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 17:45:09 +00:00
|
|
|
|
|
|
|
impl FromValue for Vec<Value> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
// FIXME: we may want to fail a little nicer here
|
|
|
|
match v {
|
|
|
|
Value::List { vals, .. } => Ok(vals.clone()),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"Vector of values".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2021-12-18 17:45:09 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 04:06:56 +00:00
|
|
|
|
2022-01-15 23:50:11 +00:00
|
|
|
// A record
|
|
|
|
impl FromValue for (Vec<String>, Vec<Value>) {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Record { cols, vals, .. } => Ok((cols.clone(), vals.clone())),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"Record".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2022-01-15 23:50:11 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 04:06:56 +00:00
|
|
|
impl FromValue for CaptureBlock {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Block { val, captures, .. } => Ok(CaptureBlock {
|
|
|
|
block_id: *val,
|
|
|
|
captures: captures.clone(),
|
|
|
|
}),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"Block".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2022-01-12 04:06:56 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromValue for Spanned<CaptureBlock> {
|
|
|
|
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
|
|
|
match v {
|
|
|
|
Value::Block {
|
|
|
|
val,
|
|
|
|
captures,
|
|
|
|
span,
|
|
|
|
} => Ok(Spanned {
|
|
|
|
item: CaptureBlock {
|
|
|
|
block_id: *val,
|
|
|
|
captures: captures.clone(),
|
|
|
|
},
|
|
|
|
span: *span,
|
|
|
|
}),
|
|
|
|
v => Err(ShellError::CantConvert(
|
|
|
|
"Block".into(),
|
|
|
|
v.get_type().to_string(),
|
|
|
|
v.span()?,
|
2022-04-18 12:34:10 +00:00
|
|
|
None,
|
2022-01-12 04:06:56 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|