From 5d901a11e1fa5eb6c3e6ab64d1e7a3481b9eaac9 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 1 Jun 2019 19:05:57 +1200 Subject: [PATCH] Add from_toml (#54) --- src/cli.rs | 1 + src/commands.rs | 1 + src/commands/from_toml.rs | 34 ++++++++++++++++++++++++++++++++++ src/commands/split_row.rs | 2 +- src/parser/parser.lalrpop | 4 ++-- src/parser/parser.rs | 8 ++++---- 6 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/commands/from_toml.rs diff --git a/src/cli.rs b/src/cli.rs index 59a5b67394..c5a4beac56 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -50,6 +50,7 @@ pub async fn cli() -> Result<(), Box> { command("first", take::take), command("size", size::size), command("from-json", from_json::from_json), + command("from-toml", from_toml::from_toml), command("open", open::open), command("column", column::column), command("split-column", split_column::split_column), diff --git a/src/commands.rs b/src/commands.rs index 54484aed3a..fc3d915829 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5,6 +5,7 @@ crate mod column; crate mod command; crate mod config; crate mod from_json; +crate mod from_toml; crate mod ls; crate mod open; crate mod ps; diff --git a/src/commands/from_toml.rs b/src/commands/from_toml.rs new file mode 100644 index 0000000000..38c87409fa --- /dev/null +++ b/src/commands/from_toml.rs @@ -0,0 +1,34 @@ +use crate::object::{Primitive, Value, Dictionary, DataDescriptor}; +use crate::object::base::OF64; +use crate::prelude::*; + +fn convert_toml_value_to_nu_value(v: &toml::Value) -> Value { + match v { + toml::Value::Boolean(b) => Value::Primitive(Primitive::Boolean(*b)), + toml::Value::Integer(n) => Value::Primitive(Primitive::Int(*n)), + toml::Value::Float(n) => Value::Primitive(Primitive::Float(OF64::from(*n))), + toml::Value::String(s) => Value::Primitive(Primitive::String(s.clone())), + toml::Value::Array(a) => Value::List(a.iter().map(|x| convert_toml_value_to_nu_value(x)).collect()), + toml::Value::Datetime(dt) => Value::Primitive(Primitive::String(dt.to_string())), + toml::Value::Table(t) => { + let mut collected = Dictionary::default(); + for (k, v) in t.iter() { + collected.add(DataDescriptor::from(k.clone()), convert_toml_value_to_nu_value(v)); + } + Value::Object(collected) + } + } +} + +pub fn from_toml(args: CommandArgs) -> Result { + let out = args.input; + Ok(out + .map(|a| match a { + Value::Primitive(Primitive::String(s)) => { + let v: toml::Value = s.parse::().unwrap(); + ReturnValue::Value(convert_toml_value_to_nu_value(&v)) + } + _ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))), + }) + .boxed()) +} diff --git a/src/commands/split_row.rs b/src/commands/split_row.rs index b443e39f84..65d6cbe890 100644 --- a/src/commands/split_row.rs +++ b/src/commands/split_row.rs @@ -7,7 +7,7 @@ use log::debug; pub fn split_row(args: CommandArgs) -> Result { let input = args.input; - let args = args.args; + let args = args.positional; let stream = input .map(move |v| match v { diff --git a/src/parser/parser.lalrpop b/src/parser/parser.lalrpop index 16d1b5ab6f..2e58f37a7d 100644 --- a/src/parser/parser.lalrpop +++ b/src/parser/parser.lalrpop @@ -23,7 +23,7 @@ Leaf: Expression = { => Expression::Leaf(Leaf::String(<>)), => Expression::Leaf(Leaf::Int(<>)), => Expression::Leaf(Leaf::Int(<>)), - => Expression::VariableReference(<>), + => <>, } BinaryExpression: Expression = { @@ -65,7 +65,7 @@ Expr: Expression = { } -Variable: Expression = { +Var: Expression = { "$" <"variable"> => Variable::from_str(<>.as_slice()), } diff --git a/src/parser/parser.rs b/src/parser/parser.rs index a387a1528c..b3a87beb76 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.17.0" -// sha256: 327a2eaaded6615e365add5d44719ae0dd3217f5b0fc3ba130f052328c2bd439 +// sha256: b26b8a2cdaceecc744b5554b038a811430dd7af761c75685fc9e389596528 #![allow(unused)] use std::str::FromStr; use crate::parser::ast::*; @@ -2009,7 +2009,7 @@ mod __parse__Pipeline { _: ::std::marker::PhantomData<(&'input ())>, ) -> (usize, usize) { - // Leaf = Variable => ActionFn(9); + // Leaf = Var => ActionFn(9); let __sym0 = __pop_Variant6(__symbols); let __start = __sym0.0.clone(); let __end = __sym0.2.clone(); @@ -2375,7 +2375,7 @@ mod __parse__Pipeline { _: ::std::marker::PhantomData<(&'input ())>, ) -> (usize, usize) { - // Variable = "$", "variable" => ActionFn(25); + // Var = "$", "variable" => ActionFn(25); let __sym1 = __pop_Variant0(__symbols); let __sym0 = __pop_Variant0(__symbols); let __start = __sym0.0.clone(); @@ -2513,7 +2513,7 @@ fn __action9< (_, __0, _): (usize, Expression, usize), ) -> Expression { - Expression::VariableReference(__0) + __0 } fn __action10<