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

Implements From Tuple for point types #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions src/ewkb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ use std::slice::Iter;

// --- Structs for reading PostGIS geometries into

#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Default)]
pub struct Point {
pub x: f64,
pub y: f64,
pub srid: Option<i32>,
}

#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Default)]
pub struct PointZ {
pub x: f64,
pub y: f64,
pub z: f64,
pub srid: Option<i32>,
}

#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Default)]
pub struct PointM {
pub x: f64,
pub y: f64,
pub m: f64,
pub srid: Option<i32>,
}

#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Default)]
pub struct PointZM {
pub x: f64,
pub y: f64,
Expand Down Expand Up @@ -185,6 +185,12 @@ impl Point {
}
}

impl From<(f64, f64)> for Point {
fn from((x, y): (f64, f64)) -> Self {
Self::new(x, y, None)
}
}

impl postgis::Point for Point {
fn x(&self) -> f64 {
self.x
Expand Down Expand Up @@ -214,6 +220,12 @@ impl PointZ {
}
}

impl From<(f64, f64, f64)> for PointZ {
fn from((x, y, z): (f64, f64, f64)) -> Self {
Self::new(x, y, z, None)
}
}

impl postgis::Point for PointZ {
fn x(&self) -> f64 {
self.x
Expand Down Expand Up @@ -246,6 +258,12 @@ impl PointM {
}
}

impl From<(f64, f64, f64)> for PointM {
fn from((x, y, m): (f64, f64, f64)) -> Self {
Self::new(x, y, m, None)
}
}

impl postgis::Point for PointM {
fn x(&self) -> f64 {
self.x
Expand Down Expand Up @@ -279,6 +297,12 @@ impl PointZM {
}
}

impl From<(f64, f64, f64, f64)> for PointZM {
fn from((x, y, z, m): (f64, f64, f64, f64)) -> Self {
Self::new(x, y, z, m, None)
}
}

impl postgis::Point for PointZM {
fn x(&self) -> f64 {
self.x
Expand Down
10 changes: 8 additions & 2 deletions src/twkb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::io::prelude::*;
use std::slice::Iter;
use std::u8;

#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Default)]
pub struct Point {
pub x: f64,
pub y: f64, // TODO: support for z, m
Expand Down Expand Up @@ -200,7 +200,13 @@ fn read_varint64_as_f64<R: Read>(raw: &mut R, precision: i8) -> Result<f64, Erro

impl Point {
fn new_from_opt_vals(x: f64, y: f64, _z: Option<f64>, _m: Option<f64>) -> Self {
Point { x: x, y: y }
Self { x: x, y: y }
}
}

impl From<(f64, f64)> for Point {
fn from((x, y): (f64, f64)) -> Self {
Self { x, y }
}
}

Expand Down