diff options
author | Martin Fischer <martin@push-f.com> | 2023-09-25 13:01:51 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-09-28 10:36:08 +0200 |
commit | 5ebbe93bf3c62df8159204b2e79d66607f136c30 (patch) | |
tree | fa4103e22163f221e8fe37b5fe76eab2209eedf1 /src | |
parent | 9e4ad2e8d9b2f1c422bbe0d32aae2afa425769fa (diff) |
feat: prettier Debug formatting for AttributeMap & Attribute
Diffstat (limited to 'src')
-rw-r--r-- | src/token.rs | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/src/token.rs b/src/token.rs index da1f8ba..2afef8c 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1,6 +1,7 @@ //! Provides the [`Token`] type. use std::collections::{btree_map, BTreeMap}; +use std::fmt::Debug; use std::iter::FromIterator; use std::ops::Index; @@ -86,14 +87,14 @@ pub struct Doctype { /// .collect(); /// assert_eq!(&attrs["href"], "http://example.com"); /// ``` -#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Default, PartialEq, Eq)] // Debug has a custom impl below pub struct AttributeMap { pub(crate) inner: BTreeMap<String, AttrInternal>, } /// The value type internally used by the [`AttributeMap`]. /// Not part of the public API. -#[derive(Clone, Default, Debug, Eq)] +#[derive(Clone, Default, Eq)] // Debug has a custom impl below pub(crate) struct AttrInternal { pub value: String, pub trace_idx: Option<AttributeTraceIdx>, @@ -119,7 +120,7 @@ impl PartialEq for AttrInternal { } /// An HTML attribute borrowed from an [`AttributeMap`]. -#[derive(Debug, Eq, PartialEq)] +#[derive(Eq, PartialEq)] // Debug has a custom impl below pub struct Attribute<'a> { name: &'a str, map_val: &'a AttrInternal, @@ -267,3 +268,31 @@ impl FromIterator<(String, String)> for AttributeMap { } } } + +impl Debug for AttributeMap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.inner.fmt(f) + } +} + +impl Debug for AttrInternal { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self.value)?; + + if let Some(idx) = self.trace_idx { + write!(f, " (trace #{})", idx.0)?; + } + + Ok(()) + } +} + +impl Debug for Attribute<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Attribute") + .field("name", &self.name) + .field("value", &self.value()) + .field("trace_idx", &self.trace_idx().map(|idx| idx.0)) + .finish() + } +} |