2014-06-22 01:35:49 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
2014-12-06 16:51:51 -06:00
|
|
|
use Table as TomlTable;
|
2015-01-03 07:28:22 -06:00
|
|
|
use Value::{self, String, Integer, Float, Boolean, Datetime, Array, Table};
|
2014-06-22 01:35:49 -05:00
|
|
|
|
2014-08-28 23:38:53 -05:00
|
|
|
struct Printer<'a, 'b:'a> {
|
2014-06-22 01:35:49 -05:00
|
|
|
output: &'a mut fmt::Formatter<'b>,
|
|
|
|
stack: Vec<&'a str>,
|
|
|
|
}
|
|
|
|
|
2015-02-03 00:54:46 -06:00
|
|
|
struct Key<'a>(&'a [&'a str]);
|
|
|
|
|
2015-01-23 10:19:29 -06:00
|
|
|
impl fmt::Display for Value {
|
2014-06-22 01:35:49 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2015-02-03 00:54:46 -06:00
|
|
|
String(ref s) => write_str(f, s),
|
2014-06-22 01:35:49 -05:00
|
|
|
Integer(i) => write!(f, "{}", i),
|
|
|
|
Float(fp) => {
|
|
|
|
try!(write!(f, "{}", fp));
|
|
|
|
if fp % 1.0 == 0.0 { try!(write!(f, ".0")) }
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Boolean(b) => write!(f, "{}", b),
|
|
|
|
Datetime(ref s) => write!(f, "{}", s),
|
|
|
|
Table(ref t) => {
|
|
|
|
let mut p = Printer { output: f, stack: Vec::new() };
|
|
|
|
p.print(t)
|
|
|
|
}
|
2015-01-07 10:58:50 -06:00
|
|
|
Array(ref a) => {
|
2015-01-09 13:48:06 -06:00
|
|
|
try!(write!(f, "["));
|
|
|
|
for (i, v) in a.iter().enumerate() {
|
|
|
|
if i != 0 { try!(write!(f, ", ")); }
|
|
|
|
try!(write!(f, "{}", v));
|
|
|
|
}
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
2014-06-22 01:35:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 00:54:46 -06:00
|
|
|
fn write_str(f: &mut fmt::Formatter, s: &str) -> fmt::Result {
|
|
|
|
try!(write!(f, "\""));
|
|
|
|
for ch in s.chars() {
|
|
|
|
match ch {
|
|
|
|
'\u{8}' => try!(write!(f, "\\b")),
|
|
|
|
'\u{9}' => try!(write!(f, "\\t")),
|
|
|
|
'\u{a}' => try!(write!(f, "\\n")),
|
|
|
|
'\u{c}' => try!(write!(f, "\\f")),
|
|
|
|
'\u{d}' => try!(write!(f, "\\r")),
|
|
|
|
'\u{22}' => try!(write!(f, "\\\"")),
|
|
|
|
'\u{5c}' => try!(write!(f, "\\\\")),
|
|
|
|
ch => try!(write!(f, "{}", ch)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write!(f, "\"")
|
|
|
|
}
|
|
|
|
|
2014-06-22 01:35:49 -05:00
|
|
|
impl<'a, 'b> Printer<'a, 'b> {
|
2014-09-21 09:55:13 -05:00
|
|
|
fn print(&mut self, table: &'a TomlTable) -> fmt::Result {
|
2014-06-22 01:35:49 -05:00
|
|
|
for (k, v) in table.iter() {
|
|
|
|
match *v {
|
|
|
|
Table(..) => continue,
|
|
|
|
Array(ref a) => {
|
2015-01-27 21:29:58 -06:00
|
|
|
match a.first() {
|
2014-06-22 01:35:49 -05:00
|
|
|
Some(&Table(..)) => continue,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-02-03 00:54:46 -06:00
|
|
|
try!(writeln!(self.output, "{} = {}", Key(&[k]), v));
|
2014-06-22 01:35:49 -05:00
|
|
|
}
|
|
|
|
for (k, v) in table.iter() {
|
|
|
|
match *v {
|
|
|
|
Table(ref inner) => {
|
2015-02-03 00:54:46 -06:00
|
|
|
self.stack.push(k);
|
|
|
|
try!(writeln!(self.output, "\n[{}]", Key(&self.stack)));
|
2014-06-22 01:35:49 -05:00
|
|
|
try!(self.print(inner));
|
|
|
|
self.stack.pop();
|
|
|
|
}
|
|
|
|
Array(ref inner) => {
|
2015-01-27 21:29:58 -06:00
|
|
|
match inner.first() {
|
2014-06-22 01:35:49 -05:00
|
|
|
Some(&Table(..)) => {}
|
|
|
|
_ => continue
|
|
|
|
}
|
2015-02-03 00:54:46 -06:00
|
|
|
self.stack.push(k);
|
2014-06-22 01:35:49 -05:00
|
|
|
for inner in inner.iter() {
|
2015-02-03 00:54:46 -06:00
|
|
|
try!(writeln!(self.output, "\n[[{}]]", Key(&self.stack)));
|
2014-06-22 01:35:49 -05:00
|
|
|
match *inner {
|
|
|
|
Table(ref inner) => try!(self.print(inner)),
|
2014-10-29 20:45:18 -05:00
|
|
|
_ => panic!("non-heterogeneous toml array"),
|
2014-06-22 01:35:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.stack.pop();
|
|
|
|
}
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 00:54:46 -06:00
|
|
|
impl<'a> fmt::Display for Key<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
for (i, part) in self.0.iter().enumerate() {
|
|
|
|
if i != 0 { try!(write!(f, ".")); }
|
|
|
|
let ok = part.chars().all(|c| {
|
|
|
|
match c {
|
|
|
|
'a' ... 'z' |
|
|
|
|
'A' ... 'Z' |
|
|
|
|
'0' ... '9' |
|
|
|
|
'-' | '_' => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if ok {
|
|
|
|
try!(write!(f, "{}", part));
|
|
|
|
} else {
|
|
|
|
try!(write_str(f, part));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 01:35:49 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
#[allow(warnings)]
|
|
|
|
mod tests {
|
2014-11-18 02:13:21 -06:00
|
|
|
use Value;
|
|
|
|
use Value::{String, Integer, Float, Boolean, Datetime, Array, Table};
|
2014-12-21 00:35:14 -06:00
|
|
|
use std::collections::BTreeMap;
|
2014-06-22 01:35:49 -05:00
|
|
|
|
2015-01-09 13:48:06 -06:00
|
|
|
macro_rules! map( ($($k:expr => $v:expr),*) => ({
|
2014-12-21 00:35:14 -06:00
|
|
|
let mut _m = BTreeMap::new();
|
2014-07-09 08:35:54 -05:00
|
|
|
$(_m.insert($k.to_string(), $v);)*
|
2014-06-22 01:35:49 -05:00
|
|
|
_m
|
2014-12-18 15:48:34 -06:00
|
|
|
}) );
|
2014-06-22 01:35:49 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_show() {
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(String("foo".to_string()).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"\"foo\"");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Integer(10).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"10");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Float(10.0).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"10.0");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Float(2.4).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"2.4");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Boolean(true).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"true");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Datetime("test".to_string()).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"test");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Array(vec![]).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"[]");
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Array(vec![Integer(1), Integer(2)]).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"[1, 2]");
|
2014-06-22 01:35:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn table() {
|
2014-07-09 08:35:54 -05:00
|
|
|
assert_eq!(Table(map! { }).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"");
|
2015-01-09 13:48:06 -06:00
|
|
|
assert_eq!(Table(map! { "test" => Integer(2) }).to_string().as_slice(),
|
2014-12-18 15:48:34 -06:00
|
|
|
"test = 2\n");
|
2014-06-22 01:35:49 -05:00
|
|
|
assert_eq!(Table(map! {
|
2015-01-09 13:48:06 -06:00
|
|
|
"test" => Integer(2),
|
|
|
|
"test2" => Table(map! {
|
|
|
|
"test" => String("wut".to_string())
|
2014-06-22 01:35:49 -05:00
|
|
|
})
|
2014-07-09 08:35:54 -05:00
|
|
|
}).to_string().as_slice(),
|
2014-06-22 01:35:49 -05:00
|
|
|
"test = 2\n\
|
|
|
|
\n\
|
|
|
|
[test2]\n\
|
2014-12-18 15:48:34 -06:00
|
|
|
test = \"wut\"\n");
|
2014-06-22 01:35:49 -05:00
|
|
|
assert_eq!(Table(map! {
|
2015-01-09 13:48:06 -06:00
|
|
|
"test" => Integer(2),
|
|
|
|
"test2" => Table(map! {
|
|
|
|
"test" => String("wut".to_string())
|
2014-06-22 01:35:49 -05:00
|
|
|
})
|
2014-07-09 08:35:54 -05:00
|
|
|
}).to_string().as_slice(),
|
2014-06-22 01:35:49 -05:00
|
|
|
"test = 2\n\
|
|
|
|
\n\
|
|
|
|
[test2]\n\
|
2014-12-18 15:48:34 -06:00
|
|
|
test = \"wut\"\n");
|
2014-06-22 01:35:49 -05:00
|
|
|
assert_eq!(Table(map! {
|
2015-01-09 13:48:06 -06:00
|
|
|
"test" => Integer(2),
|
|
|
|
"test2" => Array(vec![Table(map! {
|
|
|
|
"test" => String("wut".to_string())
|
2014-06-22 01:35:49 -05:00
|
|
|
})])
|
2014-07-09 08:35:54 -05:00
|
|
|
}).to_string().as_slice(),
|
2014-06-22 01:35:49 -05:00
|
|
|
"test = 2\n\
|
|
|
|
\n\
|
|
|
|
[[test2]]\n\
|
2014-12-18 15:48:34 -06:00
|
|
|
test = \"wut\"\n");
|
2015-02-03 00:54:46 -06:00
|
|
|
assert_eq!(Table(map! {
|
|
|
|
"foo.bar" => Integer(2),
|
|
|
|
"foo\"bar" => Integer(2)
|
|
|
|
}).to_string().as_slice(),
|
|
|
|
"\"foo\\\"bar\" = 2\n\
|
|
|
|
\"foo.bar\" = 2\n");
|
2014-06-22 01:35:49 -05:00
|
|
|
}
|
|
|
|
}
|