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
use syn::__private::Span;

use crate::{DebugPls, Formatter};

/// A helper designed to assist with creation of
/// [`DebugPls`] implementations for tuple structs.
///
/// # Examples
///
/// ```rust
/// use dbg_pls::{pretty, DebugPls, Formatter};
///
/// struct Foo(i32, String);
///
/// impl DebugPls for Foo {
///     fn fmt(&self, f: Formatter) {
///         f.debug_tuple_struct("Foo")
///             .field(&self.0)
///             .field(&self.1)
///             .finish()
///     }
/// }
///
/// let value = Foo(10, "Hello".to_string());
/// assert_eq!(format!("{}", pretty(&value)), "Foo(10, \"Hello\")");
/// ```
pub struct DebugTupleStruct<'a> {
    formatter: Formatter<'a>,
    expr: syn::ExprCall,
}

impl<'a> DebugTupleStruct<'a> {
    pub(crate) fn new(formatter: Formatter<'a>, name: &str) -> Self {
        DebugTupleStruct {
            formatter,
            expr: syn::ExprCall {
                attrs: vec![],
                func: Box::new(syn::Expr::Path(syn::ExprPath {
                    attrs: vec![],
                    qself: None,
                    path: syn::Ident::new(name, Span::call_site()).into(),
                })),
                paren_token: syn::token::Paren::default(),
                args: syn::punctuated::Punctuated::new(),
            },
        }
    }

    /// Adds the field to the tuple struct output.
    #[must_use]
    pub fn field(mut self, value: &dyn DebugPls) -> Self {
        self.expr.args.push(Formatter::process(value));
        self
    }

    /// Closes off the tuple struct.
    pub fn finish(self) {
        self.formatter.write_expr(self.expr);
    }
}