mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Add wrap and get and cell_path parsing
This commit is contained in:
@ -14,6 +14,14 @@ pub trait CallExt {
|
||||
context: &EvaluationContext,
|
||||
starting_pos: usize,
|
||||
) -> Result<Vec<T>, ShellError>;
|
||||
|
||||
fn opt<T: FromValue>(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
pos: usize,
|
||||
) -> Result<Option<T>, ShellError>;
|
||||
|
||||
fn req<T: FromValue>(&self, context: &EvaluationContext, pos: usize) -> Result<T, ShellError>;
|
||||
}
|
||||
|
||||
impl CallExt for Call {
|
||||
@ -44,4 +52,29 @@ impl CallExt for Call {
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn opt<T: FromValue>(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
pos: usize,
|
||||
) -> Result<Option<T>, ShellError> {
|
||||
if let Some(expr) = self.nth(pos) {
|
||||
let result = eval_expression(context, &expr)?;
|
||||
FromValue::from_value(&result).map(Some)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn req<T: FromValue>(&self, context: &EvaluationContext, pos: usize) -> Result<T, ShellError> {
|
||||
if let Some(expr) = self.nth(pos) {
|
||||
let result = eval_expression(context, &expr)?;
|
||||
FromValue::from_value(&result)
|
||||
} else {
|
||||
Err(ShellError::AccessBeyondEnd(
|
||||
self.positional.len(),
|
||||
self.head,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,6 +156,10 @@ pub fn eval_expression(
|
||||
Expr::Var(var_id) => context
|
||||
.get_var(*var_id)
|
||||
.map_err(move |_| ShellError::VariableNotFoundAtRuntime(expr.span)),
|
||||
Expr::CellPath(cell_path) => Ok(Value::CellPath {
|
||||
val: cell_path.clone(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::FullCellPath(cell_path) => {
|
||||
let value = eval_expression(context, &cell_path.head)?;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
// use std::path::PathBuf;
|
||||
|
||||
// use nu_path::expand_path;
|
||||
use nu_protocol::ast::CellPath;
|
||||
use nu_protocol::ShellError;
|
||||
use nu_protocol::{Range, Spanned, Value};
|
||||
|
||||
@ -110,28 +111,25 @@ impl FromValue for ColumnPath {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromValue for bool {
|
||||
*/
|
||||
|
||||
impl FromValue for CellPath {
|
||||
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
||||
match v {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Boolean(b)),
|
||||
..
|
||||
} => Ok(*b),
|
||||
Value {
|
||||
value: UntaggedValue::Row(_),
|
||||
..
|
||||
} => {
|
||||
let mut shell_error = ShellError::type_error("boolean", v.spanned_type_name());
|
||||
shell_error.notes.push(
|
||||
"Note: you can access columns using dot. eg) $it.column or (ls).column".into(),
|
||||
);
|
||||
Err(shell_error)
|
||||
}
|
||||
v => Err(ShellError::type_error("boolean", v.spanned_type_name())),
|
||||
Value::CellPath { val, .. } => Ok(val.clone()),
|
||||
v => Err(ShellError::CantConvert("cell path".into(), v.span())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromValue for bool {
|
||||
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
||||
match v {
|
||||
Value::Bool { val, .. } => Ok(*val),
|
||||
v => Err(ShellError::CantConvert("bool".into(), v.span())),
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
impl FromValue for Spanned<bool> {
|
||||
fn from_value(v: &Value) -> Result<Self, ShellError> {
|
||||
|
Reference in New Issue
Block a user