@@ -96,6 +96,42 @@ impl<'a> Attribute<'a> {
9696 Cow :: Owned ( s) => Ok ( s. into ( ) ) ,
9797 }
9898 }
99+
100+ /// If attribute value [represents] valid boolean values, returns `Some`, otherwise returns `None`.
101+ ///
102+ /// The valid boolean representations are only `"true"`, `"false"`, `"1"`, and `"0"`.
103+ ///
104+ /// # Examples
105+ ///
106+ /// ```
107+ /// # use pretty_assertions::assert_eq;
108+ /// use quick_xml::events::attributes::Attribute;
109+ ///
110+ /// let attr = Attribute::from(("attr", "false"));
111+ /// assert_eq!(attr.as_bool(), Some(false));
112+ ///
113+ /// let attr = Attribute::from(("attr", "0"));
114+ /// assert_eq!(attr.as_bool(), Some(false));
115+ ///
116+ /// let attr = Attribute::from(("attr", "true"));
117+ /// assert_eq!(attr.as_bool(), Some(true));
118+ ///
119+ /// let attr = Attribute::from(("attr", "1"));
120+ /// assert_eq!(attr.as_bool(), Some(true));
121+ ///
122+ /// let attr = Attribute::from(("attr", "bot bool"));
123+ /// assert_eq!(attr.as_bool(), None);
124+ /// ```
125+ ///
126+ /// [represents]: https://www.w3.org/TR/xmlschema11-2/#boolean
127+ #[ inline]
128+ pub fn as_bool ( & self ) -> Option < bool > {
129+ match self . value . as_ref ( ) {
130+ b"1" | b"true" => Some ( true ) ,
131+ b"0" | b"false" => Some ( false ) ,
132+ _ => None ,
133+ }
134+ }
99135}
100136
101137impl < ' a > Debug for Attribute < ' a > {
0 commit comments