geometry-io-wkt 0.0.8

OGC Well-Known Text (WKT) reader and writer for the geometry model.
Documentation
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
//! The WKT recursive-descent parser and the typed-parse conveniences.
//!
//! Mirrors `boost/geometry/io/wkt/read.hpp` — the C++ side dispatches on
//! the leading keyword to a per-kind reader (`point_parser`,
//! `linestring_parser`, `polygon_parser`, and the multi variants) that
//! walks the same parenthesised coordinate grammar. This port emits a
//! [`DynGeometry`] because WKT is heterogeneous by construction (a
//! `GEOMETRYCOLLECTION` mixes kinds).
//!
//! Reference: OGC Simple Feature Access Part 1 §7 (grammar) and §6.1.10
//! (worked examples).

use alloc::format;
use alloc::vec::Vec;

use geometry_cs::Cartesian;
use geometry_model::{
    DynGeometry, Linestring, MultiLinestring, MultiPoint, MultiPolygon, Point2D, Polygon, Ring,
};

use crate::lexer::{Lexer, Token, WktError};

/// A concrete 2D Cartesian point — the coordinate type every parsed
/// geometry is built from.
type Pt = Point2D<f64, Cartesian>;

/// Maximum `GEOMETRYCOLLECTION` nesting depth accepted while parsing.
/// `parse_collection_body` recurses through `parse_geometry` per member,
/// so an adversarial string of tens of thousands of nested
/// `GEOMETRYCOLLECTION(` headers would otherwise overflow the native
/// stack and **abort the process** (a stack overflow is not catchable).
/// A bounded depth turns that denial-of-service into a recoverable
/// [`WktError::NestingTooDeep`]. `128` mirrors the sibling WKB / `GeoJSON`
/// readers' cap; real WKT nests only a few levels deep.
const MAX_DEPTH: usize = 128;

/// A one-token lookahead cursor over the input. Every `parse_*` method
/// advances it, and tokens are scanned only as the grammar consumes them.
struct Parser<'a> {
    lexer: Lexer<'a>,
    current: Token,
}

impl<'a> Parser<'a> {
    fn new(input: &'a str) -> Result<Self, WktError> {
        let mut lexer = Lexer::new(input);
        let current = lexer.next_token()?;
        Ok(Self { lexer, current })
    }

    /// Borrow the current token without consuming it.
    fn peek(&self) -> &Token {
        &self.current
    }

    /// Consume and return the current token. The cursor is strictly
    /// monotonic, so the vacated slot is never read again; `Eof` is the
    /// placeholder because a stray re-read then fails exactly like
    /// running off the end of the stream.
    fn next(&mut self) -> Result<Token, WktError> {
        let next = self.lexer.next_token()?;
        Ok(core::mem::replace(&mut self.current, next))
    }

    fn advance(&mut self) -> Result<(), WktError> {
        self.next().map(drop)
    }

    /// Consume a `(`, or fail.
    fn expect_left_paren(&mut self) -> Result<(), WktError> {
        match self.next()? {
            Token::LeftParen => Ok(()),
            other => Err(WktError::UnexpectedToken {
                expected: "'('",
                found: format!("{other:?}"),
            }),
        }
    }

    /// Consume a `)`, or fail.
    fn expect_right_paren(&mut self) -> Result<(), WktError> {
        match self.next()? {
            Token::RightParen => Ok(()),
            other => Err(WktError::UnexpectedToken {
                expected: "')'",
                found: format!("{other:?}"),
            }),
        }
    }

    /// Consume one `f64` numeric token, or fail.
    fn expect_number(&mut self) -> Result<f64, WktError> {
        match self.next()? {
            Token::Number(n) => Ok(n),
            Token::Eof => Err(WktError::UnexpectedEof),
            other => Err(WktError::UnexpectedToken {
                expected: "number",
                found: format!("{other:?}"),
            }),
        }
    }

    /// Skip the optional OGC dimension suffix (`Z`, `M`, or `ZM`) that
    /// may follow the type keyword. This 2D port ignores the extra
    /// ordinates; the suffix is consumed so it does not confuse the
    /// coordinate scan. Mirrors Boost's handling of the dimension in
    /// `boost/geometry/io/wkt/read.hpp` (it likewise reads only the
    /// coordinates its point type declares).
    fn skip_dimension_suffix(&mut self) -> Result<(), WktError> {
        match self.peek() {
            Token::Ident(word) if word == "Z" || word == "M" || word == "ZM" => self.advance(),
            _ => Ok(()),
        }
    }

