mirror of
https://github.com/nushell/nushell
synced 2025-01-06 18:29:02 +00:00
15c22db8f4
# Description Changes `FromValue` to take owned `Value`s instead of borrowed `Value`s. This eliminates some unnecessary clones (e.g., in `call_ext.rs`). # User-Facing Changes Breaking API change for `nu_protocol`.
193 lines
5.2 KiB
Rust
193 lines
5.2 KiB
Rust
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
|
eval_const::eval_constant,
|
|
FromValue, ShellError,
|
|
};
|
|
|
|
use crate::eval_expression;
|
|
|
|
pub trait CallExt {
|
|
fn get_flag<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
name: &str,
|
|
) -> Result<Option<T>, ShellError>;
|
|
|
|
fn get_flag_const<T: FromValue>(
|
|
&self,
|
|
working_set: &StateWorkingSet,
|
|
name: &str,
|
|
) -> Result<Option<T>, ShellError>;
|
|
|
|
fn rest<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
starting_pos: usize,
|
|
) -> Result<Vec<T>, ShellError>;
|
|
|
|
fn rest_const<T: FromValue>(
|
|
&self,
|
|
working_set: &StateWorkingSet,
|
|
starting_pos: usize,
|
|
) -> Result<Vec<T>, ShellError>;
|
|
|
|
fn opt<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
pos: usize,
|
|
) -> Result<Option<T>, ShellError>;
|
|
|
|
fn req<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
pos: usize,
|
|
) -> Result<T, ShellError>;
|
|
|
|
fn req_const<T: FromValue>(
|
|
&self,
|
|
working_set: &StateWorkingSet,
|
|
pos: usize,
|
|
) -> Result<T, ShellError>;
|
|
|
|
fn req_parser_info<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
name: &str,
|
|
) -> Result<T, ShellError>;
|
|
}
|
|
|
|
impl CallExt for Call {
|
|
fn get_flag<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
name: &str,
|
|
) -> Result<Option<T>, ShellError> {
|
|
if let Some(expr) = self.get_flag_expr(name) {
|
|
let result = eval_expression(engine_state, stack, &expr)?;
|
|
FromValue::from_value(result).map(Some)
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
fn get_flag_const<T: FromValue>(
|
|
&self,
|
|
working_set: &StateWorkingSet,
|
|
name: &str,
|
|
) -> Result<Option<T>, ShellError> {
|
|
if let Some(expr) = self.get_flag_expr(name) {
|
|
let result = eval_constant(working_set, &expr)?;
|
|
FromValue::from_value(result).map(Some)
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
fn rest<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
starting_pos: usize,
|
|
) -> Result<Vec<T>, ShellError> {
|
|
let mut output = vec![];
|
|
|
|
for expr in self.positional_iter().skip(starting_pos) {
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
|
output.push(FromValue::from_value(result)?);
|
|
}
|
|
|
|
Ok(output)
|
|
}
|
|
|
|
fn rest_const<T: FromValue>(
|
|
&self,
|
|
working_set: &StateWorkingSet,
|
|
starting_pos: usize,
|
|
) -> Result<Vec<T>, ShellError> {
|
|
let mut output = vec![];
|
|
|
|
for expr in self.positional_iter().skip(starting_pos) {
|
|
let result = eval_constant(working_set, expr)?;
|
|
output.push(FromValue::from_value(result)?);
|
|
}
|
|
|
|
Ok(output)
|
|
}
|
|
|
|
fn opt<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
pos: usize,
|
|
) -> Result<Option<T>, ShellError> {
|
|
if let Some(expr) = self.positional_nth(pos) {
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
|
FromValue::from_value(result).map(Some)
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
fn req<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
pos: usize,
|
|
) -> Result<T, ShellError> {
|
|
if let Some(expr) = self.positional_nth(pos) {
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
|
FromValue::from_value(result)
|
|
} else if self.positional_len() == 0 {
|
|
Err(ShellError::AccessEmptyContent { span: self.head })
|
|
} else {
|
|
Err(ShellError::AccessBeyondEnd {
|
|
max_idx: self.positional_len() - 1,
|
|
span: self.head,
|
|
})
|
|
}
|
|
}
|
|
|
|
fn req_const<T: FromValue>(
|
|
&self,
|
|
working_set: &StateWorkingSet,
|
|
pos: usize,
|
|
) -> Result<T, ShellError> {
|
|
if let Some(expr) = self.positional_nth(pos) {
|
|
let result = eval_constant(working_set, expr)?;
|
|
FromValue::from_value(result)
|
|
} else if self.positional_len() == 0 {
|
|
Err(ShellError::AccessEmptyContent { span: self.head })
|
|
} else {
|
|
Err(ShellError::AccessBeyondEnd {
|
|
max_idx: self.positional_len() - 1,
|
|
span: self.head,
|
|
})
|
|
}
|
|
}
|
|
|
|
fn req_parser_info<T: FromValue>(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
name: &str,
|
|
) -> Result<T, ShellError> {
|
|
if let Some(expr) = self.get_parser_info(name) {
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
|
FromValue::from_value(result)
|
|
} else if self.parser_info.is_empty() {
|
|
Err(ShellError::AccessEmptyContent { span: self.head })
|
|
} else {
|
|
Err(ShellError::AccessBeyondEnd {
|
|
max_idx: self.parser_info.len() - 1,
|
|
span: self.head,
|
|
})
|
|
}
|
|
}
|
|
}
|