Parses a KML document.
(to_parse: &[u8])
| 107 | |
| 108 | /// Parses a KML document. |
| 109 | pub fn parse(to_parse: &[u8]) -> Result<Self> { |
| 110 | ensure!(to_parse.len() <= 1024 * 1024, "kml-file is too large"); |
| 111 | |
| 112 | let mut reader = quick_xml::Reader::from_reader(to_parse); |
| 113 | reader.config_mut().trim_text(true); |
| 114 | |
| 115 | let mut kml = Kml::new(); |
| 116 | kml.locations = Vec::with_capacity(100); |
| 117 | |
| 118 | let mut buf = Vec::new(); |
| 119 | |
| 120 | loop { |
| 121 | match reader.read_event_into(&mut buf).with_context(|| { |
| 122 | format!( |
| 123 | "location parsing error at position {}", |
| 124 | reader.buffer_position() |
| 125 | ) |
| 126 | })? { |
| 127 | quick_xml::events::Event::Start(ref e) => kml.starttag_cb(e, &reader), |
| 128 | quick_xml::events::Event::End(ref e) => kml.endtag_cb(e), |
| 129 | quick_xml::events::Event::Text(ref e) => kml.text_cb(e), |
| 130 | quick_xml::events::Event::Eof => break, |
| 131 | _ => (), |
| 132 | } |
| 133 | buf.clear(); |
| 134 | } |
| 135 | |
| 136 | Ok(kml) |
| 137 | } |
| 138 | |
| 139 | fn text_cb(&mut self, event: &BytesText) { |
| 140 | if self.tag == KmlTag::PlacemarkTimestampWhen |