From 420b840cd62cdd840f9a10ed3caca50f12c532ab Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 28 May 2019 16:00:00 +1200 Subject: [PATCH] Add missing files --- src/commands/from_json.rs | 32 ++++++++++++++++++++++++++++++++ src/commands/open.rs | 20 ++++++++++++++++++++ src/commands/to_json.rs | 9 +++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/commands/from_json.rs create mode 100644 src/commands/open.rs create mode 100644 src/commands/to_json.rs diff --git a/src/commands/from_json.rs b/src/commands/from_json.rs new file mode 100644 index 0000000000..f64fc031a7 --- /dev/null +++ b/src/commands/from_json.rs @@ -0,0 +1,32 @@ +use crate::object::{Primitive, Value, Dictionary, DataDescriptor}; +use crate::prelude::*; + +fn convert_json_value_to_nu_value(v: &serde_json::Value) -> Value { + match v { + serde_json::Value::Null => Value::Primitive(Primitive::String("".to_string())), + serde_json::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)), + serde_json::Value::Number(n) => Value::Primitive(Primitive::Int(n.as_i64().unwrap())), + serde_json::Value::String(s) => Value::Primitive(Primitive::String(s.clone())), + serde_json::Value::Array(a) => Value::List(a.iter().map(|x| convert_json_value_to_nu_value(x)).collect()), + serde_json::Value::Object(o) => { + let mut collected = Dictionary::default(); + for (k, v) in o.iter() { + collected.add(DataDescriptor::from(k.clone()), convert_json_value_to_nu_value(v)); + } + Value::Object(collected) + } + } +} + +pub fn from_json(args: CommandArgs) -> Result { + 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(Value::Primitive(Primitive::String("".to_string()))), + }) + .boxed()) +} diff --git a/src/commands/open.rs b/src/commands/open.rs new file mode 100644 index 0000000000..ec2b4905d3 --- /dev/null +++ b/src/commands/open.rs @@ -0,0 +1,20 @@ +use crate::errors::ShellError; +use crate::object::{Primitive, Value}; +use crate::prelude::*; +use std::path::PathBuf; + +pub fn open(args: CommandArgs) -> Result { + let cwd = args.env.lock().unwrap().cwd().to_path_buf(); + let mut full_path = PathBuf::from(cwd); + match &args.args[0] { + Value::Primitive(Primitive::String(s)) => full_path.push(s), + _ => {} + } + + 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)))); + + Ok(stream.boxed()) +} diff --git a/src/commands/to_json.rs b/src/commands/to_json.rs new file mode 100644 index 0000000000..fa8aadd0a6 --- /dev/null +++ b/src/commands/to_json.rs @@ -0,0 +1,9 @@ +use crate::object::{Primitive, Value}; +use crate::prelude::*; + +pub fn to_json(args: CommandArgs) -> Result { + let out = args.input; + Ok(out + .map(|a| ReturnValue::Value(Value::Primitive(Primitive::String(serde_json::to_string(&a).unwrap())))) + .boxed()) +}