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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// src/codec/mod.rs
//! Native binary serialization for `geometres` types.
//!
//! This module is the authoritative definition of a serialized `geometres`
//! value. It replaces the crate's former optional derive-based
//! serialization, and the format described here is part of the crate's
//! public contract.
//!
//! # Why a native format
//!
//! Not dependency count: the previous mechanism was already optional, so the
//! shipped graph was clean. The properties wanted are **deterministic encoding,
//! low allocation overhead, a byte-slice interface with no I/O, and a path to
//! compact or zero-copy representations**, and no single off-the-shelf format
//! crate provides all four.
//!
//! # The four guarantees
//!
//! **Deterministic.** The same value produces byte-identical output on every
//! run, platform and build. No map iteration, no hash seeding, no float
//! formatting: every `f64` is written with [`f64::to_le_bytes`] and read with
//! [`f64::from_le_bytes`], never through a decimal representation.
//!
//! **Round-trip exact.** Decoding an encoding yields a `PartialEq`-equal value,
//! `f64` bit patterns included. A coordinate that comes back with different
//! bits is a defect, not a tolerance.
//!
//! **Validating on decode.** Every geometry is rebuilt through its public
//! constructor (`Point::new`, `LineString::new`, `LinearRing::new`,
//! `Polygon::new`, `MultiPolygon::new`, `GeometryCollection::new` and the 3D
//! counterparts), and the constructor's error is propagated. No type is ever
//! assembled field-by-field from bytes. `Point`'s field is private so that
//! deserialization cannot bypass `Point::new`, and this decoder honors that.
//! **A malformed or hostile byte stream produces a [`CodecError`], never an
//! invalid geometry and never a panic.**
//!
//! **No I/O.** Encoding appends to a `Vec<u8>` and decoding reads a `&[u8]`;
//! nothing here uses `std::io`. The codec allocates, and like the rest of the
//! crate it is built on `std`: it is not `no_std`.
//!
//! # Format
//!
//! All integers are little-endian. All lengths are explicit `u32` counts, so a
//! decoder never scans for a boundary.
//!
//! ```text
//! document := version:u8 tag:u8 body
//! ```
//!
//! `version` is [`FORMAT_VERSION`]. A document whose version is not recognized
//! is rejected rather than guessed. The field exists from byte zero so the
//! format can change without orphaning stored artifacts.
//!
//! `tag` identifies the top-level type ([`tag`] module). An unrecognized tag is
//! an error, and so is a tag that does not match the type being decoded.
//!
//! ## Primitives
//!
//! | element | encoding |
//! |---|---|
//! | `f64` | 8 bytes, `to_le_bytes` |
//! | count | `u32`, 4 bytes little-endian |
//! | `Coord2d` | `x:f64 y:f64` |
//! | `Coord3d` | `x:f64 y:f64 z:f64` |
//! | coordinate sequence | `count:u32` then `count` coordinates |
//!
//! ## Bodies
//!
//! | type | body |
//! |---|---|
//! | `Point`, `Point3d` | one coordinate |
//! | `LineString`, `LineString3d` | coordinate sequence |
//! | `LinearRing`, `LinearRing3d` | coordinate sequence; see the closure note |
//! | `Polygon`, `Polygon3d` | exterior sequence, `holes:u32`, then each hole's sequence |
//! | `Envelope` | `min_x:f64 min_y:f64 max_x:f64 max_y:f64` |
//! | `MultiPoint3d` | `count:u32` then each point's body |
//! | `MultiLineString3d` | `count:u32` then each line string's body |
//! | `MultiPolygon3d` | `count:u32` then each polygon's body (tag `0x28`) |
//! | `MultiPoint` | `count:u32` then each point's body |
//! | `MultiLineString` | `count:u32` then each line string's body |
//! | `MultiPolygon` | `count:u32` then each polygon's body |
//! | `Geometry` | `variant:u8` then that variant's body |
//! | `GeometryCollection` | `count:u32` then each element as a `Geometry` body (tag `0x11`) |
//! | `Location` | `u8`: 0 interior, 1 boundary, 2 exterior |
//! | `Dimension` | `i8`: −1 empty, 0 point, 1 line, 2 area (the OGC values) |
//! | `IntersectionMatrix` | nine `Dimension` values in row-major DE-9IM order |
//! | `TessellateMethod` | `u8`: 0 square, 1 hex, 2 hex-trans, 3 diamond |
//!
//! The `Geometry` variant byte reuses the top-level [`tag`] values, so one tag
//! space serves both.
//!
//! ## Collections and their depth
//!
//! A `GeometryCollection`'s elements are `Geometry` bodies, so a collection
//! nests by writing variant `0x11` with a collection body. The nesting is at
//! most `GeometryCollection::MAX_NESTING` (32) levels, a permanent part of
//! the format: the decoder refuses a deeper header with
//! [`CodecError::NestingTooDeep`] before reading inside it, and the type
//! refuses a deeper value at construction, so every collection that exists
//! round-trips and no stream can nest the decoder past the limit.
//! `Geometry` is `#[non_exhaustive]`; an unknown variant byte is
//! [`CodecError::UnknownVariant`], never a silently skipped field.
//!
//! ## Ring closure, stated explicitly
//!
//! **A `LinearRing` is encoded as its `n` distinct vertices with closure
//! implied. The encoder does not write a closing vertex and the decoder
//! refuses one** (as the duplicate pair across the closing edge,
//! `GeometryError::ConsecutiveDuplicateVertices`). Refusing it, rather than
//! stripping it as `LinearRing::new` strips wire-format input, keeps every
//! value's encoding unique: whatever decodes re-encodes to the bytes read.
//!
//! This is spelled out because the opposite convention produces conversion
//! bugs at format boundaries: shapefiles and OGC WKT repeat the first vertex
//! at the end, `LinearRing::new` strips it, and a round trip that re-appends
//! it grows the ring on every pass. An independent implementation of this
//! format must not write the closing vertex.
//!
//! # What is not encoded
//!
//! The result wrappers [`Unvouched<T>`](crate::types::Unvouched) and
//! [`MaybeVouched<T, U>`](crate::types::MaybeVouched) have **no encoding**,
//! and that is a design decision rather than an omission. They describe the
//! outcome of a computation, an invalid result, and a stored artifact has
//! no outcome to describe. Encoding one would also create a route by which an
//! invalid geometry could be written to a file and read back as though it
//! were a value worth keeping, which is the opposite of what the wrappers are
//! for. The inner geometry encodes on its own once a caller has unwrapped it,
//! which is the explicit act the wrapper exists to require.
//!
//! `GeometryError` has no encoding. It never carried a serialization derive
//! either, so there is nothing here it replaces, and an error is a returned value
//! rather than a stored artifact. Giving it a wire format would create a
//! compatibility surface with no consumer.
pub use ;
pub use ;
use fmt;
/// Version byte written at the start of every document.
///
/// Present from the first release of the format. A decoder rejects any other
/// value rather than attempting to interpret the remainder.
pub const FORMAT_VERSION: u8 = 1;
/// Type tags.
///
/// These identify the top-level type of a document and double as the variant
/// discriminants inside a [`Geometry`](crate::types::Geometry) body, so a
/// value's tag means the same thing wherever it appears.
///
/// Values are fixed by the format. Changing one is a format change and
/// requires a [`FORMAT_VERSION`] bump.
/// Why a byte stream could not be decoded.
///
/// Every variant is a refusal. None of them is recoverable into a partial
/// value: a decode either yields a fully valid geometry or yields one of
/// these.
/// A bounds-checked cursor over a byte slice.
///
/// Every read returns `Result`. There is no indexing and no slicing by a
/// length read from the stream, which is the usual way a hand-written decoder
/// acquires a panic on hostile input. `forbid(unsafe_code)` and
/// `deny(unwrap_used)` prevent memory unsafety but not an out-of-bounds index.