    /// Read exactly two ordinates into a 2D point. Any further ordinates
    /// (from a `Z`/`M` input) are consumed and discarded.
    fn parse_point_coords(&mut self) -> Result<Pt, WktError> {
        let x = self.expect_number()?;
        let y = self.expect_number()?;
        // Discard trailing Z / M ordinates for this 2D port.
        while let Token::Number(_) = self.peek() {
            self.advance()?;
        }
        Ok(Point2D::new(x, y))
    }

    /// Parse a parenthesised, comma-separated list of coordinate pairs:
    /// `(x y, x y, …)`. Used by `LINESTRING`, each ring of a `POLYGON`,
    /// and the bare-coordinate form of `MULTIPOINT`.
    fn parse_coord_list(&mut self) -> Result<Vec<Pt>, WktError> {
        self.expect_left_paren()?;
        let mut pts = Vec::new();
        loop {
            pts.push(self.parse_point_coords()?);
            match self.peek() {
                Token::Comma => {
                    self.advance()?;
                }
                _ => break,
            }
        }
        self.expect_right_paren()?;
        Ok(pts)
    }

    /// Parse a parenthesised, comma-separated list of coordinate lists:
    /// `((…), (…), …)`. Used by `POLYGON` (outer ring + holes) and
    /// `MULTILINESTRING`.
    fn parse_coord_list_list(&mut self) -> Result<Vec<Vec<Pt>>, WktError> {
        self.expect_left_paren()?;
        let mut rings = Vec::new();
        loop {
            rings.push(self.parse_coord_list()?);
            match self.peek() {
                Token::Comma => {
                    self.advance()?;
                }
                _ => break,
            }
        }
        self.expect_right_paren()?;
        Ok(rings)
    }

