Skip to main content

parco_xml/dexml/
attr.rs

1use crate::{
2    DeXmlError,
3    de::{AppendPath, Reader},
4    dexml::{
5        lex::{Equal, Str},
6        parse::Action,
7        tag::Tag,
8    },
9};
10
11impl<'a> Reader<'a> {
12    /// deserialize a key value attribute and error if there isn't one or is malformed
13    ///
14    /// if you want to parse the attr value into a type look at [`Reader::dexml`]
15    pub fn attr(&mut self) -> Result<(&'a str, &'a str), DeXmlError> {
16        self.append_path(AppendPath::GeneralAttr);
17
18        let Tag(attr_name) = self.parse_tag(Action::AttrName)?;
19
20        self.exit_path();
21
22        self.append_path(AppendPath::NamedAttr(attr_name));
23
24        self.parse::<Equal>(Action::AttrValue)?;
25        let Str(attr_value) = self.parse::<Str>(Action::AttrValue)?;
26        let attr_value = attr_value.trim_matches(['"', '\'']);
27
28        self.exit_path();
29
30        Ok((attr_name, attr_value))
31    }
32
33    /// deserialize a key value attribute and if there isn't an attribute return [`None`]
34    ///
35    /// if you want to parse the attr value into a type look at [`Reader::dexml`]
36    pub fn attr_opt(&mut self) -> Result<Option<(&'a str, &'a str)>, DeXmlError> {
37        if self.lexer.peek('/') || self.lexer.peek('>') {
38            return Ok(None);
39        }
40        self.attr().map(Some)
41    }
42}