From e3e1d81f48579dc65cf528ae6655022b047cb619 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 2 Jun 2019 06:26:04 +1200 Subject: [PATCH] Add to_toml export --- src/cli.rs | 1 + src/commands.rs | 1 + src/commands/to_toml.rs | 9 +++++++++ src/object/desc.rs | 19 ++++++++++++++++++- src/object/dict.rs | 27 +++++++++++++++++++++++++-- 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/commands/to_toml.rs diff --git a/src/cli.rs b/src/cli.rs index c5a4beac56..00a47b3944 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -60,6 +60,7 @@ pub async fn cli() -> Result<(), Box> { command("trim", trim::trim), command("to-array", to_array::to_array), command("to-json", to_json::to_json), + command("to-toml", to_toml::to_toml), Arc::new(Where), Arc::new(Config), command("sort-by", sort_by::sort_by), diff --git a/src/commands.rs b/src/commands.rs index fc3d915829..9aca425f7d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -19,6 +19,7 @@ crate mod split_row; crate mod take; crate mod to_array; crate mod to_json; +crate mod to_toml; crate mod trim; crate mod view; crate mod where_; diff --git a/src/commands/to_toml.rs b/src/commands/to_toml.rs new file mode 100644 index 0000000000..ac0fe05a6d --- /dev/null +++ b/src/commands/to_toml.rs @@ -0,0 +1,9 @@ +use crate::object::{Primitive, Value}; +use crate::prelude::*; + +pub fn to_toml(args: CommandArgs) -> Result { + let out = args.input; + Ok(out + .map(|a| ReturnValue::Value(Value::Primitive(Primitive::String(toml::to_string(&a).unwrap())))) + .boxed()) +} diff --git a/src/object/desc.rs b/src/object/desc.rs index 4768262386..80d1ac4dc4 100644 --- a/src/object/desc.rs +++ b/src/object/desc.rs @@ -1,6 +1,7 @@ use crate::object::types::Type; use derive_new::new; use serde_derive::{Deserialize, Serialize}; +use serde::{Serialize, Serializer}; #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)] pub enum DescriptorName { @@ -31,13 +32,29 @@ impl DescriptorName { } } -#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash, new)] +#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Hash, new)] pub struct DataDescriptor { crate name: DescriptorName, crate readonly: bool, crate ty: Type, } +impl Serialize for DataDescriptor { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self.name { + DescriptorName::String(ref s) => { + serializer.serialize_str(s) + } + DescriptorName::ValueOf => { + serializer.serialize_str("value") + } + } + } +} + impl From<&str> for DataDescriptor { fn from(input: &str) -> DataDescriptor { DataDescriptor { diff --git a/src/object/dict.rs b/src/object/dict.rs index 36216564f1..22167698c6 100644 --- a/src/object/dict.rs +++ b/src/object/dict.rs @@ -4,10 +4,11 @@ use crate::object::DataDescriptor; use crate::object::{Primitive, Value}; use derive_new::new; use indexmap::IndexMap; -use serde_derive::{Deserialize, Serialize}; +use serde_derive::Deserialize; +use serde::ser::{Serialize, Serializer, SerializeMap}; use std::cmp::{Ordering, PartialOrd}; -#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize, Clone, new)] +#[derive(Debug, Default, Eq, PartialEq, Deserialize, Clone, new)] pub struct Dictionary { entries: IndexMap, } @@ -19,6 +20,28 @@ impl PartialOrd for Dictionary { } } +impl Serialize for Dictionary { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut map = serializer.serialize_map(Some(self.entries.len()))?; + for (k, v) in self.entries.iter() { + match v { + Value::Object(_) => {}, + _ => map.serialize_entry(k, v)? + } + } + for (k, v) in self.entries.iter() { + match v { + Value::Object(_) => map.serialize_entry(k, v)?, + _ => {} + } + } + map.end() + } +} + impl From> for Dictionary { fn from(input: IndexMap) -> Dictionary { let mut out = IndexMap::default();