    /// `POINT` body: `(x y)`. `POINT EMPTY` is rejected — see the crate
    /// docs — because a 2D `Point` cannot represent "no coordinate".
    fn parse_point_body(&mut self) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if let Token::Empty = self.peek() {
            return Err(WktError::TypeMismatch {
                expected: "POINT with coordinates",
                found: "POINT EMPTY",
            });
        }
        self.expect_left_paren()?;
        let p = self.parse_point_coords()?;
        self.expect_right_paren()?;
        Ok(DynGeometry::Point(p))
    }

    /// `LINESTRING` body: `(x y, …)` or `EMPTY`.
    fn parse_linestring_body(&mut self) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if let Token::Empty = self.peek() {
            self.advance()?;
            return Ok(DynGeometry::LineString(Linestring(Vec::new())));
        }
        let pts = self.parse_coord_list()?;
        Ok(DynGeometry::LineString(Linestring(pts)))
    }

    /// `POLYGON` body: `((outer), (hole1), …)` or `EMPTY`. The first
    /// ring is the exterior; the rest are holes.
    fn parse_polygon_body(&mut self) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        Ok(DynGeometry::Polygon(self.parse_polygon_value()?))
    }

    /// The shared `POLYGON` value builder, reused by `MULTIPOLYGON`.
    fn parse_polygon_value(&mut self) -> Result<Polygon<Pt>, WktError> {
        if let Token::Empty = self.peek() {
            self.advance()?;
            return Ok(Polygon::new(Ring::new()));
        }
        let mut rings = self.parse_coord_list_list()?.into_iter();
        let outer = rings.next().map_or_else(Ring::new, Ring::from_vec);
        let inners: Vec<Ring<Pt>> = rings.map(Ring::from_vec).collect();
        Ok(Polygon::with_inners(outer, inners))
    }

    /// `MULTIPOINT` body. OGC allows both the bare form
    /// `MULTIPOINT (1 1, 2 2)` and the parenthesised form
    /// `MULTIPOINT ((1 1), (2 2))`; this reader accepts either by
    /// peeking past the outer `(` for a nested `(`.
    fn parse_multipoint_body(&mut self) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if let Token::Empty = self.peek() {
            self.advance()?;
            return Ok(DynGeometry::MultiPoint(MultiPoint(Vec::new())));
        }
        self.expect_left_paren()?;
        let mut pts = Vec::new();
        loop {
            if let Token::LeftParen = self.peek() {
                // Parenthesised member: `(x y)`.
                self.advance()?;
                pts.push(self.parse_point_coords()?);
                self.expect_right_paren()?;
            } else {
                // Bare member: `x y`.
                pts.push(self.parse_point_coords()?);
            }
            match self.peek() {
                Token::Comma => {
                    self.advance()?;
                }
                _ => break,
            }
        }
        self.expect_right_paren()?;
        Ok(DynGeometry::MultiPoint(MultiPoint(pts)))
    }

    /// `MULTILINESTRING` body: `((x y, …), …)` or `EMPTY`.
    fn parse_multilinestring_body(&mut self) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if let Token::Empty = self.peek() {
            self.advance()?;
            return Ok(DynGeometry::MultiLineString(MultiLinestring(Vec::new())));
        }
        let lists = self.parse_coord_list_list()?;
        let lines: Vec<Linestring<Pt>> = lists.into_iter().map(Linestring).collect();
        Ok(DynGeometry::MultiLineString(MultiLinestring(lines)))
    }

    /// `MULTIPOLYGON` body: `(((ring), …), …)` or `EMPTY`.
    fn parse_multipolygon_body(&mut self) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if let Token::Empty = self.peek() {
            self.advance()?;
            return Ok(DynGeometry::MultiPolygon(MultiPolygon(Vec::new())));
        }
        self.expect_left_paren()?;
        let mut polys = Vec::new();
        loop {
            polys.push(self.parse_polygon_value()?);
            match self.peek() {
                Token::Comma => {
                    self.advance()?;
                }
                _ => break,
            }
        }
        self.expect_right_paren()?;
        Ok(DynGeometry::MultiPolygon(MultiPolygon(polys)))
    }

    /// `GEOMETRYCOLLECTION` body: `(<geometry>, …)` or `EMPTY`. Recurses
    /// into [`Parser::parse_geometry`] for each member, carrying `depth`
    /// so nested collections stay bounded by [`MAX_DEPTH`].
    fn parse_collection_body(
        &mut self,
        depth: usize,
    ) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if let Token::Empty = self.peek() {
            self.advance()?;
            return Ok(DynGeometry::GeometryCollection(Vec::new()));
        }
        self.expect_left_paren()?;
        let mut items = Vec::new();
        loop {
            items.push(self.parse_geometry(depth + 1)?);
            match self.peek() {
                Token::Comma => {
                    self.advance()?;
                }
                _ => break,
            }
        }
        self.expect_right_paren()?;
        Ok(DynGeometry::GeometryCollection(items))
    }

    /// Parse one geometry: a leading type keyword, an optional dimension
    /// suffix, then the kind-specific body. Mirrors the keyword dispatch
    /// at the top of `boost/geometry/io/wkt/read.hpp`. `depth` bounds the
    /// `GEOMETRYCOLLECTION` recursion against [`MAX_DEPTH`] so adversarial
    /// nesting fails with a recoverable error instead of overflowing the
    /// stack.
    fn parse_geometry(&mut self, depth: usize) -> Result<DynGeometry<f64, Cartesian>, WktError> {
        if depth >= MAX_DEPTH {
            return Err(WktError::NestingTooDeep);
        }
        let keyword = match self.next()? {
            Token::Ident(word) => word,
            Token::Eof => return Err(WktError::UnexpectedEof),
            other => {
                return Err(WktError::UnexpectedToken {
                    expected: "geometry type keyword",
                    found: format!("{other:?}"),
                });
            }
        };
        self.skip_dimension_suffix()?;
        match keyword.as_str() {
            "POINT" => self.parse_point_body(),
            "LINESTRING" => self.parse_linestring_body(),
            "POLYGON" => self.parse_polygon_body(),
            "MULTIPOINT" => self.parse_multipoint_body(),
            "MULTILINESTRING" => self.parse_multilinestring_body(),
            "MULTIPOLYGON" => self.parse_multipolygon_body(),
            "GEOMETRYCOLLECTION" => self.parse_collection_body(depth),
            _ => Err(WktError::UnknownGeometryType(keyword)),
        }
    }
}

