Add wrap and get and cell_path parsing

This commit is contained in:
JT
2021-10-02 15:59:11 +13:00
parent 3567bbbf32
commit 5843acec02
18 changed files with 290 additions and 81 deletions

View File

@ -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,
))
}
}
}