From 33b057ffb1d74c7ac10961f49c43e777e581a7e7 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 10 Apr 2023 08:04:05 +0300 Subject: feat: return attribute value as a string Sponsored-by: author --- src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 374518d..50ec205 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -385,6 +385,17 @@ pub enum AttributeValue { Boolean, } +impl AttributeValue { + /// Return value of an attribute as a string. For a boolean + /// attribute, this is the empty string. + pub fn as_str(&self) -> &str { + match self { + Self::String(s) => s, + Self::Boolean => "", + } + } +} + /// An HTML element. /// /// The element has a [`Tag`], possibly some attributes, and possibly @@ -665,6 +676,7 @@ mod test { e.attribute("foo"), Some(&AttributeValue::String("bar".into())) ); + assert_eq!(e.attribute("foo").map(|x| x.as_str()), Some("bar")); } #[test] -- cgit v1.2.1