/// Parse a WKT string into a runtime-tagged [`DynGeometry`].
///
/// Implements the OGC kinds `POINT`, `LINESTRING`, `POLYGON`,
/// `MULTIPOINT`, `MULTILINESTRING`, `MULTIPOLYGON`, and
/// `GEOMETRYCOLLECTION` (`POLYHEDRALSURFACE` has no concrete model type
/// yet — see `geometry_model::DynGeometry`). Mirrors the read path in
/// `boost/geometry/io/wkt/read.hpp`.
///
/// # `EMPTY` handling
///
/// The collection kinds accept `EMPTY` and yield an empty container
/// (`LINESTRING EMPTY` → an empty [`geometry_model::Linestring`],
/// `GEOMETRYCOLLECTION EMPTY` → an empty `Vec`, and so on). `POINT
/// EMPTY` is rejected with [`WktError::TypeMismatch`]: a 2D
/// [`geometry_model::Point`] has no representation for "no coordinate".
///
/// # `MULTIPOINT` forms
///
/// Both OGC spellings are accepted: the bare `MULTIPOINT (1 1, 2 2)`
/// and the parenthesised `MULTIPOINT ((1 1), (2 2))`.
///
/// # Errors
///
/// Returns a [`WktError`] on a lexer failure, a token that does not fit
/// the grammar, an unknown leading keyword, or `POINT EMPTY`.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::from_wkt;
/// use geometry_model::DynKind;
///
/// let g = from_wkt("POINT (10 10)").unwrap();
/// assert_eq!(g.kind(), DynKind::Point);
/// ```
pub fn from_wkt<S: AsRef<str>>(input: S) -> Result<DynGeometry<f64, Cartesian>, WktError> {
    let mut parser = Parser::new(input.as_ref())?;
    let g = parser.parse_geometry(0)?;
    // Reject trailing garbage after a complete geometry.
    match parser.peek() {
        Token::Eof => Ok(g),
        other => Err(WktError::UnexpectedToken {
            expected: "end of input",
            found: format!("{other:?}"),
        }),
    }
}

/// The domain-noun name of a parsed geometry, for [`WktError::TypeMismatch`].
fn kind_name(g: &DynGeometry<f64, Cartesian>) -> &'static str {
    match g {
        DynGeometry::Point(_) => "POINT",
        DynGeometry::LineString(_) => "LINESTRING",
        DynGeometry::Polygon(_) => "POLYGON",
        DynGeometry::MultiPoint(_) => "MULTIPOINT",
        DynGeometry::MultiLineString(_) => "MULTILINESTRING",
        DynGeometry::MultiPolygon(_) => "MULTIPOLYGON",
        DynGeometry::GeometryCollection(_) => "GEOMETRYCOLLECTION",
    }
}

/// Parse a WKT `POINT` into a concrete [`geometry_model::Point`].
///
/// A [`from_wkt`] followed by a variant match — convenience for callers
/// who know the kind up front (IO1.T4). Mirrors reading straight into a
/// static `model::point` in `boost/geometry/io/wkt/read.hpp`.
///
/// # Errors
///
/// [`WktError::TypeMismatch`] if the input is a different kind, plus any
/// error [`from_wkt`] can raise.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::parse_point;
/// use geometry_trait::Point as _;
///
/// let p = parse_point("POINT (10 10)").unwrap();
/// assert_eq!(p.get::<0>(), 10.0);
/// ```
pub fn parse_point(s: &str) -> Result<Pt, WktError> {
    let g = from_wkt(s)?;
    match g {
        DynGeometry::Point(p) => Ok(p),
        other => Err(WktError::TypeMismatch {
            expected: "POINT",
            found: kind_name(&other),
        }),
    }
}

/// Parse a WKT `LINESTRING` into a concrete [`geometry_model::Linestring`].
///
/// See [`parse_point`] for the pattern.
///
/// # Errors
///
/// [`WktError::TypeMismatch`] if the input is a different kind, plus any
/// error [`from_wkt`] can raise.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::parse_linestring;
/// use geometry_trait::Linestring as _;
///
/// let ls = parse_linestring("LINESTRING (10 10, 20 20, 30 40)").unwrap();
/// assert_eq!(ls.points().len(), 3);
/// ```
pub fn parse_linestring(s: &str) -> Result<Linestring<Pt>, WktError> {
    let g = from_wkt(s)?;
    match g {
        DynGeometry::LineString(ls) => Ok(ls),
        other => Err(WktError::TypeMismatch {
            expected: "LINESTRING",
            found: kind_name(&other),
        }),
    }
}

