forked from extern/nushell
Add support for magic open
This commit is contained in:
parent
f5c7bed77a
commit
a62de9356c
@ -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> {
|
pub fn from_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let out = args.input;
|
let out = args.input;
|
||||||
Ok(out
|
Ok(out
|
||||||
.map(|a| match a {
|
.map(|a| match a {
|
||||||
Value::Primitive(Primitive::String(s)) => {
|
Value::Primitive(Primitive::String(s)) => {
|
||||||
let v: serde_json::Value = serde_json::from_str(&s).unwrap();
|
ReturnValue::Value(from_json_string_to_value(s))
|
||||||
ReturnValue::Value(convert_json_value_to_nu_value(&v))
|
|
||||||
}
|
}
|
||||||
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
|
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
|
||||||
})
|
})
|
||||||
|
@ -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> {
|
pub fn from_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let out = args.input;
|
let out = args.input;
|
||||||
Ok(out
|
Ok(out
|
||||||
.map(|a| match a {
|
.map(|a| match a {
|
||||||
Value::Primitive(Primitive::String(s)) => {
|
Value::Primitive(Primitive::String(s)) => {
|
||||||
let v: toml::Value = s.parse::<toml::Value>().unwrap();
|
ReturnValue::Value(from_toml_string_to_value(s))
|
||||||
ReturnValue::Value(convert_toml_value_to_nu_value(&v))
|
|
||||||
}
|
}
|
||||||
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
|
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
|
||||||
})
|
})
|
||||||
|
@ -14,9 +14,25 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
let contents = std::fs::read_to_string(&full_path).unwrap();
|
let contents = std::fs::read_to_string(&full_path).unwrap();
|
||||||
|
|
||||||
let mut stream = VecDeque::new();
|
let mut stream = VecDeque::new();
|
||||||
|
|
||||||
|
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(
|
stream.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
|
||||||
contents,
|
contents,
|
||||||
))));
|
))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(stream.boxed())
|
Ok(stream.boxed())
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::parser::ast;
|
use crate::parser::ast;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use crate::object::Primitive;
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
|
|
||||||
#[derive(new)]
|
#[derive(new)]
|
||||||
@ -20,10 +21,7 @@ crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, S
|
|||||||
match expr {
|
match expr {
|
||||||
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
||||||
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
||||||
Expression::Flag(f) => Err(ShellError::string(format!(
|
Expression::Flag(f) => Ok(Value::Primitive(Primitive::String(f.print()))),
|
||||||
"can't evaluate the flag {}",
|
|
||||||
f.print()
|
|
||||||
))),
|
|
||||||
Expression::Block(b) => evaluate_block(&b, scope),
|
Expression::Block(b) => evaluate_block(&b, scope),
|
||||||
Expression::Path(p) => evaluate_path(&p, scope),
|
Expression::Path(p) => evaluate_path(&p, scope),
|
||||||
Expression::Binary(b) => evaluate_binary(b, scope),
|
Expression::Binary(b) => evaluate_binary(b, scope),
|
||||||
|
Loading…
Reference in New Issue
Block a user