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
use {Ion, Section, Value};

use std::fmt;

impl fmt::Display for Ion {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        for (name, section) in &self.sections {
            f.write_fmt(format_args!("[{}]\n", name))?;
            section.fmt(f)?;
            f.write_str("\n")?;
        }
        Ok(())
    }
}

impl fmt::Display for Section {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        for (k, v) in &self.dictionary {
            if v.type_str() == "string" {
                f.write_fmt(format_args!("{} = \"{}\"\n", k, v))?;
            } else {
                f.write_fmt(format_args!("{} = {}\n", k, v))?;
            }
        }

        for row in &self.rows {
            for cell in row {
                fmt::Display::fmt(&format!("| {} ", cell), f)?;
            }
            f.write_str("|\n")?;
        }
        Ok(())
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            Value::String(ref v) => v.fmt(f),
            Value::Integer(ref v) => v.fmt(f),
            Value::Float(ref v) => v.fmt(f),
            Value::Boolean(ref v) => v.fmt(f),
            Value::Array(ref v) => {
                f.write_str("[ ")?;

                let mut first = true;
                for i in v {
                    if first {
                        first = false
                    } else {
                        f.write_str(", ")?
                    }
                    if i.is_string() {
                        f.write_str("\"")?;
                        i.fmt(f)?;
                        f.write_str("\"")?;
                    } else {
                        i.fmt(f)?;
                    }
                }
                f.write_str(" ]")
            }
            Value::Dictionary(ref d) => {
                f.write_str("{ ")?;

                let mut first = true;
                for (k, v) in d {
                    if first {
                        first = false
                    } else {
                        f.write_str(", ")?
                    }
                    k.fmt(f)?;
                    f.write_str(" = ")?;
                    if v.type_str() == "string" {
                        f.write_str("\"")?;
                        v.fmt(f)?;
                        f.write_str("\"")?;
                    } else {
                        v.fmt(f)?;
                    }
                }
                f.write_str(" }")
            }
        }
    }
}

// impl fmt::Display for super::Error {
//     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//         fmt::Debug::fmt(self, f)
//     }
// }