Skip to content

Commit 1f82df3

Browse files
pkaandelf
authored andcommitted
Move PointType into ewkb module
1 parent ce4df2b commit 1f82df3

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

src/ewkb.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ pub struct PointZM {
4545
pub srid: Option<i32>,
4646
}
4747

48+
#[derive(PartialEq, Clone, Debug)]
49+
pub enum PointType {
50+
Point,
51+
PointZ,
52+
PointM,
53+
PointZM
54+
}
4855

4956
// --- Traits
5057

5158
pub trait EwkbRead: fmt::Debug + Sized {
52-
fn point_type() -> postgis::PointType;
59+
fn point_type() -> PointType;
5360

5461
fn set_srid(&mut self, _srid: Option<i32>) {
5562
}
@@ -69,22 +76,23 @@ pub trait EwkbRead: fmt::Debug + Sized {
6976
fn read_ewkb_body<R: Read>(raw: &mut R, is_be: bool, srid: Option<i32>) -> Result<Self, Error>;
7077
}
7178

79+
7280
pub trait EwkbWrite: fmt::Debug + Sized {
7381
fn opt_srid(&self) -> Option<i32> {
7482
None
7583
}
7684

77-
fn wkb_type_id(point_type: &postgis::PointType, srid: Option<i32>) -> u32 {
85+
fn wkb_type_id(point_type: &PointType, srid: Option<i32>) -> u32 {
7886
let mut type_ = 0;
7987
if srid.is_some() {
8088
type_ |= 0x20000000;
8189
}
82-
if *point_type == postgis::PointType::PointZ ||
83-
*point_type == postgis::PointType::PointZM {
90+
if *point_type == PointType::PointZ ||
91+
*point_type == PointType::PointZM {
8492
type_ |= 0x80000000;
8593
}
86-
if *point_type == postgis::PointType::PointM ||
87-
*point_type == postgis::PointType::PointZM {
94+
if *point_type == PointType::PointM ||
95+
*point_type == PointType::PointZM {
8896
type_ |= 0x40000000;
8997
}
9098
type_
@@ -227,8 +235,8 @@ impl postgis::Point for PointZM {
227235
macro_rules! impl_point_read_traits {
228236
($ptype:ident) => (
229237
impl EwkbRead for $ptype {
230-
fn point_type() -> postgis::PointType {
231-
postgis::PointType::$ptype
238+
fn point_type() -> PointType {
239+
PointType::$ptype
232240
}
233241
fn set_srid(&mut self, srid: Option<i32>) {
234242
self.srid = srid;
@@ -252,7 +260,7 @@ macro_rules! impl_point_read_traits {
252260

253261
impl<'a> AsEwkbPoint<'a> for $ptype {
254262
fn as_ewkb(&'a self) -> EwkbPoint<'a> {
255-
EwkbPoint { geom: self, srid: self.srid, point_type: postgis::PointType::$ptype }
263+
EwkbPoint { geom: self, srid: self.srid, point_type: PointType::$ptype }
256264
}
257265
}
258266
)
@@ -267,7 +275,7 @@ impl_point_read_traits!(PointZM);
267275
pub struct EwkbPoint<'a> {
268276
pub geom: &'a postgis::Point,
269277
pub srid: Option<i32>,
270-
pub point_type: postgis::PointType,
278+
pub point_type: PointType,
271279
}
272280

273281
pub trait AsEwkbPoint<'a> {
@@ -314,7 +322,7 @@ impl EwkbWrite for Point {
314322

315323

316324
macro_rules! point_container_type {
317-
// points container
325+
// geometries containing points
318326
($geotypetrait:ident for $geotype:ident) => (
319327

320328
#[derive(PartialEq, Clone, Debug)]
@@ -358,7 +366,7 @@ macro_rules! point_container_type {
358366
}
359367

360368
macro_rules! geometry_container_type {
361-
// line and polygon containers
369+
// geometries containing lines and polygons
362370
($geotypetrait:ident for $geotype:ident contains $itemtype:ident named $itemname:ident) => (
363371
#[derive(PartialEq, Clone, Debug)]
364372
pub struct $geotype<P: postgis::Point + EwkbRead> {
@@ -408,7 +416,7 @@ macro_rules! impl_read_for_point_container_type {
408416
impl<P> EwkbRead for $geotype<P>
409417
where P: postgis::Point + EwkbRead
410418
{
411-
fn point_type() -> postgis::PointType {
419+
fn point_type() -> PointType {
412420
P::point_type()
413421
}
414422
fn set_srid(&mut self, srid: Option<i32>) {
@@ -429,7 +437,7 @@ macro_rules! impl_read_for_point_container_type {
429437
impl<P> EwkbRead for $geotype<P>
430438
where P: postgis::Point + EwkbRead
431439
{
432-
fn point_type() -> postgis::PointType {
440+
fn point_type() -> PointType {
433441
P::point_type()
434442
}
435443
fn set_srid(&mut self, srid: Option<i32>) {
@@ -452,7 +460,7 @@ macro_rules! impl_read_for_geometry_container_type {
452460
impl<P> EwkbRead for $geotype<P>
453461
where P: postgis::Point + EwkbRead
454462
{
455-
fn point_type() -> postgis::PointType {
463+
fn point_type() -> PointType {
456464
P::point_type()
457465
}
458466
fn set_srid(&mut self, srid: Option<i32>) {
@@ -472,7 +480,7 @@ macro_rules! impl_read_for_geometry_container_type {
472480
impl<P> EwkbRead for $geotype<P>
473481
where P: postgis::Point + EwkbRead
474482
{
475-
fn point_type() -> postgis::PointType {
483+
fn point_type() -> PointType {
476484
P::point_type()
477485
}
478486
fn set_srid(&mut self, srid: Option<i32>) {
@@ -499,7 +507,7 @@ macro_rules! point_container_write {
499507
{
500508
pub geom: &'a postgis::$geotypetrait<'a, ItemType=P, Iter=I>,
501509
pub srid: Option<i32>,
502-
pub point_type: postgis::PointType,
510+
pub point_type: PointType,
503511
}
504512

505513
pub trait $asewkbtype<'a> {
@@ -563,7 +571,7 @@ macro_rules! geometry_container_write {
563571
{
564572
pub geom: &'a postgis::$geotypetrait<'a, ItemType=T, Iter=J>,
565573
pub srid: Option<i32>,
566-
pub point_type: postgis::PointType,
574+
pub point_type: PointType,
567575
}
568576

569577
pub trait $asewkbtype<'a> {
@@ -633,7 +641,7 @@ macro_rules! geometry_container_write {
633641
{
634642
pub geom: &'a postgis::$geotypetrait<'a, ItemType=T, Iter=J>,
635643
pub srid: Option<i32>,
636-
pub point_type: postgis::PointType,
644+
pub point_type: PointType,
637645
}
638646

639647
pub trait $asewkbtype<'a> {
@@ -855,7 +863,7 @@ fn test_multipolygon_write() {
855863
#[test]
856864
fn test_ewkb_adapters() {
857865
let point = Point { x: 10.0, y: -20.0, srid: Some(4326) };
858-
let ewkb = EwkbPoint { geom: &point, srid: Some(4326), point_type: postgis::PointType::Point };
866+
let ewkb = EwkbPoint { geom: &point, srid: Some(4326), point_type: PointType::Point };
859867
assert_eq!(ewkb.to_hex_ewkb(), "0101000020E6100000000000000000244000000000000034C0");
860868
assert_eq!(point.as_ewkb().to_hex_ewkb(), "0101000020E6100000000000000000244000000000000034C0");
861869
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate byteorder;
44

55
mod error;
66
mod types;
7-
pub use types::{Point, LineString, MultiLineString, Polygon};
7+
pub use types::{Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon};
88
pub mod ewkb;
99
pub mod twkb;
1010
mod postgis;

src/twkb.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ pub struct TwkbInfo {
6262

6363
pub trait TwkbGeom: fmt::Debug + Sized {
6464
fn read_twkb<R: Read>(raw: &mut R) -> Result<Self, Error> {
65-
// https://github.com/TWKB/Specification/blob/master/twkb.md
6665
let mut twkb_info: TwkbInfo = Default::default();
6766
// type_and_prec byte
6867
// metadata_header byte
@@ -219,7 +218,7 @@ impl TwkbGeom for Point {
219218

220219
impl<'a> ewkb::AsEwkbPoint<'a> for Point {
221220
fn as_ewkb(&'a self) -> ewkb::EwkbPoint<'a> {
222-
ewkb::EwkbPoint { geom: self, srid: None, point_type: postgis::PointType::Point }
221+
ewkb::EwkbPoint { geom: self, srid: None, point_type: ewkb::PointType::Point }
223222
}
224223
}
225224

@@ -258,7 +257,7 @@ impl<'a> ewkb::AsEwkbLineString<'a> for LineString {
258257
type PointType = Point;
259258
type Iter = Iter<'a, Point>;
260259
fn as_ewkb(&'a self) -> ewkb::EwkbLineString<'a, Self::PointType, Self::Iter> {
261-
ewkb::EwkbLineString { geom: self, srid: None, point_type: postgis::PointType::Point }
260+
ewkb::EwkbLineString { geom: self, srid: None, point_type: ewkb::PointType::Point }
262261
}
263262
}
264263

@@ -312,7 +311,7 @@ impl<'a> ewkb::AsEwkbPolygon<'a> for Polygon {
312311
type ItemType = LineString;
313312
type Iter = Iter<'a, Self::ItemType>;
314313
fn as_ewkb(&'a self) -> ewkb::EwkbPolygon<'a, Self::PointType, Self::PointIter, Self::ItemType, Self::Iter> {
315-
ewkb::EwkbPolygon { geom: self, srid: None, point_type: postgis::PointType::Point }
314+
ewkb::EwkbPolygon { geom: self, srid: None, point_type: ewkb::PointType::Point }
316315
}
317316
}
318317

@@ -359,7 +358,7 @@ impl<'a> ewkb::AsEwkbMultiPoint<'a> for MultiPoint {
359358
type PointType = Point;
360359
type Iter = Iter<'a, Point>;
361360
fn as_ewkb(&'a self) -> ewkb::EwkbMultiPoint<'a, Self::PointType, Self::Iter> {
362-
ewkb::EwkbMultiPoint { geom: self, srid: None, point_type: postgis::PointType::Point }
361+
ewkb::EwkbMultiPoint { geom: self, srid: None, point_type: ewkb::PointType::Point }
363362
}
364363
}
365364

@@ -416,7 +415,7 @@ impl<'a> ewkb::AsEwkbMultiLineString<'a> for MultiLineString {
416415
type ItemType = LineString;
417416
type Iter = Iter<'a, Self::ItemType>;
418417
fn as_ewkb(&'a self) -> ewkb::EwkbMultiLineString<'a, Self::PointType, Self::PointIter, Self::ItemType, Self::Iter> {
419-
ewkb::EwkbMultiLineString { geom: self, srid: None, point_type: postgis::PointType::Point }
418+
ewkb::EwkbMultiLineString { geom: self, srid: None, point_type: ewkb::PointType::Point }
420419
}
421420
}
422421

@@ -488,7 +487,7 @@ impl<'a> ewkb::AsEwkbMultiPolygon<'a> for MultiPolygon {
488487
type ItemType = Polygon;
489488
type Iter = Iter<'a, Self::ItemType>;
490489
fn as_ewkb(&'a self) -> ewkb::EwkbMultiPolygon<'a, Self::PointType, Self::PointIter, Self::LineType, Self::LineIter, Self::ItemType, Self::Iter> {
491-
ewkb::EwkbMultiPolygon { geom: self, srid: None, point_type: postgis::PointType::Point }
490+
ewkb::EwkbMultiPolygon { geom: self, srid: None, point_type: ewkb::PointType::Point }
492491
}
493492
}
494493

src/types.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,3 @@ pub trait MultiPolygon<'a> {
3838
type Iter: Iterator<Item=&'a Self::ItemType>;
3939
fn polygons(&'a self) -> Self::Iter;
4040
}
41-
42-
// --- Adapter structs and traits for EWKB output
43-
44-
#[derive(PartialEq, Clone, Debug)]
45-
pub enum PointType {
46-
Point,
47-
PointZ,
48-
PointM,
49-
PointZM
50-
}

0 commit comments

Comments
 (0)