Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support comments #58

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/Cargo.lock
**/*.rs.bk
rusty-tags.emacs
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down
8 changes: 6 additions & 2 deletions src/kmz_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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(),
))
}
}

Expand Down
25 changes: 20 additions & 5 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
};
}

Expand Down Expand Up @@ -812,7 +813,9 @@ 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 {
Expand Down Expand Up @@ -848,7 +851,9 @@ where
attrs,
})
} else {
Err(Error::InvalidInput)
Err(Error::InvalidInput(
"Required \"name\" attribute not present".to_string(),
))
}
}

Expand Down Expand Up @@ -1344,7 +1349,7 @@ mod tests {

#[test]
fn test_read_schema_data() {
let kml_str = r###"<SchemaData schemaUrl="#TrailHeadTypeId">
let kml_str = r##"<SchemaData schemaUrl="#TrailHeadTypeId">
<SimpleData name="TrailHeadName" anyAttribute="anySimpleType">Pi in the sky</SimpleData>
<SimpleData name="TrailLength" anyAttribute="anySimpleType">3.14159</SimpleData>
<SimpleArrayData name="cadence" anyAttribute="anySimpleType">
Expand All @@ -1356,7 +1361,7 @@ mod tests {
<SimpleArrayData name="heartrate">
<value>181</value>
</SimpleArrayData>
</SchemaData>"###;
</SchemaData>"##;

let a: Kml = kml_str.parse().unwrap();
assert_eq!(
Expand Down Expand Up @@ -1684,4 +1689,14 @@ mod tests {
Kml::KmlDocument(_)
))
}

#[test]
fn test_parse_style_merging() {
let kml_str = include_str!("../tests/fixtures/style-merging.kml");

assert!(matches!(
Kml::<f64>::from_str(kml_str).unwrap(),
Kml::KmlDocument(_)
));
}
}
126 changes: 126 additions & 0 deletions tests/fixtures/style-merging.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<open>1</open>
<!--

There are two ways of styling data in KML: globally and locally. (In this
sense, "local" means local to the feature.)

A global style must have an XML id and can be referred to from any feature's
<styleUrl>. A local style does not necessarily need an id and applies only
to the feature in which it is contained.

The rules regarding inheritancy, overriding and merging are given by example
in this file.

-->
<Style id="globalStyles">
<!-- A single style specifying an instance of each StyleSelector -->
<IconStyle id="icon">
<Icon>
<href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href>
</Icon>
</IconStyle>
<LabelStyle id="label">
<color>ff0000cc</color>
</LabelStyle>
<LineStyle id="line">
<color>7f0000ff</color>
<width>4</width>
</LineStyle>
<PolyStyle id="poly">
<color>ff0000cc</color>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
<BalloonStyle id="balloon">
<!--
white background, black text, bolded name followed by description
-->
<bgColor>ffffffff</bgColor>
<textColor>ff000000</textColor>
<text>
<![CDATA[<b>$[name]</b><br/><br/>$[description]<br/>]]>
</text>
</BalloonStyle>
<ListStyle id="list">
<listItemType>radioFolder</listItemType>
</ListStyle>
</Style>
<Folder>
<name>Wideawake field</name>
<open>1</open>
<Placemark>
<name>Airport</name>
<description>Is this the world's most remote airfield?</description>
<styleUrl>#globalStyles</styleUrl>
<!-- global style -->
<Style>
<BalloonStyle>
<!--
Local balloon style overrides background and text colors defined
in global style.
-->
<bgColor>ff000000</bgColor>
<textColor>ffffffff</textColor>
<!--
The <text> styling is not specified here and is therefore merged
from the global style.
-->
</BalloonStyle>
</Style>
<Point>
<coordinates>-14.392095,-7.967700,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>runway</name>
<styleUrl>#globalStyles</styleUrl>
<Style>
<LineStyle>
<!--
Coloring is implicitly merged from the global style, width is
explicitly overridden.
-->
<width>10</width>
</LineStyle>
</Style>
<LineString>
<tessellate>1</tessellate>
<coordinates>-14.405821,-7.963539 -14.381448,-7.975707</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>apron</name>
<styleUrl>#globalStyles</styleUrl>
<Style>
<LineStyle>
<!--
Overriding the LineStyle here will change the appearance of the
outline of the polygon. We'll make it leprechaun green and much
thicker. Gross, isn't it?
-->
<color>ff00ff00</color>
<width>16</width>
</LineStyle>
</Style>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-14.405214,-7.968981
-14.402106,-7.970539
-14.399850,-7.968762
-14.399670,-7.968411
-14.403875,-7.966311
-14.405214,-7.968981
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Folder>
</Document>
</kml>
1 change: 1 addition & 0 deletions tests/test_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading