parco_xml/dexml/
tag.rs

1use crate::{
2    DeXmlError,
3    dexml::{
4        lex::{Colon, Ident, OpenAngle},
5        parse::Action,
6        reader::Reader,
7    },
8};
9
10pub struct Tag<'a>(pub &'a str);
11
12impl<'a> Reader<'a> {
13    pub(crate) fn parse_tag(&mut self, action: Action) -> Result<Tag<'a>, DeXmlError> {
14        let ident: Ident = self.parse(action)?;
15
16        match self.lexer.peek(':') {
17            true => {
18                self.parse::<Colon>(action)?;
19                let ident: Ident = self.parse(action)?;
20                Ok(Tag(ident.0))
21            }
22            false => Ok(Tag(ident.0)),
23        }
24    }
25
26    /// inspect the next tag, can be used for parsing [`std::vec::Vec`]
27    pub fn peek_tag(&mut self) -> Option<&'a str> {
28        let mut lexer = self.lexer.clone();
29
30        if lexer.peek('<') {
31            lexer.token::<OpenAngle>().ok()?;
32        }
33        let ident = lexer.token::<Ident>().ok()?;
34        if lexer.peek(':') {
35            lexer.token::<Colon>().ok()?;
36            let id = lexer.token::<Ident>().ok()?;
37            return Some(id.0);
38        }
39
40        Some(ident.0)
41    }
42}