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
use crate::{DebugPls, Formatter};

/// A helper designed to assist with creation of
/// [`DebugPls`] implementations for sets.
///
/// # Examples
///
/// ```rust
/// use dbg_pls::{pretty, DebugPls, Formatter};
/// use std::collections::BTreeSet;
///
/// struct Foo(BTreeSet<String>);
///
/// impl DebugPls for Foo {
///     fn fmt(&self, f: Formatter) {
///         f.debug_set().entries(&self.0).finish()
///     }
/// }
/// let mut value = Foo(BTreeSet::from([
///     "Hello".to_string(),
///     "World".to_string(),
/// ]));
/// assert_eq!(
///     format!("{}", pretty(&value)),
/// "{
///     \"Hello\";
///     \"World\"
/// }",
/// );
/// ```
pub struct DebugSet<'a> {
    formatter: Formatter<'a>,
    set: syn::Block,
}

impl<'a> DebugSet<'a> {
    pub(crate) fn new(formatter: Formatter<'a>) -> Self {
        DebugSet {
            formatter,
            set: syn::Block {
                brace_token: syn::token::Brace::default(),
                stmts: vec![],
            },
        }
    }

    /// Adds the entry to the set output.
    #[must_use]
    pub fn entry(mut self, value: &dyn DebugPls) -> Self {
        let expr = Formatter::process(value);
        self.set
            .stmts
            .push(syn::Stmt::Semi(expr, syn::token::Semi::default()));
        self
    }

    /// Adds all the entries to the set output.
    #[must_use]
    pub fn entries<V, I>(self, entries: I) -> Self
    where
        V: DebugPls,
        I: IntoIterator<Item = V>,
    {
        entries.into_iter().fold(self, |f, entry| f.entry(&entry))
    }

    /// Closes off the set.
    pub fn finish(mut self) {
        // remove the last semicolon
        if let Some(syn::Stmt::Semi(entry, _)) = self.set.stmts.pop() {
            self.set.stmts.push(syn::Stmt::Expr(entry));
        }

        self.formatter.write_expr(syn::ExprBlock {
            attrs: vec![],
            label: None,
            block: self.set,
        });
    }
}