From d7ce75aa5efefab2e9f00beedea464f2f6596981 Mon Sep 17 00:00:00 2001 From: Kostia Rybnikov Date: Fri, 27 Oct 2023 17:43:41 +0300 Subject: [PATCH 1/4] Support comments Also: improve error reporting --- .gitignore | 1 + Cargo.toml | 2 +- src/errors.rs | 2 +- src/kmz_reader.rs | 8 +- src/reader.rs | 21 +++++- tests/fixtures/style-merging.kml | 126 +++++++++++++++++++++++++++++++ tests/test_roundtrip.rs | 1 + 7 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/style-merging.kml diff --git a/.gitignore b/.gitignore index d0a65ef..1a9e589 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /Cargo.lock **/*.rs.bk +rusty-tags.emacs diff --git a/Cargo.toml b/Cargo.toml index cb646a4..4449903 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ keywords = ["geo", "geospatial", "kml"] exclude = [".github/*"] [dependencies] -quick-xml = "0.28" +quick-xml = "0.31" num-traits = "0.2" thiserror = "1.0" geo-types = { version = ">=0.6, <0.8", optional = true } diff --git a/src/errors.rs b/src/errors.rs index ad2271d..679d977 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -5,7 +5,7 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum Error { #[error("Invalid input supplied for XML")] - InvalidInput, + InvalidInput(String), #[error("Encountered malformed XML: {0}")] MalformedXml(#[from] quick_xml::Error), #[error("Invalid XML event: {0}")] diff --git a/src/kmz_reader.rs b/src/kmz_reader.rs index 34ede0f..47aabfb 100644 --- a/src/kmz_reader.rs +++ b/src/kmz_reader.rs @@ -36,7 +36,9 @@ where // Should parse the first file with a KML extension for i in 0..archive.len() { - let mut kml_file = archive.by_index(i).map_err(|_| Error::InvalidInput)?; + let mut kml_file = archive + .by_index(i) + .map_err(|e| Error::InvalidInput(format!("{e:?}")))?; if !kml_file.name().to_ascii_lowercase().ends_with(".kml") { continue; } @@ -45,7 +47,9 @@ where return Ok(KmlReader::from_reader(Cursor::new(buf))); } - Err(Error::InvalidInput) + Err(Error::InvalidInput( + "Archive contains no elements".to_string(), + )) } } diff --git a/src/reader.rs b/src/reader.rs index 49ce57a..57e7c55 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -194,7 +194,8 @@ where }, Event::Decl(_) | Event::CData(_) | Event::Empty(_) | Event::Text(_) => {} Event::Eof => break, - _ => return Err(Error::InvalidInput), + Event::Comment(_) => {} + x => return Err(Error::InvalidInput(format!("{:?}", x))), }; } @@ -812,7 +813,7 @@ where simple_array_data.name = name; simple_array_data.attrs = attrs; } else { - return Err(Error::InvalidInput); + return Err(Error::InvalidInput("Required \"name\" attribute not present".to_string())); } loop { @@ -848,7 +849,7 @@ where attrs, }) } else { - Err(Error::InvalidInput) + Err(Error::InvalidInput("Required \"name\" attribute not present".to_string())) } } @@ -1684,4 +1685,18 @@ mod tests { Kml::KmlDocument(_) )) } + + #[test] + fn test_parse_style_merging() { + let kml_str = include_str!("../tests/fixtures/style-merging.kml"); + let res = Kml::::from_str(kml_str); + if let Err(e) = res { + println!("Error: {:?}", e); + } + + assert!(matches!( + Kml::::from_str(kml_str).unwrap(), + Kml::KmlDocument(_) + )) + } } diff --git a/tests/fixtures/style-merging.kml b/tests/fixtures/style-merging.kml new file mode 100644 index 0000000..6f5c30d --- /dev/null +++ b/tests/fixtures/style-merging.kml @@ -0,0 +1,126 @@ + + + + 1 + + + + Wideawake field + 1 + + Airport + Is this the world's most remote airfield? + #globalStyles + + + + -14.392095,-7.967700,0 + + + + runway + #globalStyles + + + 1 + -14.405821,-7.963539 -14.381448,-7.975707 + + + + apron + #globalStyles + + + 1 + + + + -14.405214,-7.968981 + -14.402106,-7.970539 + -14.399850,-7.968762 + -14.399670,-7.968411 + -14.403875,-7.966311 + -14.405214,-7.968981 + + + + + + + + diff --git a/tests/test_roundtrip.rs b/tests/test_roundtrip.rs index 695e04b..48fcbd8 100644 --- a/tests/test_roundtrip.rs +++ b/tests/test_roundtrip.rs @@ -30,6 +30,7 @@ mod roundtrip_tests { test_polygon: "polygon.kml", test_sample: "sample.kml", test_countries: "countries.kml", + test_style_merging: "style-merging.kml", } // Confirms that parsing from KML and writing back doesn't drop any currently tracked data From 039afac7715f0be66d2ef842859acb156c4ee5f9 Mon Sep 17 00:00:00 2001 From: Kostia Rybnikov Date: Sun, 29 Oct 2023 18:06:37 +0200 Subject: [PATCH 2/4] fmt --- src/reader.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 57e7c55..aebb582 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -813,7 +813,9 @@ where simple_array_data.name = name; simple_array_data.attrs = attrs; } else { - return Err(Error::InvalidInput("Required \"name\" attribute not present".to_string())); + return Err(Error::InvalidInput( + "Required \"name\" attribute not present".to_string(), + )); } loop { @@ -849,7 +851,9 @@ where attrs, }) } else { - Err(Error::InvalidInput("Required \"name\" attribute not present".to_string())) + Err(Error::InvalidInput( + "Required \"name\" attribute not present".to_string(), + )) } } @@ -1689,14 +1693,13 @@ mod tests { #[test] fn test_parse_style_merging() { let kml_str = include_str!("../tests/fixtures/style-merging.kml"); - let res = Kml::::from_str(kml_str); - if let Err(e) = res { - println!("Error: {:?}", e); - } + let res = Kml::::from_str(kml_str).unwrap(); + println!("{res:#?}"); assert!(matches!( Kml::::from_str(kml_str).unwrap(), Kml::KmlDocument(_) - )) + )); + assert_eq!(true, false); } } From 54976c43eef76386d7cec469478e612d51b006ad Mon Sep 17 00:00:00 2001 From: Kostia Rybnikov Date: Sun, 29 Oct 2023 18:08:09 +0200 Subject: [PATCH 3/4] remove hash --- src/reader.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index aebb582..206824a 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1349,7 +1349,7 @@ mod tests { #[test] fn test_read_schema_data() { - let kml_str = r###" + let kml_str = r##" Pi in the sky 3.14159 @@ -1361,7 +1361,7 @@ mod tests { 181 - "###; + "##; let a: Kml = kml_str.parse().unwrap(); assert_eq!( From 3591ad0ff750ce377c69a557c052b1e540673215 Mon Sep 17 00:00:00 2001 From: Kostia Rybnikov Date: Mon, 30 Oct 2023 10:19:45 +0200 Subject: [PATCH 4/4] remove assert and print --- src/reader.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 206824a..4f5e4b3 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1693,13 +1693,10 @@ mod tests { #[test] fn test_parse_style_merging() { let kml_str = include_str!("../tests/fixtures/style-merging.kml"); - let res = Kml::::from_str(kml_str).unwrap(); - println!("{res:#?}"); assert!(matches!( Kml::::from_str(kml_str).unwrap(), Kml::KmlDocument(_) )); - assert_eq!(true, false); } }