From c7cc0c763f418cf463261286757784425f3e352c Mon Sep 17 00:00:00 2001 From: pjsier Date: Sun, 22 Dec 2024 13:52:26 -0500 Subject: [PATCH] refactor: deprecate quick collection for TryFrom --- src/conversion.rs | 119 +++++++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 48 deletions(-) diff --git a/src/conversion.rs b/src/conversion.rs index 166e36c..c60c960 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -310,56 +310,75 @@ where } } -fn process_kml(k: Kml) -> Result>, Error> +#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))] +impl TryFrom> for Vec> where T: CoordType, { - match k { - Kml::KmlDocument(d) => Ok(d - .elements - .into_iter() - .flat_map(process_kml) - .flatten() - .collect()), - Kml::Point(p) => Ok(vec![ - geo_types::Geometry::Point(geo_types::Point::from(p)); - 1 - ]), - Kml::LineString(l) => Ok(vec![ - geo_types::Geometry::LineString( - geo_types::LineString::from(l), - ); - 1 - ]), - Kml::LinearRing(l) => Ok(vec![ - geo_types::Geometry::LineString( - geo_types::LineString::from(l), - ); - 1 - ]), - Kml::Polygon(p) => Ok(vec![ - geo_types::Geometry::Polygon(geo_types::Polygon::from( - p - )); - 1 - ]), - Kml::MultiGeometry(g) => Ok(geo_types::GeometryCollection::try_from(g)?.0), - Kml::Placemark(p) => Ok(if let Some(g) = p.geometry { - vec![geo_types::Geometry::try_from(g)?; 1] - } else { - vec![] - }), - Kml::Document { elements, .. } => Ok(elements - .into_iter() - .flat_map(process_kml) - .flatten() - .collect()), - Kml::Folder { elements, .. } => Ok(elements - .into_iter() - .flat_map(process_kml) - .flatten() - .collect()), - _ => Ok(vec![]), + type Error = Error; + + fn try_from(k: Kml) -> Result>, Self::Error> { + match k { + Kml::KmlDocument(d) => Ok(d + .elements + .into_iter() + .flat_map(Vec::>::try_from) + .flatten() + .collect::>>()), + Kml::Point(p) => Ok(vec![ + geo_types::Geometry::Point(geo_types::Point::from(p)); + 1 + ]), + Kml::LineString(l) => Ok(vec![ + geo_types::Geometry::LineString( + geo_types::LineString::from(l), + ); + 1 + ]), + Kml::LinearRing(l) => Ok(vec![ + geo_types::Geometry::LineString( + geo_types::LineString::from(l), + ); + 1 + ]), + Kml::Polygon(p) => Ok(vec![ + geo_types::Geometry::Polygon(geo_types::Polygon::from( + p + )); + 1 + ]), + Kml::MultiGeometry(g) => Ok(geo_types::GeometryCollection::try_from(g)?.0), + Kml::Placemark(p) => Ok(if let Some(g) = p.geometry { + vec![geo_types::Geometry::try_from(g)?; 1] + } else { + vec![] + }), + Kml::Document { elements, .. } => Ok(elements + .into_iter() + .flat_map(Vec::>::try_from) + .flatten() + .collect()), + Kml::Folder { elements, .. } => Ok(elements + .into_iter() + .flat_map(Vec::>::try_from) + .flatten() + .collect()), + _ => Ok(vec![]), + } + } +} + +#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))] +impl TryFrom> for geo_types::GeometryCollection +where + T: CoordType, +{ + type Error = Error; + + fn try_from(k: Kml) -> Result, Self::Error> { + Ok(geo_types::GeometryCollection( + Vec::>::try_from(k)?, + )) } } @@ -387,12 +406,16 @@ where /// // Turn the KML string into a geo_types GeometryCollection /// let mut collection: GeometryCollection = quick_collection(k).unwrap(); /// ``` +#[deprecated( + since = "0.8.7", + note = "use `geo_types::GeometryCollection::try_from(&k)` instead" +)] #[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))] pub fn quick_collection(k: Kml) -> Result, Error> where T: CoordType, { - Ok(geo_types::GeometryCollection(process_kml(k)?)) + geo_types::GeometryCollection::try_from(k) } #[cfg(test)]