/// Reads a string of XML data and converts it into a vector
/// of Token objects.
Reads a string of XML data and converts it into a vector
of Token objects.
Reads a string of XML data and converts it into a vector of Token objects .
pub(crate) fn xml_to_token_vec(xml: &str) -> Result<Vec<Token>, TextkitDocxError> {
    let mut result: Vec<Token> = Vec::new();
    let source_buf = BufReader::new(xml.as_bytes());
    let source_parser = EventReader::new(source_buf);
    let simple_template_pattern = Regex::new(PAT_HB_SMP).unwrap();
    let complex_template_pattern = Regex::new(PAT_HB_CPX).unwrap();
    let mut accumulator = TemplateAccumulator::Idle;
    for event in source_parser {
        match (&event, &accumulator) {
            (Ok(e @ xml::reader::XmlEvent::Characters(_)), _) => {
                if let xml::reader::XmlEvent::Characters(contents) = e {
                    accumulator.accumulate(contents);
                    if let TemplateAccumulator::Done(s) = &accumulator {
                        let token_type = if simple_template_pattern.is_match(&s) {
                            TokenType::Template
                        } else if complex_template_pattern.is_match(&s) {
                            TokenType::ComplexTemplate
                        } else {
                            TokenType::Normal
                        };
                        let new_event = xml::reader::XmlEvent::Characters(s.clone());
                        result.push(Token {
                            token_type: token_type,
                            token_text: Some(s.clone()),
                            xml_reader_event: new_event,
                        });
                        accumulator.reset();
                    }
                }
            }
            (Ok(anything_else), TemplateAccumulator::Idle) => result.push(Token {
                token_type: TokenType::Normal,
                token_text: None,
                xml_reader_event: anything_else.clone(),
            }),
            (Err(error), _) => return Err(error.clone().into()),
            _ => {}
        }
    }
    // FIXME here
    // compress_tokens(&result);
    Ok(result)
}
pub ( crate ) fn xml_to_token_vec ( xml : & str ) -> Result < Vec < Token > , TextkitDocxError > { let mut result : Vec < Token > = Vec :: new ( ) ; let source_buf = BufReader :: new ( xml . as_bytes ( ) ) ; let source_parser = EventReader :: new ( source_buf ) ; let simple_template_pattern = Regex :: new ( PAT_HB_SMP ) . unwrap ( ) ; let complex_template_pattern = Regex :: new ( PAT_HB_CPX ) . unwrap ( ) ; let mut accumulator = TemplateAccumulator :: Idle ; for event in source_parser { match ( & event , & accumulator ) { ( Ok ( e @ xml :: reader :: XmlEvent :: Characters ( _ ) ) , _ ) => { if let xml :: reader :: XmlEvent :: Characters ( contents ) = e { accumulator . accumulate ( contents ) ; if let TemplateAccumulator :: Done ( s ) = & accumulator { let token_type = if simple_template_pattern . is_match ( & s ) { TokenType :: Template } else if complex_template_pattern . is_match ( & s ) { TokenType :: ComplexTemplate } else { TokenType :: Normal } ; let new_event = xml :: reader :: XmlEvent :: Characters ( s . clone ( ) ) ; result . push ( Token { token_type : token_type , token_text : Some ( s . clone ( ) ) , xml_reader_event : new_event , } ) ; accumulator . reset ( ) ; } } } ( Ok ( anything_else ) , TemplateAccumulator :: Idle ) => result . push ( Token { token_type : TokenType :: Normal , token_text : None , xml_reader_event : anything_else . clone ( ) , } ) , ( Err ( error ) , _ ) => return Err ( error . clone ( ) . into ( ) ) , _ => { } } } // FIXME here // compress_tokens(&result); Ok ( result ) }
Reads a string of XML data and converts it into a vector
of Token objects.
Reads a string of XML data and converts it into a vector of Token objects .
xml
&str