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

use crate::{DebugPls, Formatter};

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

impl<'a> DebugStruct<'a> {
    pub(crate) fn new(formatter: Formatter<'a>, name: &str) -> Self {
        DebugStruct {
            formatter,
            expr: syn::ExprStruct {
                attrs: vec![],
                path: syn::Ident::new(name, Span::call_site()).into(),
                brace_token: syn::token::Brace::default(),
                fields: syn::punctuated::Punctuated::new(),
                dot2_token: None,
                rest: None,
            },
        }
    }

    /// Adds the field to the struct output.
    #[must_use]
    pub fn field(mut self, name: &str, value: &dyn DebugPls) -> Self {
        self.expr.fields.push(syn::FieldValue {
            expr: Formatter::process(value),
            attrs: vec![],
            member: syn::Member::Named(syn::Ident::new(name, Span::call_site())),
            colon_token: Some(syn::token::Colon::default()),
        });
        self
    }

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

    /// Closes off the struct with `..`.
    pub fn finish_non_exhaustive(mut self) {
        self.expr.dot2_token = Some(syn::token::Dot2::default());
        self.finish();
    }
}