From d753a7154f174db3cc556fd8d2b0b006945ddfd6 Mon Sep 17 00:00:00 2001 From: Pat Sier Date: Sat, 22 Jan 2022 20:02:23 -0600 Subject: [PATCH] fix: parsing Folder and Document sibling elements (#19) --- src/reader.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index ac276d5..10618fc 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -171,11 +171,11 @@ where } }; } - Event::Decl(_) - | Event::CData(_) - | Event::Empty(_) - | Event::Text(_) - | Event::End(_) => {} + Event::End(ref mut e) => match e.local_name() { + b"Folder" | b"Document" => break, + _ => {} + }, + Event::Decl(_) | Event::CData(_) | Event::Empty(_) | Event::Text(_) => {} Event::Eof => break, _ => return Err(Error::InvalidInput), }; @@ -1118,6 +1118,72 @@ mod tests { assert_eq!(placemark.description, Some("1ΒΌ miles".to_string())); } + #[test] + fn test_parse_sibling_folders() { + let kml_str = r#" + + Folder 1 + + + Folder 2 + + "#; + let f: Kml = kml_str.parse().unwrap(); + assert!(matches!(f, Kml::KmlDocument(_))); + + let doc: Option = match f { + Kml::KmlDocument(d) => Some(d), + _ => None, + }; + let doc = doc.unwrap(); + + assert_eq!(doc.elements.len(), 2); + assert!(doc.elements.iter().all(|e| matches!( + e, + Kml::Folder { + attrs: _, + elements: _ + } + ))); + } + + #[test] + fn test_parse_doc_with_sibling_folders() { + let kml_str = r#" + + + + + Folder 1 + + + Folder 2 + + + + "#; + let f: Kml = kml_str.parse().unwrap(); + assert!(matches!(f, Kml::KmlDocument(_))); + + let elements: Option>> = match f { + Kml::KmlDocument(d) => match &d.elements[0] { + Kml::Document { attrs: _, elements } => Some(elements.to_vec()), + _ => None, + }, + _ => None, + }; + + let elements = elements.unwrap(); + assert_eq!(elements.len(), 2); + assert!(elements.iter().all(|e| matches!( + e, + Kml::Folder { + attrs: _, + elements: _ + } + ))); + } + #[test] fn test_parse() { let kml_str = include_str!("../tests/fixtures/sample.kml");