nushell/crates/nu-engine/src/call_ext.rs
Fernando Herrera 56307553ae
Plugin with evaluated call (#393)
* plugin trait

* impl of trait

* record and absolute path

* plugin example crate

* clippy error

* correcting cargo

* evaluated call for plugin
2021-12-02 05:42:56 +00:00

101 lines
2.5 KiB
Rust

use nu_protocol::{
ast::Call,
engine::{EngineState, Stack},
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 rest<T: FromValue>(
&self,
engine_state: &EngineState,
stack: &mut Stack,
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>;
}
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 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 opt<T: FromValue>(
&self,
engine_state: &EngineState,
stack: &mut Stack,
pos: usize,
) -> Result<Option<T>, ShellError> {
if let Some(expr) = self.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.nth(pos) {
let result = eval_expression(engine_state, stack, &expr)?;
FromValue::from_value(&result)
} else {
Err(ShellError::AccessBeyondEnd(
self.positional.len(),
self.head,
))
}
}
}