dbg_pls/debug_tuple.rs
1use crate::{DebugPls, Formatter};
2
3/// A helper designed to assist with creation of
4/// [`DebugPls`] implementations for tuples.
5///
6/// # Examples
7///
8/// ```rust
9/// use dbg_pls::{pretty, DebugPls, Formatter};
10///
11/// struct Foo(i32, String);
12///
13/// impl DebugPls for Foo {
14/// fn fmt(&self, f: Formatter) {
15/// f.debug_tuple()
16/// .field(&self.0)
17/// .field(&self.1)
18/// .finish()
19/// }
20/// }
21///
22/// let value = Foo(10, "Hello".to_string());
23/// assert_eq!(format!("{}", pretty(&value)), "(10, \"Hello\")");
24/// ```
25pub struct DebugTuple<'a> {
26 formatter: Formatter<'a>,
27 expr: syn::ExprTuple,
28}
29
30impl<'a> DebugTuple<'a> {
31 pub(crate) fn new(formatter: Formatter<'a>) -> Self {
32 DebugTuple {
33 formatter,
34 expr: syn::ExprTuple {
35 attrs: vec![],
36 paren_token: syn::token::Paren::default(),
37 elems: syn::punctuated::Punctuated::new(),
38 },
39 }
40 }
41
42 /// Adds the field to the tuple output.
43 #[must_use]
44 pub fn field(mut self, value: &dyn DebugPls) -> Self {
45 self.expr.elems.push(Formatter::process(value));
46 self
47 }
48
49 /// Closes off the tuple.
50 pub fn finish(self) {
51 self.formatter.write_expr(self.expr);
52 }
53}