Merge pull request #57 from jonathandturner/magic_open

Add support for magic open
This commit is contained in:
Jonathan Turner 2019-06-02 07:22:30 +12:00 committed by GitHub
commit 37e17bbeb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 11 deletions

View File

@ -18,13 +18,17 @@ fn convert_json_value_to_nu_value(v: &serde_json::Value) -> Value {
}
}
pub fn from_json_string_to_value(s: String) -> Value {
let v: serde_json::Value = serde_json::from_str(&s).unwrap();
convert_json_value_to_nu_value(&v)
}
pub fn from_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
let out = args.input;
Ok(out
.map(|a| match a {
Value::Primitive(Primitive::String(s)) => {
let v: serde_json::Value = serde_json::from_str(&s).unwrap();
ReturnValue::Value(convert_json_value_to_nu_value(&v))
ReturnValue::Value(from_json_string_to_value(s))
}
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
})

View File

@ -20,13 +20,17 @@ fn convert_toml_value_to_nu_value(v: &toml::Value) -> Value {
}
}
pub fn from_toml_string_to_value(s: String) -> Value {
let v: toml::Value = s.parse::<toml::Value>().unwrap();
convert_toml_value_to_nu_value(&v)
}
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(from_toml_string_to_value(s))
}
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
})

View File

@ -14,9 +14,25 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
let contents = std::fs::read_to_string(&full_path).unwrap();
let mut stream = VecDeque::new();
stream.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
contents,
))));
let open_raw = match args.positional.get(1) {
Some(Value::Primitive(Primitive::String(s))) if s == "--raw" => true,
_ => false,
};
match full_path.extension() {
Some(x) if x == "toml" && !open_raw => {
stream.push_back(ReturnValue::Value(crate::commands::from_toml::from_toml_string_to_value(contents)));
}
Some(x) if x == "json" && !open_raw => {
stream.push_back(ReturnValue::Value(crate::commands::from_json::from_json_string_to_value(contents)));
}
_ => {
stream.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
contents,
))));
}
}
Ok(stream.boxed())
}

View File

@ -1,5 +1,6 @@
use crate::parser::ast;
use crate::prelude::*;
use crate::object::Primitive;
use derive_new::new;
#[derive(new)]
@ -20,10 +21,7 @@ crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, S
match expr {
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
Expression::Flag(f) => Err(ShellError::string(format!(
"can't evaluate the flag {}",
f.print()
))),
Expression::Flag(f) => Ok(Value::Primitive(Primitive::String(f.print()))),
Expression::Block(b) => evaluate_block(&b, scope),
Expression::Path(p) => evaluate_path(&p, scope),
Expression::Binary(b) => evaluate_binary(b, scope),