2021-09-02 18:21:37 +00:00
|
|
|
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
2021-10-25 16:58:58 +00:00
|
|
|
use nu_protocol::engine::{EngineState, Stack};
|
2021-10-25 04:01:02 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
IntoPipelineData, PipelineData, Range, ShellError, Span, Spanned, Type, Unit, Value,
|
|
|
|
};
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-10-13 17:53:27 +00:00
|
|
|
use crate::get_full_help;
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
match op {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Operator(operator),
|
|
|
|
..
|
|
|
|
} => Ok(operator.clone()),
|
2021-09-05 23:16:27 +00:00
|
|
|
Expression { span, expr, .. } => {
|
|
|
|
Err(ShellError::UnknownOperator(format!("{:?}", expr), *span))
|
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-10-25 04:01:02 +00:00
|
|
|
fn eval_call(
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-25 04:01:02 +00:00
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-10-25 06:31:39 +00:00
|
|
|
let decl = engine_state.get_decl(call.decl_id);
|
2021-10-13 17:53:27 +00:00
|
|
|
if call.named.iter().any(|(flag, _)| flag.item == "help") {
|
2021-10-25 06:31:39 +00:00
|
|
|
let full_help = get_full_help(&decl.signature(), &decl.examples(), engine_state);
|
2021-10-13 17:53:27 +00:00
|
|
|
Ok(Value::String {
|
|
|
|
val: full_help,
|
|
|
|
span: call.head,
|
2021-10-25 04:01:02 +00:00
|
|
|
}
|
|
|
|
.into_pipeline_data())
|
2021-10-13 17:53:27 +00:00
|
|
|
} else if let Some(block_id) = decl.get_block_id() {
|
2021-10-25 20:04:23 +00:00
|
|
|
let block = engine_state.get_block(block_id);
|
|
|
|
|
|
|
|
let mut stack = stack.collect_captures(&block.captures);
|
2021-08-27 02:30:10 +00:00
|
|
|
for (arg, param) in call.positional.iter().zip(
|
2021-09-02 18:21:37 +00:00
|
|
|
decl.signature()
|
2021-08-27 02:30:10 +00:00
|
|
|
.required_positional
|
|
|
|
.iter()
|
2021-09-02 18:21:37 +00:00
|
|
|
.chain(decl.signature().optional_positional.iter()),
|
2021-08-27 02:30:10 +00:00
|
|
|
) {
|
2021-10-25 06:31:39 +00:00
|
|
|
let result = eval_expression(engine_state, &mut stack, arg)?;
|
2021-07-23 21:19:30 +00:00
|
|
|
let var_id = param
|
|
|
|
.var_id
|
|
|
|
.expect("internal error: all custom parameters must have var_ids");
|
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
stack.add_var(var_id, result);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-09-07 03:37:02 +00:00
|
|
|
|
|
|
|
if let Some(rest_positional) = decl.signature().rest_positional {
|
|
|
|
let mut rest_items = vec![];
|
|
|
|
|
|
|
|
for arg in call.positional.iter().skip(
|
|
|
|
decl.signature().required_positional.len()
|
|
|
|
+ decl.signature().optional_positional.len(),
|
|
|
|
) {
|
2021-10-25 16:58:58 +00:00
|
|
|
let result = eval_expression(engine_state, &mut stack, arg)?;
|
2021-09-07 03:37:02 +00:00
|
|
|
rest_items.push(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = if let Some(rest_item) = rest_items.first() {
|
2021-10-11 18:45:31 +00:00
|
|
|
rest_item.span()?
|
2021-09-07 03:37:02 +00:00
|
|
|
} else {
|
|
|
|
Span::unknown()
|
|
|
|
};
|
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
stack.add_var(
|
2021-09-07 03:37:02 +00:00
|
|
|
rest_positional
|
|
|
|
.var_id
|
|
|
|
.expect("Internal error: rest positional parameter lacks var_id"),
|
|
|
|
Value::List {
|
2021-09-07 07:07:11 +00:00
|
|
|
vals: rest_items,
|
2021-09-07 03:37:02 +00:00
|
|
|
span,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-10-11 21:17:45 +00:00
|
|
|
|
|
|
|
for named in decl.signature().named {
|
2021-10-13 17:53:27 +00:00
|
|
|
if let Some(var_id) = named.var_id {
|
|
|
|
let mut found = false;
|
|
|
|
for call_named in &call.named {
|
|
|
|
if call_named.0.item == named.long {
|
|
|
|
if let Some(arg) = &call_named.1 {
|
2021-10-25 16:58:58 +00:00
|
|
|
let result = eval_expression(engine_state, &mut stack, arg)?;
|
2021-10-12 04:49:17 +00:00
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
stack.add_var(var_id, result);
|
2021-10-13 17:53:27 +00:00
|
|
|
} else {
|
2021-10-25 06:31:39 +00:00
|
|
|
stack.add_var(
|
2021-10-13 17:53:27 +00:00
|
|
|
var_id,
|
|
|
|
Value::Bool {
|
|
|
|
val: true,
|
|
|
|
span: call.head,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
found = true;
|
2021-10-11 21:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-12 04:49:17 +00:00
|
|
|
|
2021-10-13 17:53:27 +00:00
|
|
|
if !found && named.arg.is_none() {
|
2021-10-25 06:31:39 +00:00
|
|
|
stack.add_var(
|
2021-10-13 17:53:27 +00:00
|
|
|
var_id,
|
|
|
|
Value::Bool {
|
|
|
|
val: false,
|
|
|
|
span: call.head,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-10-12 04:49:17 +00:00
|
|
|
}
|
2021-10-11 21:17:45 +00:00
|
|
|
}
|
2021-10-25 06:31:39 +00:00
|
|
|
eval_block(engine_state, &mut stack, block, input)
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
2021-10-25 06:31:39 +00:00
|
|
|
decl.run(engine_state, stack, call, input)
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-09-19 21:48:33 +00:00
|
|
|
fn eval_external(
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-08 22:30:10 +00:00
|
|
|
name: &str,
|
|
|
|
name_span: &Span,
|
2021-10-08 21:51:47 +00:00
|
|
|
args: &[Expression],
|
2021-10-25 04:01:02 +00:00
|
|
|
input: PipelineData,
|
2021-09-23 16:42:03 +00:00
|
|
|
last_expression: bool,
|
2021-10-25 04:01:02 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-10-25 06:31:39 +00:00
|
|
|
let decl_id = engine_state
|
2021-09-19 21:48:33 +00:00
|
|
|
.find_decl("run_external".as_bytes())
|
2021-10-08 22:30:10 +00:00
|
|
|
.ok_or_else(|| ShellError::ExternalNotSupported(*name_span))?;
|
2021-09-19 21:48:33 +00:00
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
let command = engine_state.get_decl(decl_id);
|
2021-09-19 21:48:33 +00:00
|
|
|
|
|
|
|
let mut call = Call::new();
|
2021-10-08 21:51:47 +00:00
|
|
|
|
|
|
|
call.positional.push(Expression {
|
2021-10-08 22:30:10 +00:00
|
|
|
expr: Expr::String(name.trim_start_matches('^').to_string()),
|
2021-10-08 21:51:47 +00:00
|
|
|
span: *name_span,
|
|
|
|
ty: Type::String,
|
|
|
|
custom_completion: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
for arg in args {
|
2021-10-08 22:30:10 +00:00
|
|
|
call.positional.push(arg.clone())
|
2021-10-08 21:51:47 +00:00
|
|
|
}
|
2021-09-19 21:48:33 +00:00
|
|
|
|
2021-09-23 16:42:03 +00:00
|
|
|
if last_expression {
|
2021-10-11 21:17:45 +00:00
|
|
|
call.named.push((
|
|
|
|
Spanned {
|
|
|
|
item: "last_expression".into(),
|
|
|
|
span: Span::unknown(),
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
))
|
2021-09-23 16:42:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 06:31:39 +00:00
|
|
|
command.run(engine_state, stack, &call, input)
|
2021-09-19 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:58:15 +00:00
|
|
|
pub fn eval_expression(
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-09-02 22:58:15 +00:00
|
|
|
expr: &Expression,
|
|
|
|
) -> Result<Value, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
match &expr.expr {
|
2021-07-24 05:57:17 +00:00
|
|
|
Expr::Bool(b) => Ok(Value::Bool {
|
|
|
|
val: *b,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Int(i) => Ok(Value::Int {
|
|
|
|
val: *i,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 20:21:21 +00:00
|
|
|
Expr::Float(f) => Ok(Value::Float {
|
|
|
|
val: *f,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-10-25 06:31:39 +00:00
|
|
|
Expr::ValueWithUnit(e, unit) => match eval_expression(engine_state, stack, e)? {
|
2021-10-05 02:27:39 +00:00
|
|
|
Value::Int { val, .. } => Ok(compute(val, unit.item, unit.span)),
|
|
|
|
_ => Err(ShellError::CantConvert("unit value".into(), e.span)),
|
|
|
|
},
|
2021-09-11 11:13:04 +00:00
|
|
|
Expr::Range(from, next, to, operator) => {
|
2021-09-04 21:52:57 +00:00
|
|
|
let from = if let Some(f) = from {
|
2021-10-25 06:31:39 +00:00
|
|
|
eval_expression(engine_state, stack, f)?
|
2021-09-04 21:52:57 +00:00
|
|
|
} else {
|
2021-09-11 11:13:04 +00:00
|
|
|
Value::Nothing {
|
2021-09-04 21:52:57 +00:00
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-11 11:13:04 +00:00
|
|
|
let next = if let Some(s) = next {
|
2021-10-25 06:31:39 +00:00
|
|
|
eval_expression(engine_state, stack, s)?
|
2021-09-04 21:52:57 +00:00
|
|
|
} else {
|
2021-09-11 11:13:04 +00:00
|
|
|
Value::Nothing {
|
2021-09-04 21:52:57 +00:00
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-11 11:13:04 +00:00
|
|
|
let to = if let Some(t) = to {
|
2021-10-25 06:31:39 +00:00
|
|
|
eval_expression(engine_state, stack, t)?
|
2021-09-11 11:13:04 +00:00
|
|
|
} else {
|
|
|
|
Value::Nothing {
|
|
|
|
span: Span::unknown(),
|
2021-09-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Value::Range {
|
2021-09-11 11:13:04 +00:00
|
|
|
val: Box::new(Range::new(expr.span, from, next, to, operator)?),
|
2021-09-04 21:52:57 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-10-25 06:31:39 +00:00
|
|
|
Expr::Var(var_id) => stack
|
2021-07-30 21:57:22 +00:00
|
|
|
.get_var(*var_id)
|
2021-09-06 02:20:02 +00:00
|
|
|
.map_err(move |_| ShellError::VariableNotFoundAtRuntime(expr.span)),
|
2021-10-25 20:04:23 +00:00
|
|
|
Expr::VarDecl(_) => Ok(Value::Nothing { span: expr.span }),
|
2021-10-02 02:59:11 +00:00
|
|
|
Expr::CellPath(cell_path) => Ok(Value::CellPath {
|
|
|
|
val: cell_path.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-09-26 18:39:19 +00:00
|
|
|
Expr::FullCellPath(cell_path) => {
|
2021-10-25 06:31:39 +00:00
|
|
|
let value = eval_expression(engine_state, stack, &cell_path.head)?;
|
2021-09-06 22:02:24 +00:00
|
|
|
|
2021-09-26 18:39:19 +00:00
|
|
|
value.follow_cell_path(&cell_path.tail)
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
2021-10-25 06:31:39 +00:00
|
|
|
Expr::RowCondition(_, expr) => eval_expression(engine_state, stack, expr),
|
2021-10-25 04:01:02 +00:00
|
|
|
Expr::Call(call) => {
|
|
|
|
// FIXME: protect this collect with ctrl-c
|
|
|
|
Ok(Value::List {
|
2021-10-25 06:31:39 +00:00
|
|
|
vals: eval_call(engine_state, stack, call, PipelineData::new())?.collect(),
|
2021-10-25 04:01:02 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-10-08 22:30:10 +00:00
|
|
|
Expr::ExternalCall(name, span, args) => {
|
2021-10-25 04:01:02 +00:00
|
|
|
// FIXME: protect this collect with ctrl-c
|
|
|
|
Ok(Value::List {
|
2021-10-25 06:31:39 +00:00
|
|
|
vals: eval_external(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
name,
|
|
|
|
span,
|
|
|
|
args,
|
|
|
|
PipelineData::new(),
|
|
|
|
true,
|
|
|
|
)?
|
|
|
|
.collect(),
|
2021-10-25 04:01:02 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-09-23 16:42:03 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
2021-08-10 06:31:34 +00:00
|
|
|
let op_span = op.span;
|
2021-10-25 06:31:39 +00:00
|
|
|
let lhs = eval_expression(engine_state, stack, lhs)?;
|
2021-08-15 22:33:34 +00:00
|
|
|
let op = eval_operator(op)?;
|
2021-10-25 06:31:39 +00:00
|
|
|
let rhs = eval_expression(engine_state, stack, rhs)?;
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
match op {
|
2021-08-10 06:31:34 +00:00
|
|
|
Operator::Plus => lhs.add(op_span, &rhs),
|
2021-08-25 19:29:36 +00:00
|
|
|
Operator::Minus => lhs.sub(op_span, &rhs),
|
|
|
|
Operator::Multiply => lhs.mul(op_span, &rhs),
|
|
|
|
Operator::Divide => lhs.div(op_span, &rhs),
|
|
|
|
Operator::LessThan => lhs.lt(op_span, &rhs),
|
|
|
|
Operator::LessThanOrEqual => lhs.lte(op_span, &rhs),
|
|
|
|
Operator::GreaterThan => lhs.gt(op_span, &rhs),
|
|
|
|
Operator::GreaterThanOrEqual => lhs.gte(op_span, &rhs),
|
|
|
|
Operator::Equal => lhs.eq(op_span, &rhs),
|
|
|
|
Operator::NotEqual => lhs.ne(op_span, &rhs),
|
2021-10-08 22:53:28 +00:00
|
|
|
Operator::In => lhs.r#in(op_span, &rhs),
|
2021-10-09 15:58:58 +00:00
|
|
|
Operator::NotIn => lhs.not_in(op_span, &rhs),
|
2021-10-11 20:35:12 +00:00
|
|
|
Operator::Contains => lhs.contains(op_span, &rhs),
|
|
|
|
Operator::NotContains => lhs.not_contains(op_span, &rhs),
|
|
|
|
Operator::Modulo => lhs.modulo(op_span, &rhs),
|
|
|
|
Operator::And => lhs.and(op_span, &rhs),
|
|
|
|
Operator::Or => lhs.or(op_span, &rhs),
|
|
|
|
Operator::Pow => lhs.pow(op_span, &rhs),
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Subexpression(block_id) => {
|
2021-10-25 06:31:39 +00:00
|
|
|
let block = engine_state.get_block(*block_id);
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-10-25 20:04:23 +00:00
|
|
|
let mut stack = stack.collect_captures(&block.captures);
|
2021-10-25 04:01:02 +00:00
|
|
|
|
|
|
|
// FIXME: protect this collect with ctrl-c
|
|
|
|
Ok(Value::List {
|
2021-10-25 06:31:39 +00:00
|
|
|
vals: eval_block(engine_state, &mut stack, block, PipelineData::new())?.collect(),
|
2021-10-25 04:01:02 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Block(block_id) => Ok(Value::Block {
|
|
|
|
val: *block_id,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::List(x) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in x {
|
2021-10-25 06:31:39 +00:00
|
|
|
output.push(eval_expression(engine_state, stack, expr)?);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::List {
|
2021-09-07 07:07:11 +00:00
|
|
|
vals: output,
|
2021-08-08 21:02:47 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-28 19:17:30 +00:00
|
|
|
Expr::Table(headers, vals) => {
|
|
|
|
let mut output_headers = vec![];
|
|
|
|
for expr in headers {
|
2021-10-25 06:31:39 +00:00
|
|
|
output_headers.push(eval_expression(engine_state, stack, expr)?.as_string()?);
|
2021-08-28 19:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut output_rows = vec![];
|
|
|
|
for val in vals {
|
|
|
|
let mut row = vec![];
|
|
|
|
for expr in val {
|
2021-10-25 06:31:39 +00:00
|
|
|
row.push(eval_expression(engine_state, stack, expr)?);
|
2021-08-28 19:17:30 +00:00
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
output_rows.push(Value::Record {
|
|
|
|
cols: output_headers.clone(),
|
|
|
|
vals: row,
|
|
|
|
span: expr.span,
|
|
|
|
});
|
2021-08-28 19:17:30 +00:00
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
Ok(Value::List {
|
|
|
|
vals: output_rows,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-10-25 06:31:39 +00:00
|
|
|
Expr::Keyword(_, _, expr) => eval_expression(engine_state, stack, expr),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::String(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-10-04 19:21:31 +00:00
|
|
|
Expr::Filepath(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
|
|
|
Expr::GlobPattern(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
|
|
|
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
pub fn eval_block(
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-09-03 02:15:01 +00:00
|
|
|
block: &Block,
|
2021-10-25 04:01:02 +00:00
|
|
|
mut input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-09-23 16:42:03 +00:00
|
|
|
for stmt in block.stmts.iter() {
|
2021-09-03 02:15:01 +00:00
|
|
|
if let Statement::Pipeline(pipeline) = stmt {
|
2021-09-23 16:42:03 +00:00
|
|
|
for (i, elem) in pipeline.expressions.iter().enumerate() {
|
2021-09-03 02:15:01 +00:00
|
|
|
match elem {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
..
|
|
|
|
} => {
|
2021-10-25 06:31:39 +00:00
|
|
|
input = eval_call(engine_state, stack, call, input)?;
|
2021-09-03 02:15:01 +00:00
|
|
|
}
|
2021-09-19 21:48:33 +00:00
|
|
|
Expression {
|
2021-10-08 22:30:10 +00:00
|
|
|
expr: Expr::ExternalCall(name, name_span, args),
|
2021-09-19 21:48:33 +00:00
|
|
|
..
|
|
|
|
} => {
|
2021-09-23 16:42:03 +00:00
|
|
|
input = eval_external(
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state,
|
|
|
|
stack,
|
2021-09-23 16:42:03 +00:00
|
|
|
name,
|
2021-10-08 22:30:10 +00:00
|
|
|
name_span,
|
2021-09-23 16:42:03 +00:00
|
|
|
args,
|
|
|
|
input,
|
|
|
|
i == pipeline.expressions.len() - 1,
|
|
|
|
)?;
|
2021-09-19 21:48:33 +00:00
|
|
|
}
|
2021-09-03 02:15:01 +00:00
|
|
|
|
|
|
|
elem => {
|
2021-10-25 06:31:39 +00:00
|
|
|
input = eval_expression(engine_state, stack, elem)?.into_pipeline_data();
|
2021-09-03 02:15:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
Ok(input)
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-10-05 02:27:39 +00:00
|
|
|
|
|
|
|
pub fn compute(size: i64, unit: Unit, span: Span) -> Value {
|
|
|
|
match unit {
|
|
|
|
Unit::Byte => Value::Filesize { val: size, span },
|
|
|
|
Unit::Kilobyte => Value::Filesize {
|
|
|
|
val: size * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Megabyte => Value::Filesize {
|
|
|
|
val: size * 1000 * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Gigabyte => Value::Filesize {
|
|
|
|
val: size * 1000 * 1000 * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Terabyte => Value::Filesize {
|
|
|
|
val: size * 1000 * 1000 * 1000 * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Petabyte => Value::Filesize {
|
|
|
|
val: size * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
|
|
|
|
Unit::Kibibyte => Value::Filesize {
|
|
|
|
val: size * 1024,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Mebibyte => Value::Filesize {
|
|
|
|
val: size * 1024 * 1024,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Gibibyte => Value::Filesize {
|
|
|
|
val: size * 1024 * 1024 * 1024,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Tebibyte => Value::Filesize {
|
|
|
|
val: size * 1024 * 1024 * 1024 * 1024,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Pebibyte => Value::Filesize {
|
|
|
|
val: size * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
|
|
|
|
Unit::Nanosecond => Value::Duration { val: size, span },
|
|
|
|
Unit::Microsecond => Value::Duration {
|
|
|
|
val: size * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Millisecond => Value::Duration {
|
|
|
|
val: size * 1000 * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Second => Value::Duration {
|
|
|
|
val: size * 1000 * 1000 * 1000,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Minute => Value::Duration {
|
|
|
|
val: size * 1000 * 1000 * 1000 * 60,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Hour => Value::Duration {
|
|
|
|
val: size * 1000 * 1000 * 1000 * 60 * 60,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Day => Value::Duration {
|
|
|
|
val: size * 1000 * 1000 * 1000 * 60 * 60 * 24,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Unit::Week => Value::Duration {
|
|
|
|
val: size * 1000 * 1000 * 1000 * 60 * 60 * 24 * 7,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|