Add from_toml (#54)

This commit is contained in:
Jonathan Turner 2019-06-01 19:05:57 +12:00 committed by GitHub
parent 656d551b7f
commit 5d901a11e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 7 deletions

View File

@ -50,6 +50,7 @@ pub async fn cli() -> Result<(), Box<Error>> {
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),

View File

@ -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;

34
src/commands/from_toml.rs Normal file
View File

@ -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<OutputStream, ShellError> {
let out = args.input;
Ok(out
.map(|a| match a {
Value::Primitive(Primitive::String(s)) => {
let v: toml::Value = s.parse::<toml::Value>().unwrap();
ReturnValue::Value(convert_toml_value_to_nu_value(&v))
}
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
})
.boxed())
}

View File

@ -7,7 +7,7 @@ use log::debug;
pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
let args = args.args;
let args = args.positional;
let stream = input
.map(move |v| match v {

View File

@ -23,7 +23,7 @@ Leaf: Expression = {
<String> => Expression::Leaf(Leaf::String(<>)),
<Int> => Expression::Leaf(Leaf::Int(<>)),
<UnitsNum> => Expression::Leaf(Leaf::Int(<>)),
<Variable> => Expression::VariableReference(<>),
<Var> => <>,
}
BinaryExpression: Expression = {
@ -65,7 +65,7 @@ Expr: Expression = {
<PathHead>
}
Variable: Expression = {
Var: Expression = {
"$" <"variable"> => Variable::from_str(<>.as_slice()),
}

View File

@ -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<