You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
embed-nu/src/into_expression.rs

68 lines
2.0 KiB
Rust

use nu_protocol::{
ast::{Expr, Expression, RecordItem},
Span, Value,
};
use crate::{IntoValue, NewEmpty};
pub trait IntoExpression {
fn into_expression(self) -> Expression;
}
pub trait ValueIntoExpression {
fn into_expression(self) -> Expression;
fn into_expr(self) -> Expr;
}
impl<V: IntoValue> IntoExpression for V {
#[inline]
fn into_expression(self) -> Expression {
self.into_value().into_expression()
}
}
impl ValueIntoExpression for Value {
fn into_expression(self) -> Expression {
let ty = self.get_type();
Expression {
expr: self.into_expr(),
span: Span::empty(),
ty,
custom_completion: None,
}
}
fn into_expr(self) -> Expr {
match self {
Value::Bool { val, .. } => Expr::Bool(val),
Value::Int { val, .. } => Expr::Int(val),
Value::Float { val, .. } => Expr::Float(val),
Value::Filesize { val, .. } => Expr::Int(val),
Value::Duration { val, .. } => Expr::Int(val),
Value::Date { val, .. } => Expr::DateTime(val),
Value::String { val, .. } => Expr::String(val),
Value::Record { val, .. } => {
let entries = val
.into_iter()
.map(|(col, val)| {
RecordItem::Pair(col.into_expression(), val.into_expression())
})
.collect();
Expr::Record(entries)
}
Value::List { vals, .. } => {
let vals = vals.into_iter().map(|v| v.into_expression()).collect();
Expr::List(vals)
}
Value::Block { val, .. } => Expr::Block(val),
Value::Nothing { .. } => Expr::Nothing,
Value::Error { error, .. } => Expr::String(error.to_string()),
Value::Binary { val, .. } => Expr::Binary(val),
Value::CellPath { val, .. } => Expr::CellPath(val),
_ => Expr::Nothing,
}
}
}