]>
git.proxmox.com Git - rustc.git/blob - src/libcore/fmt/builders.rs
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 pub struct DebugStruct
<'a
, 'b
: 'a
> {
58 fmt
: &'a
mut fmt
::Formatter
<'b
>,
63 pub fn debug_struct_new
<'a
, 'b
>(fmt
: &'a
mut fmt
::Formatter
<'b
>, name
: &str)
64 -> DebugStruct
<'a
, 'b
> {
65 let result
= fmt
.write_str(name
);
73 impl<'a
, 'b
: 'a
> DebugStruct
<'a
, 'b
> {
74 /// Adds a new field to the generated struct output.
75 #[unstable(feature = "debug_builders", reason = "method was just created")]
76 pub fn field(mut self, name
: &str, value
: &fmt
::Debug
) -> DebugStruct
<'a
, 'b
> {
77 self.result
= self.result
.and_then(|_
| {
78 let prefix
= if self.has_fields
{
85 let mut writer
= PadAdapter
::new(self.fmt
);
86 fmt
::write(&mut writer
, format_args
!("{}\n{}: {:#?}", prefix
, name
, value
))
88 write
!(self.fmt
, "{} {}: {:?}", prefix
, name
, value
)
92 self.has_fields
= true;
96 /// Consumes the `DebugStruct`, finishing output and returning any error
98 #[unstable(feature = "debug_builders", reason = "method was just created")]
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 pub struct DebugTuple
<'a
, 'b
: 'a
> {
122 fmt
: &'a
mut fmt
::Formatter
<'b
>,
127 pub fn debug_tuple_new
<'a
, 'b
>(fmt
: &'a
mut fmt
::Formatter
<'b
>, name
: &str) -> DebugTuple
<'a
, 'b
> {
128 let result
= fmt
.write_str(name
);
136 impl<'a
, 'b
: 'a
> DebugTuple
<'a
, 'b
> {
137 /// Adds a new field to the generated tuple struct output.
138 #[unstable(feature = "debug_builders", reason = "method was just created")]
139 pub fn field(mut self, value
: &fmt
::Debug
) -> DebugTuple
<'a
, 'b
> {
140 self.result
= self.result
.and_then(|_
| {
141 let (prefix
, space
) = if self.has_fields
{
147 if self.is_pretty() {
148 let mut writer
= PadAdapter
::new(self.fmt
);
149 fmt
::write(&mut writer
, format_args
!("{}\n{:#?}", prefix
, value
))
151 write
!(self.fmt
, "{}{}{:?}", prefix
, space
, value
)
155 self.has_fields
= true;
159 /// Consumes the `DebugTuple`, finishing output and returning any error
161 #[unstable(feature = "debug_builders", reason = "method was just created")]
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
180 struct DebugInner
<'a
, 'b
: 'a
> {
181 fmt
: &'a
mut fmt
::Formatter
<'b
>,
186 impl<'a
, 'b
: 'a
> DebugInner
<'a
, 'b
> {
187 fn entry(&mut self, entry
: &fmt
::Debug
) {
188 self.result
= self.result
.and_then(|_
| {
189 if self.is_pretty() {
190 let mut writer
= PadAdapter
::new(self.fmt
);
191 let prefix
= if self.has_fields { "," }
else { "" }
;
192 fmt
::write(&mut writer
, format_args
!("{}\n{:#?}", prefix
, entry
))
194 let prefix
= if self.has_fields { ", " }
else { "" }
;
195 write
!(self.fmt
, "{}{:?}", prefix
, entry
)
199 self.has_fields
= true;
202 pub fn finish(&mut self) {
203 let prefix
= if self.is_pretty() && self.has_fields { "\n" }
else { "" }
;
204 self.result
= self.result
.and_then(|_
| self.fmt
.write_str(prefix
));
207 fn is_pretty(&self) -> bool
{
208 self.fmt
.flags() & (1 << (FlagV1
::Alternate
as usize)) != 0
212 /// A struct to help with `fmt::Debug` implementations.
214 /// Constructed by the `Formatter::debug_set` method.
216 pub struct DebugSet
<'a
, 'b
: 'a
> {
217 inner
: DebugInner
<'a
, 'b
>,
220 pub fn debug_set_new
<'a
, 'b
>(fmt
: &'a
mut fmt
::Formatter
<'b
>) -> DebugSet
<'a
, 'b
> {
221 let result
= write
!(fmt
, "{{");
231 impl<'a
, 'b
: 'a
> DebugSet
<'a
, 'b
> {
232 /// Adds a new entry to the set output.
233 #[unstable(feature = "debug_builders", reason = "method was just created")]
234 pub fn entry(mut self, entry
: &fmt
::Debug
) -> DebugSet
<'a
, 'b
> {
235 self.inner
.entry(entry
);
239 /// Consumes the `DebugSet`, finishing output and returning any error
241 #[unstable(feature = "debug_builders", reason = "method was just created")]
242 pub fn finish(mut self) -> fmt
::Result
{
244 self.inner
.result
.and_then(|_
| self.inner
.fmt
.write_str("}"))
248 /// A struct to help with `fmt::Debug` implementations.
250 /// Constructed by the `Formatter::debug_list` method.
252 pub struct DebugList
<'a
, 'b
: 'a
> {
253 inner
: DebugInner
<'a
, 'b
>,
256 pub fn debug_list_new
<'a
, 'b
>(fmt
: &'a
mut fmt
::Formatter
<'b
>) -> DebugList
<'a
, 'b
> {
257 let result
= write
!(fmt
, "[");
267 impl<'a
, 'b
: 'a
> DebugList
<'a
, 'b
> {
268 /// Adds a new entry to the set output.
269 #[unstable(feature = "debug_builders", reason = "method was just created")]
270 pub fn entry(mut self, entry
: &fmt
::Debug
) -> DebugList
<'a
, 'b
> {
271 self.inner
.entry(entry
);
275 /// Consumes the `DebugSet`, finishing output and returning any error
277 #[unstable(feature = "debug_builders", reason = "method was just created")]
278 pub fn finish(mut self) -> fmt
::Result
{
280 self.inner
.result
.and_then(|_
| self.inner
.fmt
.write_str("]"))
284 /// A struct to help with `fmt::Debug` implementations.
286 /// Constructed by the `Formatter::debug_map` method.
288 pub struct DebugMap
<'a
, 'b
: 'a
> {
289 fmt
: &'a
mut fmt
::Formatter
<'b
>,
294 pub fn debug_map_new
<'a
, 'b
>(fmt
: &'a
mut fmt
::Formatter
<'b
>) -> DebugMap
<'a
, 'b
> {
295 let result
= write
!(fmt
, "{{");
303 impl<'a
, 'b
: 'a
> DebugMap
<'a
, 'b
> {
304 /// Adds a new entry to the map output.
305 #[unstable(feature = "debug_builders", reason = "method was just created")]
306 pub fn entry(mut self, key
: &fmt
::Debug
, value
: &fmt
::Debug
) -> DebugMap
<'a
, 'b
> {
307 self.result
= self.result
.and_then(|_
| {
308 if self.is_pretty() {
309 let mut writer
= PadAdapter
::new(self.fmt
);
310 let prefix
= if self.has_fields { "," }
else { "" }
;
311 fmt
::write(&mut writer
, format_args
!("{}\n{:#?}: {:#?}", prefix
, key
, value
))
313 let prefix
= if self.has_fields { ", " }
else { "" }
;
314 write
!(self.fmt
, "{}{:?}: {:?}", prefix
, key
, value
)
318 self.has_fields
= true;
322 /// Consumes the `DebugMap`, finishing output and returning any error
324 #[unstable(feature = "debug_builders", reason = "method was just created")]
325 pub fn finish(self) -> fmt
::Result
{
326 let prefix
= if self.is_pretty() && self.has_fields { "\n" }
else { "" }
;
327 self.result
.and_then(|_
| write
!(self.fmt
, "{}}}", prefix
))
330 fn is_pretty(&self) -> bool
{
331 self.fmt
.flags() & (1 << (FlagV1
::Alternate
as usize)) != 0