1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2018 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std;
use std::error;
use std::error::Error;
use std::io::Write;
use std::rc::Rc;

use simple_error::SimpleError;
use toml;

use crate::build::Val;
use crate::convert::traits::{ConvertResult, Converter, ImportResult, Importer};

pub struct TomlConverter {}

type Result = std::result::Result<toml::Value, Box<dyn error::Error>>;

impl TomlConverter {
    pub fn new() -> Self {
        TomlConverter {}
    }

    fn convert_list(&self, items: &Vec<Rc<Val>>) -> Result {
        let mut v = Vec::new();
        for val in items.iter() {
            v.push(self.convert_value(val)?);
        }
        Ok(toml::Value::Array(v))
    }

    fn convert_tuple(&self, items: &Vec<(String, Rc<Val>)>) -> Result {
        let mut mp = toml::value::Table::new();
        for &(ref k, ref v) in items.iter() {
            mp.entry(k.clone()).or_insert(self.convert_value(v)?);
        }
        Ok(toml::Value::Table(mp))
    }

    fn convert_env(&self, items: &Vec<(String, String)>) -> Result {
        let mut mp = toml::value::Table::new();
        for &(ref k, ref v) in items.iter() {
            mp.entry(k.clone())
                .or_insert(toml::Value::String(v.clone()));
        }
        Ok(toml::Value::Table(mp))
    }

    fn convert_value(&self, v: &Val) -> Result {
        let toml_val = match v {
            &Val::Boolean(b) => toml::Value::Boolean(b),
            &Val::Empty => {
                let err = SimpleError::new("Nulls are not allowed in Toml Conversions!");
                return Err(Box::new(err));
            }
            &Val::Float(f) => toml::Value::Float(f),
            &Val::Int(i) => toml::Value::Integer(i),
            &Val::Str(ref s) => toml::Value::String(s.clone()),
            &Val::Env(ref fs) => self.convert_env(fs)?,
            &Val::List(ref l) => self.convert_list(l)?,
            &Val::Tuple(ref t) => self.convert_tuple(t)?,
        };
        Ok(toml_val)
    }

    fn convert_toml_val(&self, v: &toml::Value) -> std::result::Result<Val, Box<dyn Error>> {
        Ok(match v {
            toml::Value::String(s) => Val::Str(s.clone()),
            toml::Value::Integer(i) => Val::Int(*i),
            toml::Value::Float(f) => Val::Float(*f),
            toml::Value::Boolean(b) => Val::Boolean(*b),
            toml::Value::Array(l) => {
                let mut vs = Vec::with_capacity(l.len());
                for aval in l {
                    vs.push(Rc::new(self.convert_toml_val(aval)?));
                }
                Val::List(vs)
            }
            toml::Value::Table(m) => {
                let mut fs = Vec::with_capacity(m.len());
                for (key, value) in m {
                    fs.push((key.to_string(), Rc::new(self.convert_toml_val(value)?)));
                }
                Val::Tuple(fs)
            }
            toml::Value::Datetime(d) => Val::Str(format!("{}", d)),
        })
    }

    fn write(&self, v: &Val, w: &mut dyn Write) -> ConvertResult {
        let toml_val = self.convert_value(v)?;
        let toml_bytes = toml::ser::to_string_pretty(&toml_val)?;
        write!(w, "{}", toml_bytes)?;
        Ok(())
    }
}

impl Converter for TomlConverter {
    fn convert(&self, v: Rc<Val>, mut w: &mut dyn Write) -> ConvertResult {
        self.write(&v, &mut w)
    }

    fn file_ext(&self) -> String {
        String::from("toml")
    }

    fn description(&self) -> String {
        "Convert ucg Vals into valid ucg.".to_string()
    }

    #[allow(unused_must_use)]
    fn help(&self) -> String {
        include_str!("toml_help.txt").to_string()
    }
}

impl Importer for TomlConverter {
    fn import(&self, bytes: &[u8]) -> ImportResult {
        let json_val = toml::from_slice(bytes)?;
        Ok(Rc::new(self.convert_toml_val(&json_val)?))
    }
}