Fix expression builder for maps

pull/3/head
trivernis 2 years ago
parent 895945fb78
commit b91f7c5520
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -10,11 +10,13 @@ pub enum Argument {
impl Argument { impl Argument {
/// Creates a new named argument. No value means passing the argument as a flag (like --verbose) /// Creates a new named argument. No value means passing the argument as a flag (like --verbose)
#[inline]
pub fn named<S: ToString, E: IntoExpression>(name: S, value: Option<E>) -> Self { pub fn named<S: ToString, E: IntoExpression>(name: S, value: Option<E>) -> Self {
Self::Named((name.to_string(), value.map(|v| v.into_expression()))) Self::Named((name.to_string(), value.map(|v| v.into_expression())))
} }
/// Creates a new positional argument /// Creates a new positional argument
#[inline]
pub fn positional<E: IntoExpression>(value: E) -> Self { pub fn positional<E: IntoExpression>(value: E) -> Self {
Self::Positional(value.into_expression()) Self::Positional(value.into_expression())
} }
@ -40,12 +42,14 @@ pub trait IntoArgument {
} }
impl<E: IntoExpression> IntoArgument for E { impl<E: IntoExpression> IntoArgument for E {
#[inline]
fn into_argument(self) -> Argument { fn into_argument(self) -> Argument {
Argument::positional(self) Argument::positional(self)
} }
} }
impl IntoArgument for Argument { impl IntoArgument for Argument {
#[inline]
fn into_argument(self) -> Argument { fn into_argument(self) -> Argument {
self self
} }

@ -47,9 +47,9 @@ impl ValueIntoExpression for Value {
} => { } => {
let mut entries = Vec::new(); let mut entries = Vec::new();
for i in 0..cols.len() { for _ in 0..cols.len() {
let col = cols.remove(i).into_expression(); let col = cols.remove(0).into_expression();
let val = vals.remove(i).into_expression(); let val = vals.remove(0).into_expression();
entries.push((col, val)); entries.push((col, val));
} }

@ -13,6 +13,7 @@ pub trait IntoValue {
} }
impl IntoValue for RawValue { impl IntoValue for RawValue {
#[inline]
fn into_value(self) -> Value { fn into_value(self) -> Value {
self.0 self.0
} }
@ -41,6 +42,7 @@ impl HashableIntoString for HashableValue {
} }
impl RustyIntoValue for Vec<Value> { impl RustyIntoValue for Vec<Value> {
#[inline]
fn into_value(self) -> Value { fn into_value(self) -> Value {
Value::List { Value::List {
vals: self, vals: self,
@ -190,6 +192,7 @@ impl RustyIntoValue for rusty_value::Float {
} }
impl<R: RustyValue> IntoValue for R { impl<R: RustyValue> IntoValue for R {
#[inline]
fn into_value(self) -> Value { fn into_value(self) -> Value {
self.into_rusty_value().into_value() self.into_rusty_value().into_value()
} }

@ -1,5 +1,6 @@
use embed_nu::{CommandGroupConfig, Context, NewEmpty}; use embed_nu::{CommandGroupConfig, Context, NewEmpty};
use nu_protocol::PipelineData; use nu_protocol::PipelineData;
use rusty_value::RustyValue;
#[test] #[test]
fn it_evals_strings() { fn it_evals_strings() {
@ -13,6 +14,12 @@ fn it_evals_strings() {
ctx.print_pipeline(pipeline).unwrap() ctx.print_pipeline(pipeline).unwrap()
} }
#[derive(RustyValue)]
struct TestArg {
foo: String,
bar: usize,
}
#[test] #[test]
fn it_executes_functions() { fn it_executes_functions() {
let mut ctx = get_context(); let mut ctx = get_context();
@ -31,7 +38,11 @@ fn it_executes_functions() {
ctx.call_fn("hello", [] as [String; 0]).unwrap(); ctx.call_fn("hello", [] as [String; 0]).unwrap();
assert!(ctx.has_fn("world") == false); assert!(ctx.has_fn("world") == false);
let pipeline = ctx.call_fn("echo", ["Hello from Rust"]).unwrap(); let test_arg = TestArg {
foo: String::from("Hello World"),
bar: 12,
};
let pipeline = ctx.call_fn("echo", [test_arg]).unwrap();
ctx.print_pipeline(pipeline).unwrap(); ctx.print_pipeline(pipeline).unwrap();
} }

Loading…
Cancel
Save