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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
use decor::{Formatted, Repr}; use document::Document; use std::cell::{Cell, RefCell}; use std::fmt::{Display, Formatter, Result}; use table::{Item, Table}; use value::{Array, DateTime, InlineTable, Value}; impl Display for Repr { fn fmt(&self, f: &mut Formatter) -> Result { write!( f, "{}{}{}", self.decor.prefix, self.raw_value, self.decor.suffix ) } } impl<T> Display for Formatted<T> { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}", self.repr) } } impl Display for DateTime { fn fmt(&self, f: &mut Formatter) -> Result { match *self { DateTime::OffsetDateTime(d) => write!(f, "{}", d), DateTime::LocalDateTime(d) => write!(f, "{}", d), DateTime::LocalDate(d) => write!(f, "{}", d), DateTime::LocalTime(d) => write!(f, "{}", d), } } } impl Display for Value { fn fmt(&self, f: &mut Formatter) -> Result { match *self { Value::Integer(ref repr) => write!(f, "{}", repr), Value::String(ref repr) => write!(f, "{}", repr), Value::Float(ref repr) => write!(f, "{}", repr), Value::Boolean(ref repr) => write!(f, "{}", repr), Value::DateTime(ref repr) => write!(f, "{}", repr), Value::Array(ref array) => write!(f, "{}", array), Value::InlineTable(ref table) => write!(f, "{}", table), } } } impl Display for Array { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}[", self.decor.prefix)?; join(f, self.iter(), ",")?; if self.trailing_comma { write!(f, ",")?; } write!(f, "{}", self.trailing)?; write!(f, "]{}", self.decor.suffix) } } impl Display for InlineTable { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}{{", self.decor.prefix)?; write!(f, "{}", self.preamble)?; for (i, (key, value)) in self .items .iter() .filter(|&(_, kv)| kv.value.is_value()) .map(|(_, kv)| (&kv.key, kv.value.as_value().unwrap())) .enumerate() { if i > 0 { write!(f, ",")?; } write!(f, "{}={}", key, value)?; } write!(f, "}}{}", self.decor.suffix) } } struct TableFormatState<'t> { path: RefCell<Vec<&'t str>>, table: Cell<&'t Table>, } impl<'a> Display for TableFormatState<'a> { fn fmt(&self, f: &mut Formatter) -> Result { let table = self.table.get(); for kv in table.items.values() { if let Item::Value(ref value) = kv.value { writeln!(f, "{}={}", kv.key, value)?; } } for kv in table.items.values() { match kv.value { Item::Table(ref t) => { self.path.borrow_mut().push(&kv.key.raw_value); self.table.set(t); if !(t.implicit && t.values_len() == 0) { write!(f, "{}[", t.decor.prefix)?; write!(f, "{}", self.path.borrow().join("."))?; writeln!(f, "]{}", t.decor.suffix)?; } write!(f, "{}", self)?; self.table.set(table); self.path.borrow_mut().pop(); } Item::ArrayOfTables(ref a) => { self.path.borrow_mut().push(&kv.key.raw_value); for t in a.iter() { self.table.set(t); write!(f, "{}[[", t.decor.prefix)?; write!(f, "{}", self.path.borrow().join("."))?; writeln!(f, "]]{}", t.decor.suffix)?; write!(f, "{}", self)?; } self.table.set(table); self.path.borrow_mut().pop(); } _ => {} } } Ok(()) } } impl Display for Table { fn fmt(&self, f: &mut Formatter) -> Result { let state = TableFormatState { path: RefCell::new(Vec::new()), table: Cell::new(self), }; write!(f, "{}", state) } } impl Display for Document { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}", self.as_table())?; write!(f, "{}", self.trailing) } } fn join<D, I>(f: &mut Formatter, iter: I, sep: &str) -> Result where D: Display, I: Iterator<Item = D>, { for (i, v) in iter.enumerate() { if i > 0 { write!(f, "{}", sep)?; } write!(f, "{}", v)?; } Ok(()) }