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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! A derive macro like `#[derive(Debug)]` that doesn't require all fields to implement
//! `Debug`. Non-Debug fields display as `<!Debug>` instead of causing a compile error.
//!
//! # Basic Usage
//!
//! ```
//! use dumpit::Dump;
//!
//! struct Opaque;
//!
//! #[derive(Dump)]
//! struct Config {
//! name: String,
//! secret: Opaque,
//! }
//!
//! let cfg = Config { name: "app".into(), secret: Opaque };
//! assert_eq!(format!("{:?}", cfg), r#"Config { name: "app", secret: <!Debug> }"#);
//! ```
//!
//! # Attributes
//!
//! All single-argument attributes support both `Meta::NameValue` (`key = value`) and
//! `Meta::List` (`key(value)`) forms. Multiple arguments require `Meta::List`.
//!
//! ## `#[dump(skip)]`
//!
//! Omit the field from output.
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct User {
//! name: String,
//! #[dump(skip)]
//! internal_id: u64,
//! }
//!
//! let u = User { name: "Alice".into(), internal_id: 42 };
//! assert_eq!(format!("{:?}", u), r#"User { name: "Alice" }"#);
//! ```
//!
//! ## `#[dump(skip_if = "condition")]`
//!
//! Omit the field when the condition evaluates to `true`. The expression has access to `self`.
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct User {
//! name: String,
//! #[dump(skip_if = "self.email.is_empty()")]
//! email: String,
//! }
//!
//! let u1 = User { name: "Alice".into(), email: String::new() };
//! assert_eq!(format!("{:?}", u1), r#"User { name: "Alice" }"#);
//!
//! let u2 = User { name: "Bob".into(), email: "bob@example.com".into() };
//! assert_eq!(format!("{:?}", u2), r#"User { name: "Bob", email: "bob@example.com" }"#);
//! ```
//!
//! ## `#[dump(format("fmt", args...))]`
//!
//! Format the field using a `format!`-style string. Arguments can reference `self`.
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct Person {
//! #[dump(format("{} years", self.age))]
//! age: u32,
//! }
//!
//! let p = Person { age: 30 };
//! assert_eq!(format!("{:?}", p), "Person { age: 30 years }");
//! ```
//!
//! ## `#[dump(literal = value)]`
//!
//! Replace the field output with a literal value.
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct Credentials {
//! user: String,
//! #[dump(literal = "<redacted>")]
//! #[allow(dead_code)]
//! password: String,
//! }
//!
//! let c = Credentials { user: "admin".into(), password: "secret".into() };
//! assert_eq!(format!("{:?}", c), r#"Credentials { user: "admin", password: "<redacted>" }"#);
//! ```
//!
//! ## `#[dump(with = "path::to::function")]`
//!
//! Use a custom function to format the field. The function signature must be
//! `fn(&&FieldType, &mut Formatter) -> fmt::Result`.
//!
//! ```
//! use dumpit::Dump;
//!
//! fn mask(_: &&String, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//! f.write_str("****")
//! }
//!
//! #[derive(Dump)]
//! struct Config {
//! #[dump(with = "mask")]
//! api_key: String,
//! }
//!
//! let c = Config { api_key: "sk-12345".into() };
//! assert_eq!(format!("{:?}", c), "Config { api_key: **** }");
//! ```
//!
//! ## `#[dump(take = N)]`
//!
//! Call `.iter().take(N)` on the field and debug-print the collected elements.
//! The field name becomes `name(n/total)` showing how many elements were taken
//! vs the collection's total length.
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct Data {
//! #[dump(take = 3)]
//! items: Vec<i32>,
//! }
//!
//! let d = Data { items: vec![10, 20, 30, 40, 50] };
//! assert_eq!(format!("{:?}", d), "Data { items(3/5): [10, 20, 30] }");
//! ```
//!
//! ## `#[dump(truncate = N)]`
//!
//! Truncate the debug output to at most `N` characters, appending `...` if exceeded.
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct Post {
//! #[dump(truncate = 10)]
//! body: String,
//! }
//!
//! let p = Post { body: "Hello, world!".into() };
//! assert_eq!(format!("{:?}", p), r#"Post { body: "Hello, wo... }"#);
//! ```
//!
//! # Generics
//!
//! For concrete types, `Dump` automatically detects whether a field implements `Debug`.
//! For generic type parameters, add a `Debug` bound to get proper output:
//!
//! ```
//! use dumpit::Dump;
//!
//! #[derive(Dump)]
//! struct Wrapper<T: std::fmt::Debug> {
//! value: T,
//! }
//!
//! let w = Wrapper { value: 42 };
//! assert_eq!(format!("{:?}", w), "Wrapper { value: 42 }");
//! ```
//!
//! Without the bound, generic fields display as `<!Debug>`.
//!
//! # Enums
//!
//! All enum variant types are supported.
//!
//! ```
//! use dumpit::Dump;
//!
//! struct Opaque;
//!
//! #[derive(Dump)]
//! enum Message {
//! Text(String),
//! Binary(Opaque),
//! Ping,
//! }
//!
//! assert_eq!(format!("{:?}", Message::Text("hi".into())), r#"Text("hi")"#);
//! assert_eq!(format!("{:?}", Message::Binary(Opaque)), "Binary(<!Debug>)");
//! assert_eq!(format!("{:?}", Message::Ping), "Ping");
//! ```
pub use Dump;
use fmt;
// ---------------------------------------------------------------------------
// Autoref specialization for Debug fallback
//
// `DebugWrap(value)` has two `__dumpit_build()` methods:
//
// 1. Inherent on `DebugWrap<&T>` where `T: Debug` — returns a DebugAs that
// calls `Debug::fmt`.
// 2. Trait `DebugFallbackBuild` on `DebugWrap<T>` for all `T` — returns a
// DebugAs that prints `<!Debug>`.
//
// When calling `DebugWrap(&field).__dumpit_build()`, method resolution picks
// (1) when T: Debug (inherent takes priority), and (2) otherwise.
// ---------------------------------------------------------------------------
;
;
// Inherent — wins when T: Debug
// Trait fallback — loses to inherent
// ---------------------------------------------------------------------------
// Formatted — wraps a pre-formatted String, displays it verbatim
// ---------------------------------------------------------------------------
;
// ---------------------------------------------------------------------------
// WithFn — wraps a reference and a formatting function
// fn(&T, &mut fmt::Formatter) -> fmt::Result
// ---------------------------------------------------------------------------
;
// ---------------------------------------------------------------------------
// Truncated — debug-formats the inner value, then truncates to N chars.
// Uses the same autoref specialization pattern for truncation.
// ---------------------------------------------------------------------------
;
// Inherent — wins when T: Debug
// ---------------------------------------------------------------------------
// TakeIter — calls .iter().take(n) on a collection, formats as
// (n/total) [elem1, elem2, ...]
// Uses autoref specialization: elements that implement Debug are printed
// normally, otherwise shown as <!Debug>.
// ---------------------------------------------------------------------------
;
/// Trait to get the length of a collection. Uses autoref specialization:
/// types with `.len()` use the inherent method; others fall back to
/// `.into_iter().count()`.
// Blanket fallback: count via iterator