viral32111_xml/
lib.rs

1use self::{declaration::Declaration, element::Element};
2use std::error::Error;
3
4pub mod attributes;
5pub mod declaration;
6pub mod element;
7
8pub struct Document {
9	pub declaration: Declaration,
10	pub root: Element,
11}
12
13impl Document {}
14
15/// Parses an XML document.
16pub fn parse(text: &str) -> Result<Document, Box<dyn Error>> {
17	// Don't bother if we have nothing
18	if text.is_empty() {
19		return Err("Empty XML document".into());
20	}
21
22	// Parse the XML declaration
23	let (declaration, declaration_end_position) = declaration::parse(text)?;
24
25	// Parse the root element
26	let (root, root_element_end_position) = element::parse(&text[declaration_end_position..])?;
27
28	// Sanity check
29	if declaration_end_position + root_element_end_position > text.len() {
30		return Err("Remaining unparsed text in XML document".into());
31	}
32
33	Ok(Document { declaration, root })
34}
35
36/*
37/// Minifies an XML document.
38/// Removing new lines, indentation, etc. Spacing between attributes is preserved.
39pub fn minify(text: &str) -> String {
40	text.chars().filter(|c| !c.is_whitespace()).collect()
41}
42*/
43
44/*
45#[cfg(test)]
46mod tests {
47	use super::*;
48
49	#[test]
50	fn it_works() {
51		let result = add(2, 2);
52		assert_eq!(result, 4);
53	}
54}
55*/