dbg_pls/debug_struct.rs
1use crate::{DebugPls, Formatter};
2
3/// A helper designed to assist with creation of
4/// [`DebugPls`] implementations for structs.
5///
6/// # Examples
7///
8/// ```rust
9/// use dbg_pls::{pretty, DebugPls, Formatter};
10///
11/// struct Foo {
12/// bar: i32,
13/// baz: String,
14/// }
15///
16/// impl DebugPls for Foo {
17/// fn fmt(&self, f: Formatter) {
18/// f.debug_struct("Foo")
19/// .field("bar", &self.bar)
20/// .field("baz", &self.baz)
21/// .finish()
22/// }
23/// }
24/// let value = Foo {
25/// bar: 10,
26/// baz: "Hello World".to_string(),
27/// };
28/// assert_eq!(
29/// format!("{}", pretty(&value)),
30/// "Foo { bar: 10, baz: \"Hello World\" }",
31/// );
32/// ```
33pub struct DebugStruct<'a> {
34 formatter: Formatter<'a>,
35 expr: syn::ExprStruct,
36}
37
38impl<'a> DebugStruct<'a> {
39 pub(crate) fn new(formatter: Formatter<'a>, name: &str) -> Self {
40 DebugStruct {
41 formatter,
42 expr: syn::ExprStruct {
43 attrs: vec![],
44 qself: None,
45 path: syn::Ident::into(syn::parse_str(name).unwrap()),
46 brace_token: syn::token::Brace::default(),
47 fields: syn::punctuated::Punctuated::new(),
48 dot2_token: None,
49 rest: None,
50 },
51 }
52 }
53
54 /// Adds the field to the struct output.
55 ///
56 /// # Panics
57 /// This will panic if the name is not a valid identifier
58 #[must_use]
59 pub fn field(mut self, name: &str, value: &dyn DebugPls) -> Self {
60 self.expr.fields.push(syn::FieldValue {
61 expr: Formatter::process(value),
62 attrs: vec![],
63 member: syn::Member::Named(syn::parse_str(name).unwrap()),
64 colon_token: Some(syn::token::Colon::default()),
65 });
66 self
67 }
68
69 /// Closes off the struct.
70 pub fn finish(self) {
71 self.formatter.write_expr(self.expr);
72 }
73
74 /// Closes off the struct with `..`.
75 pub fn finish_non_exhaustive(mut self) {
76 self.expr.dot2_token = Some(syn::token::DotDot::default());
77 self.finish();
78 }
79}