/// Parse a WKT `POLYGON` into a concrete [`geometry_model::Polygon`].
///
/// See [`parse_point`] for the pattern.
///
/// # Errors
///
/// [`WktError::TypeMismatch`] if the input is a different kind, plus any
/// error [`from_wkt`] can raise.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::parse_polygon;
/// use geometry_trait::{Polygon as _, Ring as _};
///
/// let p = parse_polygon("POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))").unwrap();
/// assert_eq!(p.exterior().points().len(), 5);
/// ```
pub fn parse_polygon(s: &str) -> Result<Polygon<Pt>, WktError> {
    let g = from_wkt(s)?;
    match g {
        DynGeometry::Polygon(p) => Ok(p),
        other => Err(WktError::TypeMismatch {
            expected: "POLYGON",
            found: kind_name(&other),
        }),
    }
}

/// Parse a WKT `MULTIPOINT` into a concrete [`geometry_model::MultiPoint`].
///
/// See [`parse_point`] for the pattern. Both OGC `MULTIPOINT` spellings
/// are accepted (see [`from_wkt`]).
///
/// # Errors
///
/// [`WktError::TypeMismatch`] if the input is a different kind, plus any
/// error [`from_wkt`] can raise.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::parse_multi_point;
/// use geometry_trait::MultiPoint as _;
///
/// let mp = parse_multi_point("MULTIPOINT ((10 10), (20 20))").unwrap();
/// assert_eq!(mp.points().len(), 2);
/// ```
pub fn parse_multi_point(s: &str) -> Result<MultiPoint<Pt>, WktError> {
    let g = from_wkt(s)?;
    match g {
        DynGeometry::MultiPoint(mp) => Ok(mp),
        other => Err(WktError::TypeMismatch {
            expected: "MULTIPOINT",
            found: kind_name(&other),
        }),
    }
}

/// Parse a WKT `MULTILINESTRING` into a concrete
/// [`geometry_model::MultiLinestring`].
///
/// See [`parse_point`] for the pattern.
///
/// # Errors
///
/// [`WktError::TypeMismatch`] if the input is a different kind, plus any
/// error [`from_wkt`] can raise.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::parse_multi_linestring;
/// use geometry_trait::MultiLinestring as _;
///
/// let mls = parse_multi_linestring("MULTILINESTRING ((10 10, 20 20), (15 15, 30 15))").unwrap();
/// assert_eq!(mls.linestrings().len(), 2);
/// ```
pub fn parse_multi_linestring(s: &str) -> Result<MultiLinestring<Linestring<Pt>>, WktError> {
    let g = from_wkt(s)?;
    match g {
        DynGeometry::MultiLineString(mls) => Ok(mls),
        other => Err(WktError::TypeMismatch {
            expected: "MULTILINESTRING",
            found: kind_name(&other),
        }),
    }
}

/// Parse a WKT `MULTIPOLYGON` into a concrete
/// [`geometry_model::MultiPolygon`].
///
/// See [`parse_point`] for the pattern.
///
/// # Errors
///
/// [`WktError::TypeMismatch`] if the input is a different kind, plus any
/// error [`from_wkt`] can raise.
///
/// # Examples
///
/// ```
/// use geometry_io_wkt::parse_multi_polygon;
/// use geometry_trait::MultiPolygon as _;
///
/// let mpg = parse_multi_polygon("MULTIPOLYGON (((10 10, 10 20, 20 20, 20 15, 10 10)))").unwrap();
/// assert_eq!(mpg.polygons().len(), 1);
/// ```
pub fn parse_multi_polygon(s: &str) -> Result<MultiPolygon<Polygon<Pt>>, WktError> {
    let g = from_wkt(s)?;
    match g {
        DynGeometry::MultiPolygon(mpg) => Ok(mpg),
        other => Err(WktError::TypeMismatch {
            expected: "MULTIPOLYGON",
            found: kind_name(&other),
        }),
    }
}

