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, 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 self.fmt.write_str(" ")?;
35 let split = match s.find('\n') {
37 self.on_newline = true;
41 self.on_newline = false;
45 self.fmt.write_str(&s[..split])?;
53 /// A struct to help with `fmt::Debug` implementations.
55 /// Constructed by the `Formatter::debug_struct` method.
57 #[allow(missing_debug_implementations)]
58 #[stable(feature = "debug_builders", since = "1.2.0")]
59 pub struct DebugStruct<'a, 'b: 'a> {
60 fmt: &'a mut fmt::Formatter<'b>,
65 pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
67 -> DebugStruct<'a, 'b> {
68 let result = fmt.write_str(name);
76 impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
77 /// Adds a new field to the generated struct output.
78 #[stable(feature = "debug_builders", since = "1.2.0")]
79 pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
80 self.result = self.result.and_then(|_| {
81 let prefix = if self.has_fields {
88 let mut writer = PadAdapter::new(self.fmt);
89 fmt::write(&mut writer,
90 format_args!("{}\n{}: {:#?}", prefix, name, value))
92 write!(self.fmt, "{} {}: {:?}", prefix, name, value)
96 self.has_fields = true;
100 /// Finishes output and returns any error encountered.
101 #[stable(feature = "debug_builders", since = "1.2.0")]
102 pub fn finish(&mut self) -> fmt::Result {
104 self.result = self.result.and_then(|_| {
105 if self.is_pretty() {
106 self.fmt.write_str("\n}")
108 self.fmt.write_str(" }")
115 fn is_pretty(&self) -> bool {
116 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
120 /// A struct to help with `fmt::Debug` implementations.
122 /// Constructed by the `Formatter::debug_tuple` method.
124 #[allow(missing_debug_implementations)]
125 #[stable(feature = "debug_builders", since = "1.2.0")]
126 pub struct DebugTuple<'a, 'b: 'a> {
127 fmt: &'a mut fmt::Formatter<'b>,
133 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
134 let result = fmt.write_str(name);
139 empty_name: name.is_empty(),
143 impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
144 /// Adds a new field to the generated tuple struct output.
145 #[stable(feature = "debug_builders", since = "1.2.0")]
146 pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
147 self.result = self.result.and_then(|_| {
148 let (prefix, space) = if self.fields > 0 {
154 if self.is_pretty() {
155 let mut writer = PadAdapter::new(self.fmt);
156 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, value))
158 write!(self.fmt, "{}{}{:?}", prefix, space, value)
166 /// Finishes output and returns any error encountered.
167 #[stable(feature = "debug_builders", since = "1.2.0")]
168 pub fn finish(&mut self) -> fmt::Result {
170 self.result = self.result.and_then(|_| {
171 if self.is_pretty() {
172 self.fmt.write_str("\n")?;
174 if self.fields == 1 && self.empty_name {
175 self.fmt.write_str(",")?;
177 self.fmt.write_str(")")
183 fn is_pretty(&self) -> bool {
184 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
188 struct DebugInner<'a, 'b: 'a> {
189 fmt: &'a mut fmt::Formatter<'b>,
194 impl<'a, 'b: 'a> DebugInner<'a, 'b> {
195 fn entry(&mut self, entry: &fmt::Debug) {
196 self.result = self.result.and_then(|_| {
197 if self.is_pretty() {
198 let mut writer = PadAdapter::new(self.fmt);
199 let prefix = if self.has_fields {
204 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
206 let prefix = if self.has_fields {
211 write!(self.fmt, "{}{:?}", prefix, entry)
215 self.has_fields = true;
218 pub fn finish(&mut self) {
219 let prefix = if self.is_pretty() && self.has_fields {
224 self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
227 fn is_pretty(&self) -> bool {
228 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
232 /// A struct to help with `fmt::Debug` implementations.
234 /// Constructed by the `Formatter::debug_set` method.
236 #[allow(missing_debug_implementations)]
237 #[stable(feature = "debug_builders", since = "1.2.0")]
238 pub struct DebugSet<'a, 'b: 'a> {
239 inner: DebugInner<'a, 'b>,
242 pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
243 let result = write!(fmt, "{{");
253 impl<'a, 'b: 'a> DebugSet<'a, 'b> {
254 /// Adds a new entry to the set output.
255 #[stable(feature = "debug_builders", since = "1.2.0")]
256 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
257 self.inner.entry(entry);
261 /// Adds the contents of an iterator of entries to the set output.
262 #[stable(feature = "debug_builders", since = "1.2.0")]
263 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
265 I: IntoIterator<Item = D>
267 for entry in entries {
273 /// Finishes output and returns any error encountered.
274 #[stable(feature = "debug_builders", since = "1.2.0")]
275 pub fn finish(&mut self) -> fmt::Result {
277 self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
281 /// A struct to help with `fmt::Debug` implementations.
283 /// Constructed by the `Formatter::debug_list` method.
285 #[allow(missing_debug_implementations)]
286 #[stable(feature = "debug_builders", since = "1.2.0")]
287 pub struct DebugList<'a, 'b: 'a> {
288 inner: DebugInner<'a, 'b>,
291 pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
292 let result = write!(fmt, "[");
302 impl<'a, 'b: 'a> DebugList<'a, 'b> {
303 /// Adds a new entry to the list output.
304 #[stable(feature = "debug_builders", since = "1.2.0")]
305 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
306 self.inner.entry(entry);
310 /// Adds the contents of an iterator of entries to the list output.
311 #[stable(feature = "debug_builders", since = "1.2.0")]
312 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
314 I: IntoIterator<Item = D>
316 for entry in entries {
322 /// Finishes output and returns any error encountered.
323 #[stable(feature = "debug_builders", since = "1.2.0")]
324 pub fn finish(&mut self) -> fmt::Result {
326 self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
330 /// A struct to help with `fmt::Debug` implementations.
332 /// Constructed by the `Formatter::debug_map` method.
334 #[allow(missing_debug_implementations)]
335 #[stable(feature = "debug_builders", since = "1.2.0")]
336 pub struct DebugMap<'a, 'b: 'a> {
337 fmt: &'a mut fmt::Formatter<'b>,
342 pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
343 let result = write!(fmt, "{{");
351 impl<'a, 'b: 'a> DebugMap<'a, 'b> {
352 /// Adds a new entry to the map output.
353 #[stable(feature = "debug_builders", since = "1.2.0")]
354 pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
355 self.result = self.result.and_then(|_| {
356 if self.is_pretty() {
357 let mut writer = PadAdapter::new(self.fmt);
358 let prefix = if self.has_fields {
363 fmt::write(&mut writer,
364 format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
366 let prefix = if self.has_fields {
371 write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
375 self.has_fields = true;
379 /// Adds the contents of an iterator of entries to the map output.
380 #[stable(feature = "debug_builders", since = "1.2.0")]
381 pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
384 I: IntoIterator<Item = (K, V)>
386 for (k, v) in entries {
392 /// Finishes output and returns any error encountered.
393 #[stable(feature = "debug_builders", since = "1.2.0")]
394 pub fn finish(&mut self) -> fmt::Result {
395 let prefix = if self.is_pretty() && self.has_fields {
400 self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
403 fn is_pretty(&self) -> bool {
404 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0