1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use fmt::{self, Write, FlagV1};
14 struct PadAdapter<'a, 'b: 'a> {
15 fmt: &'a mut fmt::Formatter<'b>,
19 impl<'a, 'b: 'a> PadAdapter<'a, 'b> {
20 fn new(fmt: &'a mut fmt::Formatter<'b>) -> PadAdapter<'a, 'b> {
28 impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
29 fn write_str(&mut self, mut s: &str) -> fmt::Result {
32 try!(self.fmt.write_str(" "));
35 let split = match s.find('\n') {
37 self.on_newline = true;
41 self.on_newline = false;
45 try!(self.fmt.write_str(&s[..split]));
53 /// A struct to help with `fmt::Debug` implementations.
55 /// Constructed by the `Formatter::debug_struct` method.
57 #[stable(feature = "debug_builders", since = "1.2.0")]
58 pub struct DebugStruct<'a, 'b: 'a> {
59 fmt: &'a mut fmt::Formatter<'b>,
64 pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str)
65 -> DebugStruct<'a, 'b> {
66 let result = fmt.write_str(name);
74 impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
75 /// Adds a new field to the generated struct output.
76 #[stable(feature = "debug_builders", since = "1.2.0")]
77 pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
78 self.result = self.result.and_then(|_| {
79 let prefix = if self.has_fields {
86 let mut writer = PadAdapter::new(self.fmt);
87 fmt::write(&mut writer, format_args!("{}\n{}: {:#?}", prefix, name, value))
89 write!(self.fmt, "{} {}: {:?}", prefix, name, value)
93 self.has_fields = true;
97 /// Finishes output and returns any error encountered.
98 #[stable(feature = "debug_builders", since = "1.2.0")]
99 pub fn finish(&mut self) -> fmt::Result {
101 self.result = self.result.and_then(|_| {
102 if self.is_pretty() {
103 self.fmt.write_str("\n}")
105 self.fmt.write_str(" }")
112 fn is_pretty(&self) -> bool {
113 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
117 /// A struct to help with `fmt::Debug` implementations.
119 /// Constructed by the `Formatter::debug_tuple` method.
121 #[stable(feature = "debug_builders", since = "1.2.0")]
122 pub struct DebugTuple<'a, 'b: 'a> {
123 fmt: &'a mut fmt::Formatter<'b>,
128 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
129 let result = fmt.write_str(name);
137 impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
138 /// Adds a new field to the generated tuple struct output.
139 #[stable(feature = "debug_builders", since = "1.2.0")]
140 pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
141 self.result = self.result.and_then(|_| {
142 let (prefix, space) = if self.has_fields {
148 if self.is_pretty() {
149 let mut writer = PadAdapter::new(self.fmt);
150 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, value))
152 write!(self.fmt, "{}{}{:?}", prefix, space, value)
156 self.has_fields = true;
160 /// Finishes output and returns any error encountered.
161 #[stable(feature = "debug_builders", since = "1.2.0")]
162 pub fn finish(&mut self) -> fmt::Result {
164 self.result = self.result.and_then(|_| {
165 if self.is_pretty() {
166 self.fmt.write_str("\n)")
168 self.fmt.write_str(")")
175 fn is_pretty(&self) -> bool {
176 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
179 /// Returns the wrapped `Formatter`.
180 #[unstable(feature = "debug_builder_formatter", reason = "recently added")]
181 pub fn formatter(&mut self) -> &mut fmt::Formatter<'b> {
186 struct DebugInner<'a, 'b: 'a> {
187 fmt: &'a mut fmt::Formatter<'b>,
192 impl<'a, 'b: 'a> DebugInner<'a, 'b> {
193 fn entry(&mut self, entry: &fmt::Debug) {
194 self.result = self.result.and_then(|_| {
195 if self.is_pretty() {
196 let mut writer = PadAdapter::new(self.fmt);
197 let prefix = if self.has_fields { "," } else { "" };
198 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
200 let prefix = if self.has_fields { ", " } else { "" };
201 write!(self.fmt, "{}{:?}", prefix, entry)
205 self.has_fields = true;
208 pub fn finish(&mut self) {
209 let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
210 self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
213 fn is_pretty(&self) -> bool {
214 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
218 /// A struct to help with `fmt::Debug` implementations.
220 /// Constructed by the `Formatter::debug_set` method.
222 #[stable(feature = "debug_builders", since = "1.2.0")]
223 pub struct DebugSet<'a, 'b: 'a> {
224 inner: DebugInner<'a, 'b>,
227 pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
228 let result = write!(fmt, "{{");
238 impl<'a, 'b: 'a> DebugSet<'a, 'b> {
239 /// Adds a new entry to the set output.
240 #[stable(feature = "debug_builders", since = "1.2.0")]
241 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
242 self.inner.entry(entry);
246 /// Adds the contents of an iterator of entries to the set output.
247 #[stable(feature = "debug_builders", since = "1.2.0")]
248 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
249 where D: fmt::Debug, I: IntoIterator<Item=D> {
250 for entry in entries {
256 /// Finishes output and returns any error encountered.
257 #[stable(feature = "debug_builders", since = "1.2.0")]
258 pub fn finish(&mut self) -> fmt::Result {
260 self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
264 /// A struct to help with `fmt::Debug` implementations.
266 /// Constructed by the `Formatter::debug_list` method.
268 #[stable(feature = "debug_builders", since = "1.2.0")]
269 pub struct DebugList<'a, 'b: 'a> {
270 inner: DebugInner<'a, 'b>,
273 pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
274 let result = write!(fmt, "[");
284 impl<'a, 'b: 'a> DebugList<'a, 'b> {
285 /// Adds a new entry to the list output.
286 #[stable(feature = "debug_builders", since = "1.2.0")]
287 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
288 self.inner.entry(entry);
292 /// Adds the contents of an iterator of entries to the list output.
293 #[stable(feature = "debug_builders", since = "1.2.0")]
294 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
295 where D: fmt::Debug, I: IntoIterator<Item=D> {
296 for entry in entries {
302 /// Finishes output and returns any error encountered.
303 #[stable(feature = "debug_builders", since = "1.2.0")]
304 pub fn finish(&mut self) -> fmt::Result {
306 self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
310 /// A struct to help with `fmt::Debug` implementations.
312 /// Constructed by the `Formatter::debug_map` method.
314 #[stable(feature = "debug_builders", since = "1.2.0")]
315 pub struct DebugMap<'a, 'b: 'a> {
316 fmt: &'a mut fmt::Formatter<'b>,
321 pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
322 let result = write!(fmt, "{{");
330 impl<'a, 'b: 'a> DebugMap<'a, 'b> {
331 /// Adds a new entry to the map output.
332 #[stable(feature = "debug_builders", since = "1.2.0")]
333 pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
334 self.result = self.result.and_then(|_| {
335 if self.is_pretty() {
336 let mut writer = PadAdapter::new(self.fmt);
337 let prefix = if self.has_fields { "," } else { "" };
338 fmt::write(&mut writer, format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
340 let prefix = if self.has_fields { ", " } else { "" };
341 write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
345 self.has_fields = true;
349 /// Adds the contents of an iterator of entries to the map output.
350 #[stable(feature = "debug_builders", since = "1.2.0")]
351 pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
352 where K: fmt::Debug, V: fmt::Debug, I: IntoIterator<Item=(K, V)> {
353 for (k, v) in entries {
359 /// Finishes output and returns any error encountered.
360 #[stable(feature = "debug_builders", since = "1.2.0")]
361 pub fn finish(&mut self) -> fmt::Result {
362 let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
363 self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
366 fn is_pretty(&self) -> bool {
367 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0