#[cfg(test)]
mod tests {
    //! Reproduces the OGC SFA-1 §6.1.10 worked examples: parse each and
    //! assert the kind plus the point count / coordinate values.
    #![allow(
        clippy::float_cmp,
        reason = "coordinate values come from exact integer WKT literals"
    )]

    use super::*;
    use geometry_trait::{
        Linestring as _, MultiLinestring as _, MultiPoint as _, MultiPolygon as _, Point as _,
        Polygon as _, Ring as _,
    };

    #[test]
    fn deeply_nested_collections_are_rejected_without_overflow() {
        // Regression: a chain of nested GEOMETRYCOLLECTION headers used to
        // recurse until the native stack overflowed (uncatchable SIGABRT).
        // Now the reader caps recursion and returns NestingTooDeep.
        let mut s = alloc::string::String::new();
        for _ in 0..10_000 {
            s.push_str("GEOMETRYCOLLECTION(");
        }
        s.push_str("POINT(1 1)");
        for _ in 0..10_000 {
            s.push(')');
        }
        assert_eq!(from_wkt(&s).unwrap_err(), WktError::NestingTooDeep);
        // A modest, legitimate nesting depth still parses.
        let ok = "GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(1 1)))";
        assert!(from_wkt(ok).is_ok());
    }

    #[test]
    fn point_example() {
        let g = from_wkt("POINT (10 10)").unwrap();
        assert_eq!(g, DynGeometry::Point(Pt::new(10.0, 10.0)));
    }

    #[test]
    fn linestring_example() {
        let g = from_wkt("LINESTRING (10 10, 20 20, 30 40)").unwrap();
        assert_eq!(
            g,
            DynGeometry::LineString(Linestring::from_vec(vec![
                Pt::new(10.0, 10.0),
                Pt::new(20.0, 20.0),
                Pt::new(30.0, 40.0),
            ]))
        );
    }

    #[test]
    fn polygon_example() {
        let g = from_wkt("POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))").unwrap();
        assert_eq!(
            g,
            DynGeometry::Polygon(Polygon::new(Ring::from_vec(vec![
                Pt::new(10.0, 10.0),
                Pt::new(10.0, 20.0),
                Pt::new(20.0, 20.0),
                Pt::new(20.0, 15.0),
                Pt::new(10.0, 10.0),
            ])))
        );
    }

    #[test]
    fn polygon_with_hole() {
        let g =
            from_wkt("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))").unwrap();
        assert_eq!(
            g,
            DynGeometry::Polygon(Polygon::with_inners(
                Ring::from_vec(vec![
                    Pt::new(0.0, 0.0),
                    Pt::new(0.0, 10.0),
                    Pt::new(10.0, 10.0),
                    Pt::new(10.0, 0.0),
                    Pt::new(0.0, 0.0),
                ]),
                vec![Ring::from_vec(vec![
                    Pt::new(2.0, 2.0),
                    Pt::new(2.0, 4.0),
                    Pt::new(4.0, 4.0),
                    Pt::new(4.0, 2.0),
                    Pt::new(2.0, 2.0),
                ])],
            ))
        );
    }

    #[test]
    fn multipoint_parenthesised_form() {
        let g = from_wkt("MULTIPOINT ((10 10), (20 20))").unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiPoint(MultiPoint::from_vec(vec![
                Pt::new(10.0, 10.0),
                Pt::new(20.0, 20.0),
            ]))
        );
    }

    #[test]
    fn multipoint_bare_form() {
        let g = from_wkt("MULTIPOINT (10 10, 20 20)").unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiPoint(MultiPoint::from_vec(vec![
                Pt::new(10.0, 10.0),
                Pt::new(20.0, 20.0),
            ]))
        );
    }

    #[test]
    fn multilinestring_example() {
        let g = from_wkt("MULTILINESTRING ((10 10, 20 20), (15 15, 30 15))").unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiLineString(MultiLinestring::from_vec(vec![
                Linestring::from_vec(vec![Pt::new(10.0, 10.0), Pt::new(20.0, 20.0)]),
                Linestring::from_vec(vec![Pt::new(15.0, 15.0), Pt::new(30.0, 15.0)]),
            ]))
        );
    }

    #[test]
    fn multipolygon_example() {
        let g = from_wkt("MULTIPOLYGON (((10 10, 10 20, 20 20, 20 15, 10 10)))").unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiPolygon(MultiPolygon::from_vec(vec![Polygon::new(Ring::from_vec(
                vec![
                    Pt::new(10.0, 10.0),
                    Pt::new(10.0, 20.0),
                    Pt::new(20.0, 20.0),
                    Pt::new(20.0, 15.0),
                    Pt::new(10.0, 10.0),
                ],
            ))]))
        );
    }

    #[test]
    fn geometrycollection_example() {
        let g = from_wkt("GEOMETRYCOLLECTION (POINT (10 10), LINESTRING (10 10, 20 20))").unwrap();
        assert_eq!(
            g,
            DynGeometry::GeometryCollection(vec![
                DynGeometry::Point(Pt::new(10.0, 10.0)),
                DynGeometry::LineString(Linestring::from_vec(vec![
                    Pt::new(10.0, 10.0),
                    Pt::new(20.0, 20.0),
                ])),
            ])
        );
    }

    #[test]
    fn linestring_empty() {
        let g = from_wkt("LINESTRING EMPTY").unwrap();
        assert_eq!(g, DynGeometry::LineString(Linestring::from_vec(Vec::new())));
    }

    #[test]
    fn geometrycollection_empty() {
        let g = from_wkt("GEOMETRYCOLLECTION EMPTY").unwrap();
        assert_eq!(g, DynGeometry::GeometryCollection(Vec::new()));
    }

    #[test]
    fn point_empty_is_rejected() {
        let err = from_wkt("POINT EMPTY").unwrap_err();
        assert!(matches!(err, WktError::TypeMismatch { .. }));
    }

    #[test]
    fn typed_parse_type_mismatch() {
        let err = parse_point("LINESTRING (10 10, 20 20)").unwrap_err();
        assert_eq!(
            err,
            WktError::TypeMismatch {
                expected: "POINT",
                found: "LINESTRING",
            }
        );
    }

    #[test]
    fn unknown_keyword_errors() {
        let err = from_wkt("TRIANGLE (0 0, 1 0, 0 1, 0 0)").unwrap_err();
        assert_eq!(err, WktError::UnknownGeometryType("TRIANGLE".into()));
    }

    #[test]
    fn dimension_suffix_is_skipped() {
        // The Z ordinate is dropped; the 2D coordinates survive.
        let g = from_wkt("POINT Z (10 10 5)").unwrap();
        assert_eq!(g, DynGeometry::Point(Pt::new(10.0, 10.0)));
    }

    #[test]
    fn malformed_token_after_dimension_suffix_is_reported() {
        assert_eq!(
            from_wkt("POINT Z @"),
            Err(WktError::UnexpectedChar { pos: 8, ch: '@' })
        );
    }

    // ---- EMPTY forms for the remaining collection kinds --------------

    /// `POLYGON EMPTY` yields a polygon with an empty exterior ring and
    /// no holes.
    #[test]
    fn polygon_empty() {
        let g = from_wkt("POLYGON EMPTY").unwrap();
        assert_eq!(g, DynGeometry::Polygon(Polygon::new(Ring::new())));
    }

    /// `MULTIPOINT EMPTY`, `MULTILINESTRING EMPTY`, and `MULTIPOLYGON
    /// EMPTY` each yield an empty container of the matching kind.
    #[test]
    fn multi_kinds_empty() {
        let multipoint = from_wkt("MULTIPOINT EMPTY").unwrap();
        assert_eq!(
            multipoint,
            DynGeometry::MultiPoint(MultiPoint::from_vec(Vec::new()))
        );

        let multilinestring = from_wkt("MULTILINESTRING EMPTY").unwrap();
        assert_eq!(
            multilinestring,
            DynGeometry::MultiLineString(MultiLinestring::from_vec(Vec::new()))
        );

        let multipolygon = from_wkt("MULTIPOLYGON EMPTY").unwrap();
        assert_eq!(
            multipolygon,
            DynGeometry::MultiPolygon(MultiPolygon::from_vec(Vec::new()))
        );
    }

    // ---- Grammar error branches -------------------------------------

    /// A body that opens with the wrong token fails
    /// `expect_left_paren` with an `UnexpectedToken` naming `'('`.
    #[test]
    fn missing_left_paren_is_reported() {
        let err = from_wkt("LINESTRING 10 10").unwrap_err();
        assert!(
            matches!(&err, WktError::UnexpectedToken { expected, .. } if *expected == "'('"),
            "got {err:?}"
        );
    }

    /// A coordinate list that never closes fails `expect_right_paren`
    /// (the terminating token is `Eof`, not `)`).
    #[test]
    fn missing_right_paren_is_reported() {
        let err = from_wkt("LINESTRING (10 10, 20 20").unwrap_err();
        assert!(
            matches!(&err, WktError::UnexpectedToken { expected, .. } if *expected == "')'"),
            "got {err:?}"
        );
    }

    /// A coordinate pair truncated after the first ordinate runs into
    /// `Eof` where the second number is required → `UnexpectedEof`.
    #[test]
    fn missing_ordinate_hits_eof() {
        let err = from_wkt("POINT (10").unwrap_err();
        assert_eq!(err, WktError::UnexpectedEof);
    }

    /// A non-number where an ordinate is expected fails `expect_number`
    /// with an `UnexpectedToken` naming `number`.
    #[test]
    fn non_number_ordinate_is_reported() {
        // The second "ordinate" is a `)`, not a number.
        let err = from_wkt("POINT (10 )").unwrap_err();
        assert!(
            matches!(&err, WktError::UnexpectedToken { expected, .. } if *expected == "number"),
            "got {err:?}"
        );
    }

    /// Trailing tokens after a complete geometry are rejected by the
    /// end-of-input guard in `from_wkt`.
    #[test]
    fn trailing_tokens_are_rejected() {
        let err = from_wkt("POINT (1 1) POINT (2 2)").unwrap_err();
        assert!(
            matches!(&err, WktError::UnexpectedToken { expected, .. } if *expected == "end of input"),
            "got {err:?}"
        );
    }

    /// Empty input reaches `parse_geometry` with an immediate `Eof`
    /// keyword slot → `UnexpectedEof`.
    #[test]
    fn empty_input_is_unexpected_eof() {
        assert_eq!(from_wkt("").unwrap_err(), WktError::UnexpectedEof);
    }

    /// A leading token that is neither an identifier nor `Eof` (here a
    /// `(`) fails the keyword dispatch with `UnexpectedToken`.
    #[test]
    fn non_keyword_leading_token_is_reported() {
        let err = from_wkt("(1 1)").unwrap_err();
        assert!(
            matches!(&err, WktError::UnexpectedToken { expected, .. }
                if *expected == "geometry type keyword"),
            "got {err:?}"
        );
    }

    // ---- Typed-parse happy paths and per-kind mismatches -------------

    /// Each typed parser returns its concrete kind on matching input.
    #[test]
    fn typed_parsers_accept_their_own_kind() {
        assert_eq!(parse_point("POINT (1 2)").unwrap().get::<0>(), 1.0);
        assert_eq!(
            parse_linestring("LINESTRING (0 0, 1 1)")
                .unwrap()
                .points()
                .len(),
            2
        );
        assert_eq!(
            parse_polygon("POLYGON ((0 0, 1 0, 1 1, 0 0))")
                .unwrap()
                .exterior()
                .points()
                .len(),
            4
        );
        assert_eq!(
            parse_multi_point("MULTIPOINT (0 0, 1 1)")
                .unwrap()
                .points()
                .len(),
            2
        );
        assert_eq!(
            parse_multi_linestring("MULTILINESTRING ((0 0, 1 1))")
                .unwrap()
                .linestrings()
                .len(),
            1
        );
        assert_eq!(
            parse_multi_polygon("MULTIPOLYGON (((0 0, 1 0, 1 1, 0 0)))")
                .unwrap()
                .polygons()
                .len(),
            1
        );
    }

    /// Each typed parser rejects a different-kind input with a
    /// `TypeMismatch` whose `found` names the actual kind — exercising
    /// every arm of `kind_name`.
    #[test]
    fn typed_parsers_reject_wrong_kind_naming_the_kind() {
        let cases: &[(WktError, &str, &str)] = &[
            (
                parse_linestring("POINT (1 1)").unwrap_err(),
                "LINESTRING",
                "POINT",
            ),
            (
                parse_polygon("LINESTRING (0 0, 1 1)").unwrap_err(),
                "POLYGON",
                "LINESTRING",
            ),
            (
                parse_multi_point("POLYGON ((0 0, 1 0, 1 1, 0 0))").unwrap_err(),
                "MULTIPOINT",
                "POLYGON",
            ),
            (
                parse_multi_linestring("MULTIPOINT (0 0, 1 1)").unwrap_err(),
                "MULTILINESTRING",
                "MULTIPOINT",
            ),
            (
                parse_multi_polygon("MULTILINESTRING ((0 0, 1 1))").unwrap_err(),
                "MULTIPOLYGON",
                "MULTILINESTRING",
            ),
            (
                parse_point("MULTIPOLYGON (((0 0, 1 0, 1 1, 0 0)))").unwrap_err(),
                "POINT",
                "MULTIPOLYGON",
            ),
            (
                parse_point("GEOMETRYCOLLECTION (POINT (1 1))").unwrap_err(),
                "POINT",
                "GEOMETRYCOLLECTION",
            ),
        ];
        for (err, want_expected, want_found) in cases {
            assert_eq!(
                err,
                &WktError::TypeMismatch {
                    expected: want_expected,
                    found: want_found,
                }
            );
        }
    }
}