1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
use libc::{c_int, size_t};

// use std::convert::TryFrom;
// TODO: remove this implementation when 1.34 is released.
pub trait TryFrom<T>: Sized {
    type Error;
    fn try_from(value: T) -> Result<Self, Self::Error>;
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum CoordDimensions {
    OneD,
    TwoD,
    ThreeD,
}

impl From<u32> for CoordDimensions {
    fn from(dimensions: u32) -> Self {
        match dimensions {
            1 => CoordDimensions::OneD,
            2 => CoordDimensions::TwoD,
            3 => CoordDimensions::ThreeD,
            _ => panic!("dimensions must be >= 1 and <= 3"),
        }
    }
}

impl TryFrom<u32> for CoordDimensions {
    type Error = &'static str;

    fn try_from(dimensions: u32) -> Result<Self, Self::Error> {
        match dimensions {
            1 => Ok(CoordDimensions::OneD),
            2 => Ok(CoordDimensions::TwoD),
            3 => Ok(CoordDimensions::ThreeD),
            _ => Err("dimensions must be >= 1 and <= 3"),
        }
    }
}

impl Into<u32> for CoordDimensions {
    fn into(self) -> u32 {
        match self {
            CoordDimensions::OneD => 1,
            CoordDimensions::TwoD => 2,
            CoordDimensions::ThreeD => 3,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum Dimensions {
    TwoD,
    ThreeD,
    Other(u32),
}

impl From<c_int> for Dimensions {
    fn from(dimensions: c_int) -> Self {
        match dimensions {
            2 => Dimensions::TwoD,
            3 => Dimensions::ThreeD,
            x if x > 3 => Dimensions::Other(x as _),
            _ => panic!("dimensions must be > 1"),
        }
    }
}

impl TryFrom<c_int> for Dimensions {
    type Error = &'static str;

    fn try_from(dimensions: c_int) -> Result<Self, Self::Error> {
        match dimensions {
            2 => Ok(Dimensions::TwoD),
            3 => Ok(Dimensions::ThreeD),
            x if x > 3 => Ok(Dimensions::Other(x as _)),
            _ => Err("dimensions must be > 1"),
        }
    }
}

impl Into<c_int> for Dimensions {
    fn into(self) -> c_int {
        match self {
            Dimensions::TwoD => 2,
            Dimensions::ThreeD => 3,
            Dimensions::Other(dim) => dim as _,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum OutputDimension {
    TwoD,
    ThreeD,
}

impl From<c_int> for OutputDimension {
    fn from(dimensions: c_int) -> Self {
        match dimensions {
            2 => OutputDimension::TwoD,
            3 => OutputDimension::ThreeD,
            _ => panic!("dimension must be 2 or 3"),
        }
    }
}

impl TryFrom<c_int> for OutputDimension {
    type Error = &'static str;

    fn try_from(dimensions: c_int) -> Result<Self, Self::Error> {
        match dimensions {
            2 => Ok(OutputDimension::TwoD),
            3 => Ok(OutputDimension::ThreeD),
            _ => Err("dimension must be 2 or 3"),
        }
    }
}

impl Into<c_int> for OutputDimension {
    fn into(self) -> c_int {
        match self {
            OutputDimension::TwoD => 2,
            OutputDimension::ThreeD => 3,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum ByteOrder {
    BigEndian,
    LittleEndian,
}

impl From<c_int> for ByteOrder {
    fn from(order: c_int) -> Self {
        match order {
            0 => ByteOrder::BigEndian,
            _ => ByteOrder::LittleEndian,
        }
    }
}

impl TryFrom<c_int> for ByteOrder {
    type Error = &'static str;

    fn try_from(order: c_int) -> Result<Self, Self::Error> {
        match order {
            0 => Ok(ByteOrder::BigEndian),
            _ => Ok(ByteOrder::LittleEndian),
        }
    }
}

impl Into<c_int> for ByteOrder {
    fn into(self) -> c_int {
        match self {
            ByteOrder::BigEndian => 0,
            ByteOrder::LittleEndian => 1,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
#[repr(C)]
pub enum GeometryTypes {
    Point,
    LineString,
    LinearRing,
    Polygon,
    MultiPoint,
    MultiLineString,
    MultiPolygon,
    GeometryCollection,
    #[doc(hidden)]
    __Unknonwn(u32),
}

impl From<c_int> for GeometryTypes {
    fn from(dimensions: c_int) -> Self {
        match dimensions {
            0 => GeometryTypes::Point,
            1 => GeometryTypes::LineString,
            2 => GeometryTypes::LinearRing,
            3 => GeometryTypes::Polygon,
            4 => GeometryTypes::MultiPoint,
            5 => GeometryTypes::MultiLineString,
            6 => GeometryTypes::MultiPolygon,
            7 => GeometryTypes::GeometryCollection,
            x => GeometryTypes::__Unknonwn(x as _),
        }
    }
}

impl TryFrom<c_int> for GeometryTypes {
    type Error = &'static str;

    fn try_from(dimensions: c_int) -> Result<Self, Self::Error> {
        match dimensions {
            0 => Ok(GeometryTypes::Point),
            1 => Ok(GeometryTypes::LineString),
            2 => Ok(GeometryTypes::LinearRing),
            3 => Ok(GeometryTypes::Polygon),
            4 => Ok(GeometryTypes::MultiPoint),
            5 => Ok(GeometryTypes::MultiLineString),
            6 => Ok(GeometryTypes::MultiPolygon),
            7 => Ok(GeometryTypes::GeometryCollection),
            x => Ok(GeometryTypes::__Unknonwn(x as _)),
        }
    }
}

impl Into<c_int> for GeometryTypes {
    fn into(self) -> c_int {
        match self {
            GeometryTypes::Point => 0,
            GeometryTypes::LineString => 1,
            GeometryTypes::LinearRing => 2,
            GeometryTypes::Polygon => 3,
            GeometryTypes::MultiPoint => 4,
            GeometryTypes::MultiLineString => 5,
            GeometryTypes::MultiPolygon => 6,
            GeometryTypes::GeometryCollection => 7,
            GeometryTypes::__Unknonwn(x) => x as _,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum Orientation {
    /// If reaching P takes a counter-clockwise (left) turn.
    CounterClockwise,
    /// If reaching P takes a clockwise (right) turn
    Clockwise,
    /// if P is collinear with A-B.
    Colinear,
}

impl From<c_int> for Orientation {
    fn from(orientation: c_int) -> Self {
        match orientation {
            -1 => Orientation::CounterClockwise,
            0 => Orientation::Clockwise,
            1 => Orientation::Colinear,
            _ => panic!("invalid value for Orientation!"),
        }
    }
}

impl TryFrom<c_int> for Orientation {
    type Error = &'static str;

    fn try_from(orientation: c_int) -> Result<Self, Self::Error> {
        match orientation {
            -1 => Ok(Orientation::CounterClockwise),
            0 => Ok(Orientation::Clockwise),
            1 => Ok(Orientation::Colinear),
            _ => Err("value must be -1, 0 or 1"),
        }
    }
}

impl Into<c_int> for Orientation {
    fn into(self) -> c_int {
        match self {
            Orientation::CounterClockwise => -1,
            Orientation::Clockwise => 0,
            Orientation::Colinear => 1,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum Ordinate {
    X,
    Y,
    Z,
}

impl From<size_t> for Ordinate {
    fn from(ordinate: size_t) -> Self {
        match ordinate {
            0 => Ordinate::X,
            1 => Ordinate::Y,
            2 => Ordinate::Z,
            _ => panic!("ordinate must be >= 0 and <= 2"),
        }
    }
}

impl TryFrom<size_t> for Ordinate {
    type Error = &'static str;

    fn try_from(ordinate: size_t) -> Result<Self, Self::Error> {
        match ordinate {
            0 => Ok(Ordinate::X),
            1 => Ok(Ordinate::Y),
            2 => Ok(Ordinate::Z),
            _ => Err("ordinate value must be >= 0 and <= 2"),
        }
    }
}

impl Into<u32> for Ordinate {
    fn into(self) -> u32 {
        match self {
            Ordinate::X => 0,
            Ordinate::Y => 1,
            Ordinate::Z => 2,
        }
    }
}

#[cfg(any(feature = "v3_6_0", feature = "dox"))]
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum Precision {
    NoTopo,
    KeepCollapsed,
}

#[cfg(any(feature = "v3_6_0", feature = "dox"))]
impl From<c_int> for Precision {
    fn from(order: c_int) -> Self {
        match order {
            1 => Precision::NoTopo,
            2 => Precision::KeepCollapsed,
            x => panic!("Unknown precision type {}", x),
        }
    }
}

#[cfg(any(feature = "v3_6_0", feature = "dox"))]
impl TryFrom<c_int> for Precision {
    type Error = &'static str;

    fn try_from(order: c_int) -> Result<Self, Self::Error> {
        match order {
            1 => Ok(Precision::NoTopo),
            2 => Ok(Precision::KeepCollapsed),
            _ => Err("Unknown precision type"),
        }
    }
}

#[cfg(any(feature = "v3_6_0", feature = "dox"))]
impl Into<c_int> for Precision {
    fn into(self) -> c_int {
        match self {
            Precision::NoTopo => 0,
            Precision::KeepCollapsed => 1,
        }
    }
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum JoinStyle {
    Round,
    Mitre,
    Bevel,
}

impl From<c_int> for JoinStyle {
    fn from(join_style: c_int) -> Self {
        match join_style {
            1 => JoinStyle::Round,
            2 => JoinStyle::Mitre,
            3 => JoinStyle::Bevel,
            _ => panic!("Unknown join style"),
        }
    }
}

impl TryFrom<c_int> for JoinStyle {
    type Error = &'static str;

    fn try_from(join_style: c_int) -> Result<Self, Self::Error> {
        match join_style {
            1 => Ok(JoinStyle::Round),
            2 => Ok(JoinStyle::Mitre),
            3 => Ok(JoinStyle::Bevel),
            _ => Err("Unknown join style"),
        }
    }
}

impl Into<c_int> for JoinStyle {
    fn into(self) -> c_int {
        match self {
            JoinStyle::Round => 1,
            JoinStyle::Mitre => 2,
            JoinStyle::Bevel => 3,
        }
    }
}