2021-10-25 06:31:39 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
2021-10-25 16:58:58 +00:00
|
|
|
engine::{EngineState, Stack},
|
2021-12-02 05:42:56 +00:00
|
|
|
FromValue, ShellError,
|
2021-10-25 06:31:39 +00:00
|
|
|
};
|
2021-10-01 21:53:13 +00:00
|
|
|
|
2021-12-02 05:42:56 +00:00
|
|
|
use crate::eval_expression;
|
2021-10-01 21:53:13 +00:00
|
|
|
|
|
|
|
pub trait CallExt {
|
|
|
|
fn get_flag<T: FromValue>(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-01 21:53:13 +00:00
|
|
|
name: &str,
|
|
|
|
) -> Result<Option<T>, ShellError>;
|
|
|
|
|
|
|
|
fn rest<T: FromValue>(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-01 21:53:13 +00:00
|
|
|
starting_pos: usize,
|
|
|
|
) -> Result<Vec<T>, ShellError>;
|
2021-10-02 02:59:11 +00:00
|
|
|
|
|
|
|
fn opt<T: FromValue>(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-02 02:59:11 +00:00
|
|
|
pos: usize,
|
|
|
|
) -> Result<Option<T>, ShellError>;
|
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
fn req<T: FromValue>(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
pos: usize,
|
|
|
|
) -> Result<T, ShellError>;
|
2022-12-22 14:36:13 +00:00
|
|
|
|
|
|
|
fn req_parser_info<T: FromValue>(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2023-04-05 16:56:48 +00:00
|
|
|
name: &str,
|
2022-12-22 14:36:13 +00:00
|
|
|
) -> Result<T, ShellError>;
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CallExt for Call {
|
|
|
|
fn get_flag<T: FromValue>(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-01 21:53:13 +00:00
|
|
|
name: &str,
|
|
|
|
) -> Result<Option<T>, ShellError> {
|
|
|
|
if let Some(expr) = self.get_flag_expr(name) {
|
2021-10-25 06:31:39 +00:00
|
|
|
let result = eval_expression(engine_state, stack, &expr)?;
|
2021-10-01 21:53:13 +00:00
|
|
|
FromValue::from_value(&result).map(Some)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rest<T: FromValue>(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-01 21:53:13 +00:00
|
|
|
starting_pos: usize,
|
|
|
|
) -> Result<Vec<T>, ShellError> {
|
|
|
|
let mut output = vec![];
|
|
|
|
|
2022-04-09 02:55:02 +00:00
|
|
|
for expr in self.positional_iter().skip(starting_pos) {
|
2021-10-25 06:31:39 +00:00
|
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
2021-10-01 21:53:13 +00:00
|
|
|
output.push(FromValue::from_value(&result)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(output)
|
|
|
|
}
|
2021-10-02 02:59:11 +00:00
|
|
|
|
|
|
|
fn opt<T: FromValue>(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-02 02:59:11 +00:00
|
|
|
pos: usize,
|
|
|
|
) -> Result<Option<T>, ShellError> {
|
2022-04-09 02:55:02 +00:00
|
|
|
if let Some(expr) = self.positional_nth(pos) {
|
|
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
2021-10-02 02:59:11 +00:00
|
|
|
FromValue::from_value(&result).map(Some)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
fn req<T: FromValue>(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
pos: usize,
|
|
|
|
) -> Result<T, ShellError> {
|
2022-04-09 02:55:02 +00:00
|
|
|
if let Some(expr) = self.positional_nth(pos) {
|
|
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
2021-10-02 02:59:11 +00:00
|
|
|
FromValue::from_value(&result)
|
2022-10-29 17:47:50 +00:00
|
|
|
} else if self.positional_len() == 0 {
|
2023-03-06 17:33:09 +00:00
|
|
|
Err(ShellError::AccessEmptyContent { span: self.head })
|
2021-10-02 02:59:11 +00:00
|
|
|
} else {
|
2023-03-06 17:33:09 +00:00
|
|
|
Err(ShellError::AccessBeyondEnd {
|
|
|
|
max_idx: self.positional_len() - 1,
|
|
|
|
span: self.head,
|
|
|
|
})
|
2021-10-02 02:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 14:36:13 +00:00
|
|
|
|
|
|
|
fn req_parser_info<T: FromValue>(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2023-04-05 16:56:48 +00:00
|
|
|
name: &str,
|
2022-12-22 14:36:13 +00:00
|
|
|
) -> Result<T, ShellError> {
|
2023-04-05 16:56:48 +00:00
|
|
|
if let Some(expr) = self.get_parser_info(name) {
|
2022-12-22 14:36:13 +00:00
|
|
|
let result = eval_expression(engine_state, stack, expr)?;
|
|
|
|
FromValue::from_value(&result)
|
|
|
|
} else if self.parser_info.is_empty() {
|
2023-03-06 17:33:09 +00:00
|
|
|
Err(ShellError::AccessEmptyContent { span: self.head })
|
2022-12-22 14:36:13 +00:00
|
|
|
} else {
|
2023-03-06 17:33:09 +00:00
|
|
|
Err(ShellError::AccessBeyondEnd {
|
|
|
|
max_idx: self.parser_info.len() - 1,
|
|
|
|
span: self.head,
|
|
|
|
})
|
2022-12-22 14:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 21:53:13 +00:00
|
|
|
}
|