nushell/crates/nu-protocol/src/value/from_value.rs

597 lines
18 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
Add pattern matching (#8590) # Description This adds `match` and basic pattern matching. An example: ``` match $x { 1..10 => { print "Value is between 1 and 10" } { foo: $bar } => { print $"Value has a 'foo' field with value ($bar)" } [$a, $b] => { print $"Value is a list with two items: ($a) and ($b)" } _ => { print "Value is none of the above" } } ``` Like the recent changes to `if` to allow it to be used as an expression, `match` can also be used as an expression. This allows you to assign the result to a variable, eg) `let xyz = match ...` I've also included a short-hand pattern for matching records, as I think it might help when doing a lot of record patterns: `{$foo}` which is equivalent to `{foo: $foo}`. There are still missing components, so consider this the first step in full pattern matching support. Currently missing: * Patterns for strings * Or-patterns (like the `|` in Rust) * Patterns for tables (unclear how we want to match a table, so it'll need some design) * Patterns for binary values * And much more # User-Facing Changes [see above] # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2023-03-24 01:52:01 +00:00
use crate::ast::{CellPath, MatchPattern, PathMember};
use crate::engine::{Block, Closure};
use crate::ShellError;
use crate::{Range, Spanned, Value};
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,
2021-10-01 21:53:13 +00:00
span: *span,
}),
Value::Duration { val, span } => Ok(Spanned {
item: *val,
2021-10-01 21:53:13 +00:00
span: *span,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "integer".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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),
Value::Filesize { val, .. } => Ok(*val),
Value::Duration { val, .. } => Ok(*val),
2021-10-01 21:53:13 +00:00
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "integer".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "float".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "float".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
2021-10-01 21:53:13 +00:00
}
}
}
impl FromValue for Spanned<usize> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
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,
})
}
}
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "non-negative integer".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
impl FromValue for usize {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
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)
}
}
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "non-negative integer".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
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
match v {
Value::CellPath { val, .. } => Ok(val.into_string()),
Value::String { val, .. } => Ok(val.clone()),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
2021-10-01 21:53:13 +00:00
}
}
impl FromValue for Spanned<String> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
Ok(Spanned {
item: match v {
Value::CellPath { val, .. } => val.into_string(),
Value::String { val, .. } => val.clone(),
v => {
2023-03-06 17:33:09 +00:00
return Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
})
}
},
2021-10-11 18:45:31 +00:00
span: v.span()?,
2021-10-01 21:53:13 +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()),
2023-03-06 17:33:09 +00:00
c => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: c.get_type().to_string(),
span: c.span()?,
help: None,
}),
})
.collect::<Result<Vec<String>, ShellError>>(),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
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,
}),
2023-03-06 17:33:09 +00:00
c => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: c.get_type().to_string(),
span: c.span()?,
help: None,
}),
})
.collect::<Result<Vec<Spanned<String>>, ShellError>>(),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
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),
2023-03-06 17:33:09 +00:00
c => Err(ShellError::CantConvert {
to_type: "bool".into(),
from_type: c.get_type().to_string(),
span: c.span()?,
help: None,
}),
})
.collect::<Result<Vec<bool>, ShellError>>(),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "bool".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: 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,
Optional members in cell paths: Attempt 2 (#8379) This is a follow up from https://github.com/nushell/nushell/pull/7540. Please provide feedback if you have the time! ## Summary This PR lets you use `?` to indicate that a member in a cell path is optional and Nushell should return `null` if that member cannot be accessed. Unlike the previous PR, `?` is now a _postfix_ modifier for cell path members. A cell path of `.foo?.bar` means that `foo` is optional and `bar` is not. `?` does _not_ suppress all errors; it is intended to help in situations where data has "holes", i.e. the data types are correct but something is missing. Type mismatches (like trying to do a string path access on a date) will still fail. ### Record Examples ```bash { foo: 123 }.foo # returns 123 { foo: 123 }.bar # errors { foo: 123 }.bar? # returns null { foo: 123 } | get bar # errors { foo: 123 } | get bar? # returns null { foo: 123 }.bar.baz # errors { foo: 123 }.bar?.baz # errors because `baz` is not present on the result from `bar?` { foo: 123 }.bar.baz? # errors { foo: 123 }.bar?.baz? # returns null ``` ### List Examples ``` 〉[{foo: 1} {foo: 2} {}].foo Error: nu::shell::column_not_found × Cannot find column ╭─[entry #30:1:1] 1 │ [{foo: 1} {foo: 2} {}].foo · ─┬ ─┬─ · │ ╰── cannot find column 'foo' · ╰── value originates here ╰──── 〉[{foo: 1} {foo: 2} {}].foo? ╭───┬───╮ │ 0 │ 1 │ │ 1 │ 2 │ │ 2 │ │ ╰───┴───╯ 〉[{foo: 1} {foo: 2} {}].foo?.2 | describe nothing 〉[a b c].4? | describe nothing 〉[{foo: 1} {foo: 2} {}] | where foo? == 1 ╭───┬─────╮ │ # │ foo │ ├───┼─────┤ │ 0 │ 1 │ ╰───┴─────╯ ``` # Breaking changes 1. Column names with `?` in them now need to be quoted. 2. The `-i`/`--ignore-errors` flag has been removed from `get` and `select` 1. After this PR, most `get` error handling can be done with `?` and/or `try`/`catch`. 4. Cell path accesses like this no longer work without a `?`: ```bash 〉[{a:1 b:2} {a:3}].b.0 2 ``` We had some clever code that was able to recognize that since we only want row `0`, it's OK if other rows are missing column `b`. I removed that because it's tricky to maintain, and now that query needs to be written like: ```bash 〉[{a:1 b:2} {a:3}].b?.0 2 ``` I think the regression is acceptable for now. I plan to do more work in the future to enable streaming of cell path accesses, and when that happens I'll be able to make `.b.0` work again.
2023-03-16 03:50:58 +00:00
optional: false,
2021-10-02 05:43:43 +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,
Optional members in cell paths: Attempt 2 (#8379) This is a follow up from https://github.com/nushell/nushell/pull/7540. Please provide feedback if you have the time! ## Summary This PR lets you use `?` to indicate that a member in a cell path is optional and Nushell should return `null` if that member cannot be accessed. Unlike the previous PR, `?` is now a _postfix_ modifier for cell path members. A cell path of `.foo?.bar` means that `foo` is optional and `bar` is not. `?` does _not_ suppress all errors; it is intended to help in situations where data has "holes", i.e. the data types are correct but something is missing. Type mismatches (like trying to do a string path access on a date) will still fail. ### Record Examples ```bash { foo: 123 }.foo # returns 123 { foo: 123 }.bar # errors { foo: 123 }.bar? # returns null { foo: 123 } | get bar # errors { foo: 123 } | get bar? # returns null { foo: 123 }.bar.baz # errors { foo: 123 }.bar?.baz # errors because `baz` is not present on the result from `bar?` { foo: 123 }.bar.baz? # errors { foo: 123 }.bar?.baz? # returns null ``` ### List Examples ``` 〉[{foo: 1} {foo: 2} {}].foo Error: nu::shell::column_not_found × Cannot find column ╭─[entry #30:1:1] 1 │ [{foo: 1} {foo: 2} {}].foo · ─┬ ─┬─ · │ ╰── cannot find column 'foo' · ╰── value originates here ╰──── 〉[{foo: 1} {foo: 2} {}].foo? ╭───┬───╮ │ 0 │ 1 │ │ 1 │ 2 │ │ 2 │ │ ╰───┴───╯ 〉[{foo: 1} {foo: 2} {}].foo?.2 | describe nothing 〉[a b c].4? | describe nothing 〉[{foo: 1} {foo: 2} {}] | where foo? == 1 ╭───┬─────╮ │ # │ foo │ ├───┼─────┤ │ 0 │ 1 │ ╰───┴─────╯ ``` # Breaking changes 1. Column names with `?` in them now need to be quoted. 2. The `-i`/`--ignore-errors` flag has been removed from `get` and `select` 1. After this PR, most `get` error handling can be done with `?` and/or `try`/`catch`. 4. Cell path accesses like this no longer work without a `?`: ```bash 〉[{a:1 b:2} {a:3}].b.0 2 ``` We had some clever code that was able to recognize that since we only want row `0`, it's OK if other rows are missing column `b`. I removed that because it's tricky to maintain, and now that query needs to be written like: ```bash 〉[{a:1 b:2} {a:3}].b?.0 2 ``` I think the regression is acceptable for now. I plan to do more work in the future to enable streaming of cell path accesses, and when that happens I'll be able to make `.b.0` work again.
2023-03-16 03:50:58 +00:00
optional: false,
}],
})
}
}
2023-03-06 17:33:09 +00:00
x => Err(ShellError::CantConvert {
to_type: "cell path".into(),
from_type: x.get_type().to_string(),
span,
2023-03-06 17:33:09 +00:00
help: None,
}),
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),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "bool".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "bool".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "date".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "date".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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()),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "range".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "range".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
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()),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "binary data".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
2021-10-20 05:58:25 +00:00
}
}
}
2021-10-01 21:53:13 +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,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "binary data".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
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,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "range".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
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()),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "Vector of values".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
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())),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "Record".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
2022-01-15 23:50:11 +00:00
}
}
}
impl FromValue for Closure {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::Closure { val, captures, .. } => Ok(Closure {
block_id: *val,
captures: captures.clone(),
}),
Value::Block { val, .. } => Ok(Closure {
block_id: *val,
captures: HashMap::new(),
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "Closure".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
impl FromValue for Block {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::Block { val, .. } => Ok(Block { block_id: *val }),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "Block".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
impl FromValue for Spanned<Closure> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::Closure {
val,
captures,
span,
} => Ok(Spanned {
item: Closure {
block_id: *val,
captures: captures.clone(),
},
span: *span,
}),
2023-03-06 17:33:09 +00:00
v => Err(ShellError::CantConvert {
to_type: "Closure".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
Add pattern matching (#8590) # Description This adds `match` and basic pattern matching. An example: ``` match $x { 1..10 => { print "Value is between 1 and 10" } { foo: $bar } => { print $"Value has a 'foo' field with value ($bar)" } [$a, $b] => { print $"Value is a list with two items: ($a) and ($b)" } _ => { print "Value is none of the above" } } ``` Like the recent changes to `if` to allow it to be used as an expression, `match` can also be used as an expression. This allows you to assign the result to a variable, eg) `let xyz = match ...` I've also included a short-hand pattern for matching records, as I think it might help when doing a lot of record patterns: `{$foo}` which is equivalent to `{foo: $foo}`. There are still missing components, so consider this the first step in full pattern matching support. Currently missing: * Patterns for strings * Or-patterns (like the `|` in Rust) * Patterns for tables (unclear how we want to match a table, so it'll need some design) * Patterns for binary values * And much more # User-Facing Changes [see above] # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2023-03-24 01:52:01 +00:00
impl FromValue for Spanned<MatchPattern> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::MatchPattern { val, span } => Ok(Spanned {
item: *val.clone(),
span: *span,
}),
v => Err(ShellError::CantConvert {
to_type: "Match pattern".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
impl FromValue for MatchPattern {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::MatchPattern { val, .. } => Ok(*val.clone()),
v => Err(ShellError::CantConvert {
to_type: "Match pattern".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}