Skip to content

Commit ec2fb25

Browse files
authored
refactor: deprecate quick collection for TryFrom (#64)
* refactor: deprecate quick collection for TryFrom * style: update test to use tryfrom * style: allow deprecated where exported * docs: remove quick_collection from examples
1 parent 02723ff commit ec2fb25

File tree

3 files changed

+81
-56
lines changed

3 files changed

+81
-56
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ writer.write(&kml).unwrap();
6666

6767
```rust
6868
use geo_types::{self, GeometryCollection};
69-
use kml::{quick_collection, Kml, types::Point};
69+
use kml::{Kml, types::Point};
7070

7171
let kml_point = Point::new(1., 1., None);
7272
// Convert into geo_types primitives
@@ -87,8 +87,7 @@ let kml_folder_str = r#"
8787
</Folder>"#;
8888
let kml_folder: Kml<f64> = kml_folder_str.parse().unwrap();
8989

90-
// Use the quick_collection helper to convert Kml to a geo_types::GeometryCollection
91-
let geom_coll: GeometryCollection<f64> = quick_collection(kml_folder).unwrap();
90+
let geom_coll: GeometryCollection<f64> = kml_folder.try_into().unwrap();
9291
```
9392

9493
## Code of Conduct

src/conversion.rs

+76-50
Original file line numberDiff line numberDiff line change
@@ -310,56 +310,75 @@ where
310310
}
311311
}
312312

313-
fn process_kml<T>(k: Kml<T>) -> Result<Vec<geo_types::Geometry<T>>, Error>
313+
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
314+
impl<T> TryFrom<Kml<T>> for Vec<geo_types::Geometry<T>>
314315
where
315316
T: CoordType,
316317
{
317-
match k {
318-
Kml::KmlDocument(d) => Ok(d
319-
.elements
320-
.into_iter()
321-
.flat_map(process_kml)
322-
.flatten()
323-
.collect()),
324-
Kml::Point(p) => Ok(vec![
325-
geo_types::Geometry::Point(geo_types::Point::from(p));
326-
1
327-
]),
328-
Kml::LineString(l) => Ok(vec![
329-
geo_types::Geometry::LineString(
330-
geo_types::LineString::from(l),
331-
);
332-
1
333-
]),
334-
Kml::LinearRing(l) => Ok(vec![
335-
geo_types::Geometry::LineString(
336-
geo_types::LineString::from(l),
337-
);
338-
1
339-
]),
340-
Kml::Polygon(p) => Ok(vec![
341-
geo_types::Geometry::Polygon(geo_types::Polygon::from(
342-
p
343-
));
344-
1
345-
]),
346-
Kml::MultiGeometry(g) => Ok(geo_types::GeometryCollection::try_from(g)?.0),
347-
Kml::Placemark(p) => Ok(if let Some(g) = p.geometry {
348-
vec![geo_types::Geometry::try_from(g)?; 1]
349-
} else {
350-
vec![]
351-
}),
352-
Kml::Document { elements, .. } => Ok(elements
353-
.into_iter()
354-
.flat_map(process_kml)
355-
.flatten()
356-
.collect()),
357-
Kml::Folder { elements, .. } => Ok(elements
358-
.into_iter()
359-
.flat_map(process_kml)
360-
.flatten()
361-
.collect()),
362-
_ => Ok(vec![]),
318+
type Error = Error;
319+
320+
fn try_from(k: Kml<T>) -> Result<Vec<geo_types::Geometry<T>>, Self::Error> {
321+
match k {
322+
Kml::KmlDocument(d) => Ok(d
323+
.elements
324+
.into_iter()
325+
.flat_map(Vec::<geo_types::Geometry<T>>::try_from)
326+
.flatten()
327+
.collect::<Vec<geo_types::Geometry<T>>>()),
328+
Kml::Point(p) => Ok(vec![
329+
geo_types::Geometry::Point(geo_types::Point::from(p));
330+
1
331+
]),
332+
Kml::LineString(l) => Ok(vec![
333+
geo_types::Geometry::LineString(
334+
geo_types::LineString::from(l),
335+
);
336+
1
337+
]),
338+
Kml::LinearRing(l) => Ok(vec![
339+
geo_types::Geometry::LineString(
340+
geo_types::LineString::from(l),
341+
);
342+
1
343+
]),
344+
Kml::Polygon(p) => Ok(vec![
345+
geo_types::Geometry::Polygon(geo_types::Polygon::from(
346+
p
347+
));
348+
1
349+
]),
350+
Kml::MultiGeometry(g) => Ok(geo_types::GeometryCollection::try_from(g)?.0),
351+
Kml::Placemark(p) => Ok(if let Some(g) = p.geometry {
352+
vec![geo_types::Geometry::try_from(g)?; 1]
353+
} else {
354+
vec![]
355+
}),
356+
Kml::Document { elements, .. } => Ok(elements
357+
.into_iter()
358+
.flat_map(Vec::<geo_types::Geometry<T>>::try_from)
359+
.flatten()
360+
.collect()),
361+
Kml::Folder { elements, .. } => Ok(elements
362+
.into_iter()
363+
.flat_map(Vec::<geo_types::Geometry<T>>::try_from)
364+
.flatten()
365+
.collect()),
366+
_ => Ok(vec![]),
367+
}
368+
}
369+
}
370+
371+
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
372+
impl<T> TryFrom<Kml<T>> for geo_types::GeometryCollection<T>
373+
where
374+
T: CoordType,
375+
{
376+
type Error = Error;
377+
378+
fn try_from(k: Kml<T>) -> Result<geo_types::GeometryCollection<T>, Self::Error> {
379+
Ok(geo_types::GeometryCollection(
380+
Vec::<geo_types::Geometry<T>>::try_from(k)?,
381+
))
363382
}
364383
}
365384

@@ -387,12 +406,16 @@ where
387406
/// // Turn the KML string into a geo_types GeometryCollection
388407
/// let mut collection: GeometryCollection<f64> = quick_collection(k).unwrap();
389408
/// ```
409+
#[deprecated(
410+
since = "0.8.7",
411+
note = "use `geo_types::GeometryCollection::try_from(&k)` instead"
412+
)]
390413
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
391414
pub fn quick_collection<T>(k: Kml<T>) -> Result<geo_types::GeometryCollection<T>, Error>
392415
where
393416
T: CoordType,
394417
{
395-
Ok(geo_types::GeometryCollection(process_kml(k)?))
418+
geo_types::GeometryCollection::try_from(k)
396419
}
397420

