From b91f7c5520d1a313e7a77e025563f681c4b55fa7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 3 Oct 2022 20:11:49 +0200 Subject: [PATCH] Fix expression builder for maps --- src/argument.rs | 4 ++++ src/into_expression.rs | 6 +++--- src/into_value.rs | 3 +++ tests/test_eval.rs | 13 ++++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/argument.rs b/src/argument.rs index 611c5b6..b9aadb3 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -10,11 +10,13 @@ pub enum Argument { impl Argument { /// Creates a new named argument. No value means passing the argument as a flag (like --verbose) + #[inline] pub fn named(name: S, value: Option) -> Self { Self::Named((name.to_string(), value.map(|v| v.into_expression()))) } /// Creates a new positional argument + #[inline] pub fn positional(value: E) -> Self { Self::Positional(value.into_expression()) } @@ -40,12 +42,14 @@ pub trait IntoArgument { } impl IntoArgument for E { + #[inline] fn into_argument(self) -> Argument { Argument::positional(self) } } impl IntoArgument for Argument { + #[inline] fn into_argument(self) -> Argument { self } diff --git a/src/into_expression.rs b/src/into_expression.rs index 3ce342f..804ef10 100644 --- a/src/into_expression.rs +++ b/src/into_expression.rs @@ -47,9 +47,9 @@ impl ValueIntoExpression for Value { } => { let mut entries = Vec::new(); - for i in 0..cols.len() { - let col = cols.remove(i).into_expression(); - let val = vals.remove(i).into_expression(); + for _ in 0..cols.len() { + let col = cols.remove(0).into_expression(); + let val = vals.remove(0).into_expression(); entries.push((col, val)); } diff --git a/src/into_value.rs b/src/into_value.rs index dd6d94d..6c94420 100644 --- a/src/into_value.rs +++ b/src/into_value.rs @@ -13,6 +13,7 @@ pub trait IntoValue { } impl IntoValue for RawValue { + #[inline] fn into_value(self) -> Value { self.0 } @@ -41,6 +42,7 @@ impl HashableIntoString for HashableValue { } impl RustyIntoValue for Vec { + #[inline] fn into_value(self) -> Value { Value::List { vals: self, @@ -190,6 +192,7 @@ impl RustyIntoValue for rusty_value::Float { } impl IntoValue for R { + #[inline] fn into_value(self) -> Value { self.into_rusty_value().into_value() } diff --git a/tests/test_eval.rs b/tests/test_eval.rs index f7ed505..b4eef14 100644 --- a/tests/test_eval.rs +++ b/tests/test_eval.rs @@ -1,5 +1,6 @@ use embed_nu::{CommandGroupConfig, Context, NewEmpty}; use nu_protocol::PipelineData; +use rusty_value::RustyValue; #[test] fn it_evals_strings() { @@ -13,6 +14,12 @@ fn it_evals_strings() { ctx.print_pipeline(pipeline).unwrap() } +#[derive(RustyValue)] +struct TestArg { + foo: String, + bar: usize, +} + #[test] fn it_executes_functions() { let mut ctx = get_context(); @@ -31,7 +38,11 @@ fn it_executes_functions() { ctx.call_fn("hello", [] as [String; 0]).unwrap(); 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(); }