### Install cargo-fuzz Source: https://github.com/razrfalcon/xmlparser/blob/master/fuzz/README.md Installs the cargo-fuzz tool required for fuzz testing. ```bash cargo install cargo-fuzz ``` -------------------------------- ### Build Document Structure from XML Tokens in Rust Source: https://context7.com/razrfalcon/xmlparser/llms.txt This example shows how to parse XML tokens and build a hierarchical element structure. It requires importing the Tokenizer and Token enums from the xmlparser crate. The code iterates through tokens, managing a stack to reconstruct parent-child relationships and attributes. ```rust use xmlparser::{Tokenizer, Token, ElementEnd}; #[derive(Debug)] struct Element { name: String, attributes: Vec<(String, String)>, children: Vec, } #[derive(Debug)] enum Node { Element(Element), Text(String), } fn main() { let xml = r###" Rust Programming Jane Doe XML Parsing John Smith "###; let mut stack: Vec = Vec::new(); let mut current_attrs: Vec<(String, String)> = Vec::new(); let mut current_name = String::new(); for token in Tokenizer::from(xml) { match token.unwrap() { Token::ElementStart { local, .. } => { current_name = local.as_str().to_string(); current_attrs.clear(); } Token::Attribute { local, value, .. } => { current_attrs.push((local.as_str().to_string(), value.as_str().to_string())); } Token::ElementEnd { end: ElementEnd::Open, .. } => { stack.push(Element { name: current_name.clone(), attributes: current_attrs.clone(), children: Vec::new(), }); } Token::ElementEnd { end: ElementEnd::Empty, .. } => { let elem = Element { name: current_name.clone(), attributes: current_attrs.clone(), children: Vec::new(), }; if let Some(parent) = stack.last_mut() { parent.children.push(Node::Element(elem)); } } Token::ElementEnd { end: ElementEnd::Close(_, _), .. } => { if let Some(elem) = stack.pop() { if let Some(parent) = stack.last_mut() { parent.children.push(Node::Element(elem)); } else { // Root element print_element(&elem, 0); } } } Token::Text { text } => { let trimmed = text.as_str().trim(); if !trimmed.is_empty() { if let Some(parent) = stack.last_mut() { parent.children.push(Node::Text(trimmed.to_string())); } } } _ => {} } } } fn print_element(elem: &Element, indent: usize) { let prefix = " ".repeat(indent); print!("{}\<{}", prefix, elem.name); for (k, v) in &elem.attributes { print!(" {}=\"{}\"", k, v); } println!(">"); for child in &elem.children { match child { Node::Element(e) => print_element(e, indent + 1), Node::Text(t) => println!("{} {}", prefix, t), } } println!("{}\", prefix, elem.name); } ``` -------------------------------- ### Install AFL fuzzer Source: https://github.com/razrfalcon/xmlparser/blob/master/afl-fuzz/README.md Installs the cargo-afl tool required for fuzzing Rust projects. ```bash cargo install afl ``` -------------------------------- ### Build and run fuzzer Source: https://github.com/razrfalcon/xmlparser/blob/master/afl-fuzz/README.md Compiles the project with instrumentation and initiates the fuzzing process. ```bash cargo afl build cargo afl fuzz -i in -o out target/debug/afl-fuzz ``` -------------------------------- ### Run the fuzzer Source: https://github.com/razrfalcon/xmlparser/blob/master/fuzz/README.md Executes the fuzz_xml target using the nightly toolchain. ```bash cd .. cargo +nightly fuzz run fuzz_xml ``` -------------------------------- ### Handle XML Parsing Errors Source: https://context7.com/razrfalcon/xmlparser/llms.txt Demonstrates how to catch and inspect parsing errors, including retrieving line and column positions and matching on specific error types. ```rust use xmlparser::{Tokenizer, Token, Error, StreamError, TextPos}; fn main() { let invalid_xml = ""; for token in Tokenizer::from(invalid_xml) { match token { Ok(t) => println!("Token: {:?}", t), Err(e) => { // Get error position let pos: TextPos = e.pos(); println!("Error at line {}, column {}", pos.row, pos.col); println!("Error: {}", e); // Pattern match on specific error types match e { Error::InvalidAttribute(stream_err, pos) => { println!("Invalid attribute at {}:{}", pos.row, pos.col); match stream_err { StreamError::InvalidQuote(c, _) => { println!("Expected quote, got '{}'", c as char); } _ => println!("Stream error: {}", stream_err), } } Error::InvalidElement(_, pos) => { println!("Invalid element at {}:{}", pos.row, pos.col); } Error::UnknownToken(pos) => { println!("Unknown token at {}:{}", pos.row, pos.col); } _ => {} } break; } } } } ``` -------------------------------- ### Parse XML with Stream Source: https://context7.com/razrfalcon/xmlparser/llms.txt Utilize the low-level Stream interface for manual parsing of XML structures. ```rust use xmlparser::{Stream, TextPos}; fn main() { let mut stream = Stream::from("name='value' other"); // Consume an XML name let name = stream.consume_name().unwrap(); println!("Name: {} ({}..{})", name.as_str(), name.start(), name.end()); // Consume equals sign (with optional whitespace) stream.consume_eq().unwrap(); // Consume quote and value let quote = stream.consume_quote().unwrap(); let value = stream.consume_bytes(|_, c| c != quote); stream.consume_byte(quote).unwrap(); println!("Value: {}", value.as_str()); // Check current position println!("Current position: {}", stream.pos()); println!("Text position: {:?}", stream.gen_text_pos()); // Skip whitespace stream.skip_spaces(); // Check what's next println!("Starts with 'other': {}", stream.starts_with(b"other")); println!("At end: {}", stream.at_end()); } ``` -------------------------------- ### Parse XML fragments with Tokenizer::from_fragment Source: https://context7.com/razrfalcon/xmlparser/llms.txt Parses XML content that lacks a single root element or valid document structure. ```rust use xmlparser::{Tokenizer, Token, ElementEnd}; fn main() { // Parse just a fragment of XML content (multiple root elements) let fragment = "FirstSecond"; let tokenizer = Tokenizer::from_fragment(fragment, 0..fragment.len()); let mut items = Vec::new(); let mut current_text = String::new(); for token in tokenizer { match token.unwrap() { Token::Text { text } => { current_text = text.as_str().to_string(); } Token::ElementEnd { end: ElementEnd::Close(_, local), .. } if local.as_str() == "item" => { items.push(current_text.clone()); } _ => {} } } println!("Parsed items: {:?}", items); // Output: Parsed items: ["First", "Second"] } ``` -------------------------------- ### Tokenize XML string Source: https://github.com/razrfalcon/xmlparser/blob/master/README.md Iterate over tokens in an XML string using the Tokenizer. ```rust for token in xmlparser::Tokenizer::from("") { println!("{:?}", token); } ``` -------------------------------- ### Parse XML documents with Tokenizer Source: https://context7.com/razrfalcon/xmlparser/llms.txt Uses the Tokenizer to iterate over XML tokens, matching on specific constructs like declarations, elements, and attributes. ```rust use xmlparser::{Tokenizer, Token, ElementEnd}; fn main() { let xml = r#" Text content "#; for token in Tokenizer::from(xml) { match token { Ok(Token::Declaration { version, encoding, standalone, span }) => { println!("XML Declaration: version={}, encoding={:?}", version.as_str(), encoding.map(|e| e.as_str())); } Ok(Token::ElementStart { prefix, local, span }) => { println!("Element start: <{}{}> at {}..{}", if prefix.is_empty() { "".to_string() } else { format!("{}:", prefix.as_str()) }, local.as_str(), span.start(), span.end()); } Ok(Token::Attribute { prefix, local, value, span }) => { println!("Attribute: {}=\"{}\"", local.as_str(), value.as_str()); } Ok(Token::ElementEnd { end, span }) => { match end { ElementEnd::Open => println!("Element opened (>)"), ElementEnd::Close(prefix, local) => println!("Element closed: ", local.as_str()), ElementEnd::Empty => println!("Self-closing element (/>)"), } } Ok(Token::Text { text }) => { println!("Text: {:?}", text.as_str().trim()); } Err(e) => { println!("Error at {}:{}: {}", e.pos().row, e.pos().col, e); break; } _ => {} } } } ``` -------------------------------- ### Parse XML Namespaces in Rust Source: https://context7.com/razrfalcon/xmlparser/llms.txt Iterates through XML tokens to extract namespace prefixes, local names, and attribute values. Requires the xmlparser crate and standard library collections. ```rust use xmlparser::{Tokenizer, Token, ElementEnd}; use std::collections::HashMap; fn main() { let xml = r#""#; let mut namespaces: HashMap = HashMap::new(); for token in Tokenizer::from(xml) { match token.unwrap() { Token::ElementStart { prefix, local, .. } => { if !prefix.is_empty() { println!("Namespaced element: {}:{}", prefix.as_str(), local.as_str()); } else { println!("Element: {}", local.as_str()); } } Token::Attribute { prefix, local, value, .. } => { // Check for namespace declarations if local.as_str() == "xmlns" || prefix.as_str() == "xmlns" { let ns_prefix = if prefix.as_str() == "xmlns" { local.as_str().to_string() } else { "".to_string() }; namespaces.insert(ns_prefix.clone(), value.as_str().to_string()); println!("Namespace declaration: {} -> {}", if ns_prefix.is_empty() { "(default)" } else { &ns_prefix }, value.as_str()); } else if !prefix.is_empty() { println!("Namespaced attribute: {}:{} = \"{}\"", prefix.as_str(), local.as_str(), value.as_str()); } } _ => {} } } println!("\nResolved namespaces:"); for (prefix, uri) in &namespaces { println!(" {} -> {}", if prefix.is_empty() { "(default)" } else { prefix }, uri); } } ``` -------------------------------- ### Tokenize XML content Source: https://context7.com/razrfalcon/xmlparser/llms.txt Iterate through XML tokens using the Tokenizer to handle declarations, DTDs, elements, and attributes. ```rust use xmlparser::{Tokenizer, Token, ElementEnd, ExternalId, EntityDefinition}; fn main() { let xml = r#" ]> ]]> "#; for token in Tokenizer::from(xml) { match token.unwrap() { Token::Declaration { version, encoding, standalone, span } => { println!("Declaration: version={}", version.as_str()); } Token::DtdStart { name, external_id, span } => { println!("DOCTYPE start: {}", name.as_str()); if let Some(ExternalId::System(uri)) = external_id { println!(" SYSTEM: {}", uri.as_str()); } } Token::EntityDeclaration { name, definition, span } => { if let EntityDefinition::EntityValue(value) = definition { println!("Entity: {} = \"{}\"", name.as_str(), value.as_str()); } } Token::DtdEnd { span } => { println!("DOCTYPE end"); } Token::ProcessingInstruction { target, content, span } => { println!("PI: ", target.as_str(), content.map(|c| c.as_str()).unwrap_or("")); } Token::Comment { text, span } => { println!("Comment: ", text.as_str()); } Token::Cdata { text, span } => { println!("CDATA: {}", text.as_str()); } Token::ElementStart { prefix, local, span } => { let name = if prefix.is_empty() { local.as_str().to_string() } else { format!("{}:{}", prefix.as_str(), local.as_str()) }; println!("Element: <{}>", name); } Token::Attribute { prefix, local, value, span } => { println!(" @{} = \"{}\"", local.as_str(), value.as_str()); } _ => {} } } } ``` -------------------------------- ### Parse XML References Source: https://context7.com/razrfalcon/xmlparser/llms.txt Uses the Stream API to consume and identify character and entity references within an XML string. ```rust use xmlparser::{Stream, Reference}; fn main() { // Parse character references let mut stream = Stream::from("AA<&&custom;"); while !stream.at_end() { if let Some(reference) = stream.try_consume_reference() { match reference { Reference::Char(c) => { println!("Character reference: '{}' (U+{:04X})", c, c as u32); } Reference::Entity(name) => { println!("Entity reference: &{};", name); } } } else { break; } } } ``` -------------------------------- ### Access StrSpan position information Source: https://context7.com/razrfalcon/xmlparser/llms.txt Use StrSpan to retrieve byte offsets and range information for parsed XML components. ```rust use xmlparser::{Tokenizer, Token, StrSpan}; fn main() { let xml = "content"; for token in Tokenizer::from(xml) { if let Ok(Token::Attribute { local, value, span, .. }) = token { // StrSpan provides position information println!("Attribute '{}' found:", local.as_str()); println!(" Value: {}", value.as_str()); println!(" Value position: {}..{}", value.start(), value.end()); println!(" Full attribute span: {}..{}", span.start(), span.end()); println!(" Range: {:?}", span.range()); // StrSpan implements Deref to str println!(" Value length: {}", value.len()); println!(" Is empty: {}", value.is_empty()); } } } ``` -------------------------------- ### Validate XML Characters and Bytes Source: https://context7.com/razrfalcon/xmlparser/llms.txt Utilizes XmlCharExt and XmlByteExt traits to verify if characters or bytes conform to XML standards. ```rust use xmlparser::{XmlCharExt, XmlByteExt}; fn main() { // Check if characters are valid XML name characters let chars = ['a', 'A', '1', '_', ':', '-', '.', ' ', '<']; for c in &chars { println!("'{}': name_start={}, name={}, xml_char={}", c, c.is_xml_name_start(), c.is_xml_name(), c.is_xml_char()); } println!(); // Check bytes for XML validity let bytes: &[u8] = b"abc123 \t\n<>"; for b in bytes { println!("'{}' (0x{:02X}): digit={}, hex={}, space={}, letter={}, name={}", *b as char, b, b.is_xml_digit(), b.is_xml_hex_digit(), b.is_xml_space(), b.is_xml_letter(), b.is_xml_name()); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.