-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
622: Add optional `arbitrary` integration in geo-types. r=michaelkirk a=frewsxcv - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. --- Extracted from #532 If people feel this is too niche, this could also just be a separate crate that lives in this repo or a separate one 🤷🏻 Co-authored-by: Corey Farwell <coreyf@rwell.org>
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use std::mem; | ||
use crate::{ | ||
CoordFloat, Coordinate, Geometry, GeometryCollection, LineString, MultiLineString, MultiPoint, | ||
MultiPolygon, Point, Polygon, Rect, Triangle, | ||
}; | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Coordinate<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
let x = u.arbitrary::<T>()?; | ||
let y = u.arbitrary::<T>()?; | ||
Ok(Coordinate { x, y }) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Point<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
u.arbitrary::<Coordinate<T>>().map(Point) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for LineString<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
let coords = u.arbitrary::<Vec<Coordinate<T>>>()?; | ||
|
||
if coords.len() < 2 { | ||
return Err(arbitrary::Error::IncorrectFormat); | ||
} | ||
|
||
Ok(LineString(coords)) | ||
} | ||
|
||
fn size_hint(_depth: usize) -> (usize, Option<usize>) { | ||
(mem::size_of::<T>() * 2, None) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Polygon<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
Ok(Polygon::new( | ||
u.arbitrary::<LineString<T>>()?, | ||
u.arbitrary::<Vec<LineString<T>>>()?, | ||
)) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for MultiPoint<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
u.arbitrary::<Vec<Point<T>>>().map(MultiPoint) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for MultiLineString<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
u.arbitrary::<Vec<LineString<T>>>().map(MultiLineString) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for MultiPolygon<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
u.arbitrary::<Vec<Polygon<T>>>().map(MultiPolygon) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> | ||
for GeometryCollection<T> | ||
{ | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
u.arbitrary() | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Rect<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
Ok(Rect::new( | ||
u.arbitrary::<Coordinate<T>>()?, | ||
u.arbitrary::<Coordinate<T>>()?, | ||
)) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Triangle<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
Ok(Triangle( | ||
u.arbitrary::<Coordinate<T>>()?, | ||
u.arbitrary::<Coordinate<T>>()?, | ||
u.arbitrary::<Coordinate<T>>()?, | ||
)) | ||
} | ||
} | ||
|
||
impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Geometry<T> { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
let n = u.int_in_range(0..=8)?; | ||
|
||
Ok(match n { | ||
0 => Geometry::Point(u.arbitrary()?), | ||
1 => Geometry::LineString(u.arbitrary()?), | ||
2 => Geometry::Polygon(u.arbitrary()?), | ||
3 => Geometry::MultiPoint(u.arbitrary()?), | ||
4 => Geometry::MultiLineString(u.arbitrary()?), | ||
5 => Geometry::MultiPolygon(u.arbitrary()?), | ||
6 => Geometry::GeometryCollection(u.arbitrary()?), | ||
7 => Geometry::Triangle(u.arbitrary()?), | ||
8 => Geometry::Rect(u.arbitrary()?), | ||
_ => unreachable!(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters