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
159
160
161
162
163
164
165
166
167
use super::{Format, Result, Style, Write};
/// A programmer-friendly repr for debugging purposes.
///
/// Often mimics the actual Rust structure of the type.
///
/// Can be derived via [`derive`](crate::fmt::derive).
///
/// Equivalent of [`core::fmt::Debug`].
#[derive(Default, Clone, Copy)]
pub struct Debug;
impl Style for Debug {}
super::derive!(struct Debug);
impl Debug {
/// Assists with formatting tuple structs (or tuples, when `name` is empty).
pub fn dbg_tuple<'w>(&self, f: &'w mut dyn Write, name: &str) -> DebugTuple<'w> {
DebugTuple::new(f, name)
}
/// Assists with formatting structs (or objects, when `name` is empty).
pub fn dbg_struct<'w>(&self, f: &'w mut dyn Write, name: &str) -> DebugStruct<'w> {
DebugStruct::new(f, name)
}
}
/// A helper for formatting tuples and tuple structs.
///
/// If an error is encountered, any future calls will no-op.
pub struct DebugTuple<'w> {
f: &'w mut dyn Write,
first: bool,
err: Result,
}
impl<'w> DebugTuple<'w> {
/// Create a new `DebugTuple`. Leave `name` empty for formatting tuples.
pub fn new(f: &'w mut dyn Write, name: &str) -> Self {
let err = f.write_str(name).and_then(|_| f.write_char('('));
Self {
f,
first: true,
err,
}
}
/// Format a field using [`Debug`].
pub fn field<T: Format<Debug>>(&mut self, data: &T) -> &mut Self {
self.field_with(|f| data.fmt(f, &Debug))
}
/// Format a field using a given style.
pub fn field_styled<T: Format<S>, S: Style>(&mut self, data: &T, style: &S) -> &mut Self {
self.field_with(|f| data.fmt(f, style))
}
/// Format a field using a given closure instead of data.
pub fn field_with(&mut self, f: impl FnOnce(&mut dyn Write) -> Result) -> &mut Self {
if !self.first {
self.with_err(|f| f.write_str(", "));
}
self.with_err(f);
self.first = false;
self
}
/// Finish off the tuple (struct), returning an error if any were
/// encountered.
pub fn finish(&mut self) -> Result {
self.err.and_then(|_| self.f.write_char(')'))
}
/// Finish off the tuple (struct), with a trailing `...` field, returning an
/// error if any were encountered.
pub fn non_exhaustive(&mut self) -> Result {
if !self.first {
self.with_err(|f| f.write_str(", "));
}
self.err.and_then(|_| self.f.write_str("...)"))
}
/// Run a closure with the writer, if no errors have already been
/// encountered, updating `self.err`.
fn with_err(&mut self, f: impl FnOnce(&mut dyn Write) -> Result) {
self.err = self.err.and_then(|_| f(self.f));
}
}
/// A helper for formatting structs and objects.
///
/// If an error is encountered, any future calls will no-op.
pub struct DebugStruct<'w> {
f: &'w mut dyn Write,
first: bool,
err: Result,
}
impl<'w> DebugStruct<'w> {
/// Create a new `DebugStruct`. Leave `name` empty for formatting objects.
pub fn new(f: &'w mut dyn Write, name: &str) -> Self {
let err = f.write_str(name).and_then(|_| f.write_str(" { "));
Self {
f,
first: true,
err,
}
}
/// Format a field using [`Debug`].
pub fn field<T: Format<Debug>>(&mut self, name: &str, data: &T) -> &mut Self {
self.field_with(name, |f| data.fmt(f, &Debug))
}
/// Format a field using a given style.
pub fn field_styled<T: Format<S>, S: Style>(
&mut self,
name: &str,
data: &T,
style: &S,
) -> &mut Self {
self.field_with(name, |f| data.fmt(f, style))
}
/// Format a field using a given closure instead of data.
pub fn field_with(
&mut self,
name: &str,
f: impl FnOnce(&mut dyn Write) -> Result,
) -> &mut Self {
if !self.first {
self.with_err(|f| f.write_str(", "));
}
self.with_err(|f| f.write_str(name).and_then(|_| f.write_str(": ")));
self.with_err(f);
self.first = false;
self
}
/// Finish off the struct (or object), returning an error if any were
/// encountered.
pub fn finish(&mut self) -> Result {
self.err.and_then(|_| self.f.write_str(" }"))
}
/// Finish off the struct (or object), with a trailing `...` field,
/// returning an error if any were encountered.
pub fn non_exhaustive(&mut self) -> Result {
if !self.first {
self.with_err(|f| f.write_str(", "));
}
self.err.and_then(|_| self.f.write_str("... }"))
}
/// Run a closure with the writer, if no errors have already been
/// encountered, updating `self.err`.
fn with_err(&mut self, f: impl FnOnce(&mut dyn Write) -> Result) {
self.err = self.err.and_then(|_| f(self.f));
}
}