398421
#[cfg(test)]
@@ -402,7 +425,7 @@ mod tests {
402425
use std::collections::HashMap;
403426

404427
#[test]
405-
fn test_quick_collection() {
428+
fn test_try_from_collection() {
406429
let k = KmlDocument {
407430
elements: vec![
408431
Kml::Point(Point::from(Coord::from((1., 1.)))),
@@ -425,6 +448,9 @@ mod tests {
425448
geo_types::Geometry::LineString(geo_types::LineString::from(vec![(1., 1.), (2., 2.)])),
426449
geo_types::Geometry::Point(geo_types::Point::from((3., 3.))),
427450
]);
428-
assert_eq!(quick_collection(Kml::KmlDocument(k)).unwrap(), gc);
451+
assert_eq!(
452+
geo_types::GeometryCollection::try_from(Kml::KmlDocument(k)).unwrap(),
453+
gc
454+
);
429455
}
430456
}

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
//! ```
6868
//! # #[cfg(feature = "geo-types")] {
6969
//! use geo_types::{self, GeometryCollection};
70-
//! use kml::{quick_collection, Kml, types::Point};
70+
//! use kml::{Kml, types::Point};
7171
//!
7272
//! let kml_point = Point::new(1., 1., None);
7373
//! // Convert into geo_types primitives
@@ -88,8 +88,7 @@
8888
//! </Folder>"#;
8989
//! let kml_folder: Kml<f64> = kml_folder_str.parse().unwrap();
9090
//!
91-
//! // Use the quick_collection helper to convert Kml to a geo_types::GeometryCollection
92-
//! let geom_coll: GeometryCollection<f64> = quick_collection(kml_folder).unwrap();
91+
//! let geom_coll: GeometryCollection<f64> = kml_folder.try_into().unwrap();
9392
//! # }
9493
//! ```
9594
@@ -112,6 +111,7 @@ pub use crate::writer::KmlWriter;
112111
pub mod conversion;
113112

114113
#[cfg(feature = "geo-types")]
114+
#[allow(deprecated)]
115115
pub use conversion::quick_collection;
116116

117117
#[cfg(feature = "zip")]

0 commit comments

Comments
 (0)