Skip to content

Commit

Permalink
Remove Value::MatchPattern (#11356)
Browse files Browse the repository at this point in the history
# Description
`Value::MatchPattern` implies that `MatchPattern`s are first-class
values. This PR removes this case, and commands must now instead use
`Expr::MatchPattern` to extract `MatchPattern`s just like how the
`match` command does using `Expr::MatchBlock`.

# User-Facing Changes
Breaking API change for `nu_protocol` crate.
  • Loading branch information
IanManske committed Dec 17, 2023
1 parent 2264682 commit c9c93f5
Show file tree
Hide file tree
Showing 14 changed files with 7 additions and 134 deletions.
1 change: 0 additions & 1 deletion crates/nu-cmd-lang/src/core_commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ fn describe_value(
| Value::Date { .. }
| Value::Range { .. }
| Value::String { .. }
| Value::MatchPattern { .. }
| Value::Nothing { .. } => Value::record(
record!(
"type" => Value::string(value.get_type().to_string(), head),
Expand Down
3 changes: 0 additions & 3 deletions crates/nu-cmd-lang/src/example_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ impl<'a> std::fmt::Debug for DebuggableValue<'a> {
let rec = val.collect().map_err(|_| std::fmt::Error)?;
write!(f, "LazyRecord({:?})", DebuggableValue(&rec))
}
Value::MatchPattern { val, .. } => {
write!(f, "MatchPattern({:?})", val)
}
}
}
}
3 changes: 1 addition & 2 deletions crates/nu-color-config/src/style_computer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ impl<'a> StyleComputer<'a> {
Value::Closure { .. }
| Value::CustomValue { .. }
| Value::Error { .. }
| Value::LazyRecord { .. }
| Value::MatchPattern { .. } => TextStyle::basic_left(),
| Value::LazyRecord { .. } => TextStyle::basic_left(),
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/src/database/commands/into_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ fn nu_value_to_string(value: Value, separator: &str) -> String {
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.to_string(),
Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/src/debug/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,5 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.to_string(),
Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
}
}
1 change: 0 additions & 1 deletion crates/nu-command/src/filters/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@ fn value_should_be_printed(
Err(_) => false,
},
Value::Binary { .. } => false,
Value::MatchPattern { .. } => false,
});
if invert {
match_found = !match_found;
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/formats/to/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {

Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Closure { .. }
| Value::Block { .. }
| Value::Range { .. }
| Value::MatchPattern { .. } => nu_json::Value::Null,
Value::Closure { .. } | Value::Block { .. } | Value::Range { .. } => nu_json::Value::Null,
Value::Binary { val, .. } => {
nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect())
}
Expand Down
6 changes: 0 additions & 6 deletions crates/nu-command/src/formats/to/nuon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,6 @@ pub fn value_to_string(
))
}
}
Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput {
msg: "match patterns are currently not nuon-compatible".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!(
"{}..{}{}",
Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/src/formats/to/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.to_string(),
Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/src/formats/to/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
.collect::<Result<Vec<toml::Value>, ShellError>>()?,
),
Value::CustomValue { .. } => toml::Value::String("<Custom Value>".to_string()),
Value::MatchPattern { .. } => toml::Value::String("<Match Pattern>".to_string()),
})
}

Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/src/formats/to/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
.collect::<Result<Vec<serde_yaml::Value>, ShellError>>()?,
),
Value::CustomValue { .. } => serde_yaml::Value::Null,
Value::MatchPattern { .. } => serde_yaml::Value::Null,
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/nu-protocol/src/eval_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ pub trait Eval {
Expr::GlobPattern(pattern) => {
Self::eval_glob_pattern(state, mut_state, pattern.clone(), expr.span)
}
Expr::MatchPattern(pattern) => Ok(Value::match_pattern(*pattern.clone(), expr.span)),
Expr::MatchBlock(_) // match blocks are handled by `match`
Expr::MatchPattern(_) // match patterns are handled directly by commands
| Expr::MatchBlock(_) // match blocks are handled by `match`
| Expr::VarDecl(_)
| Expr::ImportPattern(_)
| Expr::Signature(_)
Expand Down
31 changes: 1 addition & 30 deletions crates/nu-protocol/src/value/from_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use crate::ast::{CellPath, MatchPattern, PathMember};
use crate::ast::{CellPath, PathMember};
use crate::engine::{Block, Closure};
use crate::{Range, Record, ShellError, Spanned, Value};
use chrono::{DateTime, FixedOffset};
Expand Down Expand Up @@ -535,32 +535,3 @@ impl FromValue for Spanned<Closure> {
}
}
}

impl FromValue for Spanned<MatchPattern> {
fn from_value(v: Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::MatchPattern { val, .. } => Ok(Spanned { item: *val, 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),
v => Err(ShellError::CantConvert {
to_type: "Match pattern".into(),
from_type: v.get_type().to_string(),
span: v.span(),
help: None,
}),
}
}
}

0 comments on commit c9c93f5

Please sign in to comment.