Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ fn main() {
.version("1.0.0")
.author("Philipp Speck <[email protected]>")
.about("A clone of xmllint, written in Rust")
.arg(Arg::with_name("noout")
.long("noout")
.help("Suppress output. By default, xmllint outputs the result tree.")
.takes_value(false)
)
.arg(Arg::new("file")
.required(true)
.help("XML file to parse")
)
.get_matches();

let quiet = matches.is_present("noout");
let path = matches.value_of("file").unwrap();
let file = File::open(path).unwrap();
let file = BufReader::new(file);
Expand All @@ -27,12 +33,12 @@ fn main() {
for e in parser {
match e {
Ok(XmlEvent::StartElement { name, .. }) => {
println!("{}+{}", indent(depth), name);
if !quiet { println!("{}+{}", indent(depth), name); }
depth += 1;
}
Ok(XmlEvent::EndElement { name }) => {
depth -= 1;
println!("{}-{}", indent(depth), name);
if !quiet { println!("{}-{}", indent(depth), name); }
}
Err(e) => {
println!("Error: {}", e);
Expand All @@ -47,4 +53,4 @@ fn indent(size: usize) -> String {
const INDENT: &'static str = " ";
(0..size).map(|_| INDENT)
.fold(String::with_capacity(size*INDENT.len()), |r, s| r + s)
}
}