aboutsummaryrefslogtreecommitdiff
path: root/examples/toml2json.rs
blob: 0d406804a38be144de5fbef88fe0f19dbeee8219 (plain)
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
#![deny(warnings)]

extern crate toml;
extern crate rustc_serialize;

use std::fs::File;
use std::env;
use std::io;
use std::io::prelude::*;

use toml::Value;
use rustc_serialize::json::Json;

fn main() {
    let mut args = env::args();
    let mut input = String::new();
    let filename = if args.len() > 1 {
        let name = args.nth(1).unwrap();
        File::open(&name).and_then(|mut f| {
            f.read_to_string(&mut input)
        }).unwrap();
        name
    } else {
        io::stdin().read_to_string(&mut input).unwrap();
        "<stdin>".to_string()
    };

    let mut parser = toml::Parser::new(&input);
    let toml = match parser.parse() {
        Some(toml) => toml,
        None => {
            for err in &parser.errors {
                let (loline, locol) = parser.to_linecol(err.lo);
                let (hiline, hicol) = parser.to_linecol(err.hi);
                println!("{}:{}:{}-{}:{} error: {}",
                         filename, loline, locol, hiline, hicol, err.desc);
            }
            return
        }
    };
    let json = convert(Value::Table(toml));
    println!("{}", json.pretty());
}

fn convert(toml: Value) -> Json {
    match toml {
        Value::String(s) => Json::String(s),
        Value::Integer(i) => Json::I64(i),
        Value::Float(f) => Json::F64(f),
        Value::Boolean(b) => Json::Boolean(b),
        Value::Array(arr) => Json::Array(arr.into_iter().map(convert).collect()),
        Value::Table(table) => Json::Object(table.into_iter().map(|(k, v)| {
            (k, convert(v))
        }).collect()),
        Value::Datetime(dt) => Json::String(dt),
    }
}