Add to_toml export

This commit is contained in:
Jonathan Turner 2019-06-02 06:26:04 +12:00
parent f8bb0c99ec
commit e3e1d81f48
5 changed files with 54 additions and 3 deletions

View file

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

View file

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

9
src/commands/to_toml.rs Normal file
View file

@ -0,0 +1,9 @@
use crate::object::{Primitive, Value};
use crate::prelude::*;
pub fn to_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
let out = args.input;
Ok(out
.map(|a| ReturnValue::Value(Value::Primitive(Primitive::String(toml::to_string(&a).unwrap()))))
.boxed())
}

View file

@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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 {

View file

@ -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<DataDescriptor, Value>,
}
@ -19,6 +20,28 @@ impl PartialOrd for Dictionary {
}
}
impl Serialize for Dictionary {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<IndexMap<String, Value>> for Dictionary {
fn from(input: IndexMap<String, Value>) -> Dictionary {
let mut out = IndexMap::default();