| 1 | #![forbid(unsafe_code)] |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#}; |
| 8 | |
| 9 | use xmlrust-analyzer cargo xml-rs 0.8.19 crate/::readerrust-analyzer cargo xml-rs 0.8.19 reader/::XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#; |
| 10 | use xmlrust-analyzer cargo xml-rs 0.8.19 crate/::ParserConfigrust-analyzer cargo xml-rs 0.8.19 reader/config/ParserConfig#; |
| 11 | |
| 12 | fn mainrust-analyzer cargo xml-rs 0.8.19 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#>> { |
| 13 | let mut file; |
| 14 | let mut stdin; |
| 15 | let source: &mut dyn Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(file_name) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/args().().nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(1) { |
| 16 | file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(file_name).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Cannot open input file: {e}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 17 | &mut file |
| 18 | } else { |
| 19 | stdin = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdin().(); |
| 20 | &mut stdin |
| 21 | }; |
| 22 | |
| 23 | let reader = ParserConfigrust-analyzer cargo xml-rs 0.8.19 reader/config/ParserConfig#::newrust-analyzer cargo xml-rs 0.8.19 reader/config/impl#[ParserConfig]new().() |
| 24 | .whitespace_to_charactersrust-analyzer cargo xml-rs 0.8.19 reader/config/impl#[ParserConfig]whitespace_to_characters().(true) |
| 25 | .ignore_commentsrust-analyzer cargo xml-rs 0.8.19 reader/config/impl#[ParserConfig]ignore_comments().(false) |
| 26 | .create_readerrust-analyzer cargo xml-rs 0.8.19 reader/config/impl#[ParserConfig]create_reader().(BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(source)); |
| 27 | |
| 28 | let mut processing_instructions = 0; |
| 29 | let mut elements = 0; |
| 30 | let mut character_blocks = 0; |
| 31 | let mut cdata_blocks = 0; |
| 32 | let mut characters = 0; |
| 33 | let mut comment_blocks = 0; |
| 34 | let mut comment_characters = 0; |
| 35 | let mut namespaces = HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`]new().(); |
| 36 | let mut depth = 0; |
| 37 | let mut max_depth = 0; |
| 38 | |
| 39 | for e in reader { |
| 40 | let e = e.map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Error parsing XML document: {e}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 41 | match e { |
| 42 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::StartDocumentrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartDocument# { versionrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartDocument#version., encodingrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartDocument#encoding., standalonerust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartDocument#standalone. } => |
| 43 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 44 | "XML document version {}, encoded in {}, {}standalone", |
| 45 | version, encoding, if standalone.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(false) { "" } else { "not " } |
| 46 | ), |
| 47 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::EndDocumentrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#EndDocument# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Document finished"), |
| 48 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::ProcessingInstructionrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#ProcessingInstruction# { .. } => processing_instructions rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1, |
| 49 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::Whitespacerust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#Whitespace#(_) => {} // can't happen due to configuration |
| 50 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::Charactersrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#Characters#(s) => { |
| 51 | character_blocks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 52 | characters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().s.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 53 | } |
| 54 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::CDatarust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#CData#(s) => { |
| 55 | cdata_blocks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 56 | characters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().s.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 57 | } |
| 58 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::Commentrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#Comment#(s) => { |
| 59 | comment_blocks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 60 | comment_characters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().s.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 61 | } |
| 62 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::StartElementrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartElement# { namespacerust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartElement#namespace., .. } => { |
| 63 | depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 64 | max_depth = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(max_depth, depth); |
| 65 | elements rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 66 | namespaces.extendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`][`Extend<T>`]extend().(namespace.0rust-analyzer cargo xml-rs 0.8.19 namespace/Namespace#0..into_valuesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]into_values().()); |
| 67 | } |
| 68 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::EndElementrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#EndElement# { .. } => { |
| 69 | depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().1; |
| 70 | } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | namespaces.removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]remove().(xmlrust-analyzer cargo xml-rs 0.8.19 crate/::namespacerust-analyzer cargo xml-rs 0.8.19 namespace/::NS_EMPTY_URIrust-analyzer cargo xml-rs 0.8.19 namespace/NS_EMPTY_URI.); |
| 75 | namespaces.removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]remove().(xmlrust-analyzer cargo xml-rs 0.8.19 crate/::namespacerust-analyzer cargo xml-rs 0.8.19 namespace/::NS_XMLNS_URIrust-analyzer cargo xml-rs 0.8.19 namespace/NS_XMLNS_URI.); |
| 76 | namespaces.removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]remove().(xmlrust-analyzer cargo xml-rs 0.8.19 crate/::namespacerust-analyzer cargo xml-rs 0.8.19 namespace/::NS_XML_URIrust-analyzer cargo xml-rs 0.8.19 namespace/NS_XML_URI.); |
| 77 | |
| 78 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Elements: {elements}, maximum depth: {max_depth}"); |
| 79 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Namespaces (excluding built-in): {}", namespaces.lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]len().()); |
| 80 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Characters: {characters}, characters blocks: {character_blocks}, CDATA blocks: {cdata_blocks}"); |
| 81 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Comment blocks: {comment_blocks}, comment characters: {comment_characters}"); |
| 82 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Processing instructions (excluding built-in): {processing_instructions}"); |
| 83 | |
| 84 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 85 | } |