use crate::{Debug, Formatter, INDENT};
#[must_use = "must eventually call `finish()` on Debug builders"]
pub struct DebugStruct<'a> {
fmt: &'a mut Formatter,
has_fields: bool,
}
pub(crate) fn new<'a>(fmt: &'a mut Formatter, name: &str) -> DebugStruct<'a> {
fmt.cbox(INDENT);
fmt.ibox(-INDENT);
fmt.word_s(name);
fmt.end();
DebugStruct {
fmt,
has_fields: false,
}
}
impl<'a> DebugStruct<'a> {
pub fn field(&mut self, name: &str, value: &dyn Debug) -> &mut Self {
if self.has_fields {
self.fmt.trailing_comma_or_space(false);
} else {
self.fmt.word(" {");
self.fmt.space_if_nonempty();
}
self.fmt.word_s(name);
self.fmt.word(": ");
self.fmt.ibox(0);
value.fmt(self.fmt);
self.fmt.end();
self.has_fields = true;
self
}
pub fn finish_non_exhaustive(&mut self) {
if self.has_fields {
self.fmt.trailing_comma_or_space(false);
} else {
self.fmt.word(" {");
self.fmt.space_if_nonempty();
}
self.fmt.word("..");
self.fmt.space();
self.fmt.offset(-INDENT);
self.fmt.end_with_max_width(34); self.fmt.word("}");
}
pub fn finish(&mut self) {
if self.has_fields {
self.fmt.trailing_comma_or_space(true);
} else {
self.fmt.zerobreak();
}
self.fmt.offset(-INDENT);
self.fmt.end_with_max_width(34);
if self.has_fields {
self.fmt.word("}");
}
}
}