feedparser-rs 0.5.3

High-performance RSS/Atom/JSON Feed parser
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
//! GeoRSS namespace support for geographic location data
//!
//! Supports parsing GeoRSS Simple elements for specifying geographic locations
//! in RSS and Atom feeds. GeoRSS is commonly used in mapping applications,
//! location-based services, and geocoded content.
//!
//! # Supported Elements
//!
//! - `georss:point` - Single latitude/longitude point
//! - `georss:line` - Line string (multiple points)
//! - `georss:polygon` - Polygon (closed shape)
//! - `georss:box` - Bounding box (lower-left + upper-right)
//!
//! # Specification
//!
//! GeoRSS Simple: <http://www.georss.org/simple>

use crate::limits::ParserLimits;
use crate::types::{Entry, FeedMeta};

/// `GeoRSS` namespace URI
pub const GEORSS: &str = "http://www.georss.org/georss";

/// Type of geographic shape
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GeoType {
    /// Single point (latitude, longitude)
    #[default]
    Point,
    /// Line connecting multiple points
    Line,
    /// Closed polygon shape
    Polygon,
    /// Bounding box (lower-left, upper-right corners)
    Box,
}

/// Geographic location data from `GeoRSS`
#[derive(Debug, Clone, Default, PartialEq)]
pub struct GeoLocation {
    /// Type of geographic shape
    pub geo_type: GeoType,
    /// Coordinate pairs as (latitude, longitude)
    ///
    /// - Point: 1 coordinate pair
    /// - Line: 2+ coordinate pairs
    /// - Polygon: 3+ coordinate pairs (first == last for closed polygon)
    /// - Box: 2 coordinate pairs (lower-left, upper-right)
    pub coordinates: Vec<(f64, f64)>,
    /// Coordinate reference system (e.g., "EPSG:4326" for WGS84)
    ///
    /// Default is WGS84 (latitude/longitude) if not specified
    pub srs_name: Option<String>,
    /// Elevation in meters (from `georss:elev`)
    pub elev: Option<f64>,
    /// Feature type classification (from `georss:featuretypetag`)
    pub feature_type_tag: Option<String>,
    /// Human-readable place name (from `georss:featurename`)
    pub feature_name: Option<String>,
    /// Relationship type (from `georss:relationshiptag`)
    pub relationship_tag: Option<String>,
}

impl GeoLocation {
    /// Creates new point location
    ///
    /// # Arguments
    ///
    /// * `lat` - Latitude in decimal degrees
    /// * `lon` - Longitude in decimal degrees
    ///
    /// # Examples
    ///
    /// ```
    /// use feedparser_rs::namespace::georss::GeoLocation;
    ///
    /// let loc = GeoLocation::point(45.256, -71.92);
    /// assert_eq!(loc.coordinates.len(), 1);
    /// ```
    #[must_use]
    pub fn point(lat: f64, lon: f64) -> Self {
        Self {
            geo_type: GeoType::Point,
            coordinates: vec![(lat, lon)],
            ..Default::default()
        }
    }

    /// Creates new line location
    ///
    /// # Arguments
    ///
    /// * `coords` - Vector of (latitude, longitude) pairs
    ///
    /// # Examples
    ///
    /// ```
    /// use feedparser_rs::namespace::georss::GeoLocation;
    ///
    /// let coords = vec![(45.256, -71.92), (46.0, -72.0)];
    /// let loc = GeoLocation::line(coords);
    /// assert_eq!(loc.coordinates.len(), 2);
    /// ```
    #[must_use]
    pub fn line(coords: Vec<(f64, f64)>) -> Self {
        Self {
            geo_type: GeoType::Line,
            coordinates: coords,
            ..Default::default()
        }
    }

    /// Creates new polygon location
    ///
    /// # Arguments
    ///
    /// * `coords` - Vector of (latitude, longitude) pairs
    ///
    /// # Examples
    ///
    /// ```
    /// use feedparser_rs::namespace::georss::GeoLocation;
    ///
    /// let coords = vec![
    ///     (45.0, -71.0),
    ///     (46.0, -71.0),
    ///     (46.0, -72.0),
    ///     (45.0, -71.0), // Close the polygon
    /// ];
    /// let loc = GeoLocation::polygon(coords);
    /// ```
    #[must_use]
    pub fn polygon(coords: Vec<(f64, f64)>) -> Self {
        Self {
            geo_type: GeoType::Polygon,
            coordinates: coords,
            ..Default::default()
        }
    }

    /// Creates new bounding box location
    ///
    /// # Arguments
    ///
    /// * `lower_lat` - Lower-left latitude
    /// * `lower_lon` - Lower-left longitude
    /// * `upper_lat` - Upper-right latitude
    /// * `upper_lon` - Upper-right longitude
    ///
    /// # Examples
    ///
    /// ```
    /// use feedparser_rs::namespace::georss::GeoLocation;
    ///
    /// let loc = GeoLocation::bbox(45.0, -72.0, 46.0, -71.0);
    /// assert_eq!(loc.coordinates.len(), 2);
    /// ```
    #[must_use]
    pub fn bbox(lower_lat: f64, lower_lon: f64, upper_lat: f64, upper_lon: f64) -> Self {
        Self {
            geo_type: GeoType::Box,
            coordinates: vec![(lower_lat, lower_lon), (upper_lat, upper_lon)],
            ..Default::default()
        }
    }
}

/// Parse W3C Basic Geo element and update entry
///
/// Handles `geo:lat` and `geo:long` elements. When both are present,
/// auto-constructs `entry.r#where` as a point location.
///
/// # Arguments
///
/// * `tag` - Element local name (e.g., "lat", "long", "lon")
/// * `text` - Element text content
/// * `entry` - Entry to update
///
/// # Returns
///
/// `true` if element was recognized and handled, `false` otherwise
pub fn handle_entry_geo_element(tag: &[u8], text: &str, entry: &mut Entry) -> bool {
    match tag {
        b"lat" => {
            entry.geo_lat = Some(text.to_string());
            try_build_entry_where(entry);
            true
        }
        b"long" | b"lon" => {
            entry.geo_long = Some(text.to_string());
            try_build_entry_where(entry);
            true
        }
        _ => false,
    }
}

/// Parse W3C Basic Geo element and update feed metadata
///
/// Handles `geo:lat` and `geo:long` elements. When both are present,
/// auto-constructs `feed.r#where` as a point location.
///
/// # Arguments
///
/// * `tag` - Element local name (e.g., "lat", "long", "lon")
/// * `text` - Element text content
/// * `feed` - Feed metadata to update
///
/// # Returns
///
/// `true` if element was recognized and handled, `false` otherwise
pub fn handle_feed_geo_element(tag: &[u8], text: &str, feed: &mut FeedMeta) -> bool {
    match tag {
        b"lat" => {
            feed.geo_lat = Some(text.to_string());
            try_build_feed_where(feed);
            true
        }
        b"long" | b"lon" => {
            feed.geo_long = Some(text.to_string());
            try_build_feed_where(feed);
            true
        }
        _ => false,
    }
}

fn try_build_entry_where(entry: &mut Entry) {
    if let (Some(lat_str), Some(lon_str)) = (entry.geo_lat.as_deref(), entry.geo_long.as_deref())
        && let (Ok(lat), Ok(lon)) = (lat_str.parse::<f64>(), lon_str.parse::<f64>())
        && (-90.0..=90.0).contains(&lat)
        && (-180.0..=180.0).contains(&lon)
    {
        entry.r#where = Some(Box::new(GeoLocation::point(lat, lon)));
    }
}

fn try_build_feed_where(feed: &mut FeedMeta) {
    if let (Some(lat_str), Some(lon_str)) = (feed.geo_lat.as_deref(), feed.geo_long.as_deref())
        && let (Ok(lat), Ok(lon)) = (lat_str.parse::<f64>(), lon_str.parse::<f64>())
        && (-90.0..=90.0).contains(&lat)
        && (-180.0..=180.0).contains(&lon)
    {
        feed.r#where = Some(Box::new(GeoLocation::point(lat, lon)));
    }
}

/// Parse `GeoRSS` element and update entry
///
/// # Arguments
///
/// * `tag` - Element local name (e.g., "point", "line", "polygon", "box")
/// * `text` - Element text content
/// * `entry` - Entry to update
/// * `_limits` - Parser limits (unused but kept for API consistency)
///
/// # Returns
///
/// `true` if element was recognized and handled, `false` otherwise
pub fn handle_entry_element(
    tag: &[u8],
    text: &str,
    entry: &mut Entry,
    _limits: &ParserLimits,
) -> bool {
    match tag {
        b"point" => {
            if let Some(loc) = parse_point(text) {
                let existing = entry
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"line" => {
            if let Some(loc) = parse_line(text) {
                let existing = entry
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"polygon" => {
            if let Some(loc) = parse_polygon(text) {
                let existing = entry
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"box" => {
            if let Some(loc) = parse_box(text) {
                let existing = entry
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"elev" => {
            if let Ok(v) = text.trim().parse::<f64>()
                && v.is_finite()
            {
                entry
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()))
                    .elev = Some(v);
            }
            true
        }
        b"featuretypetag" => {
            entry
                .r#where
                .get_or_insert_with(|| Box::new(GeoLocation::default()))
                .feature_type_tag = Some(text.to_string());
            true
        }
        b"featurename" => {
            entry
                .r#where
                .get_or_insert_with(|| Box::new(GeoLocation::default()))
                .feature_name = Some(text.to_string());
            true
        }
        b"relationshiptag" => {
            entry
                .r#where
                .get_or_insert_with(|| Box::new(GeoLocation::default()))
                .relationship_tag = Some(text.to_string());
            true
        }
        _ => false,
    }
}

/// Parse `GeoRSS` element and update feed metadata
///
/// # Arguments
///
/// * `tag` - Element local name (e.g., "point", "line", "polygon", "box")
/// * `text` - Element text content
/// * `feed` - Feed metadata to update
/// * `_limits` - Parser limits (unused but kept for API consistency)
///
/// # Returns
///
/// `true` if element was recognized and handled, `false` otherwise
pub fn handle_feed_element(
    tag: &[u8],
    text: &str,
    feed: &mut FeedMeta,
    _limits: &ParserLimits,
) -> bool {
    match tag {
        b"point" => {
            if let Some(loc) = parse_point(text) {
                let existing = feed
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"line" => {
            if let Some(loc) = parse_line(text) {
                let existing = feed
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"polygon" => {
            if let Some(loc) = parse_polygon(text) {
                let existing = feed
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"box" => {
            if let Some(loc) = parse_box(text) {
                let existing = feed
                    .r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()));
                existing.geo_type = loc.geo_type;
                existing.coordinates = loc.coordinates;
                existing.srs_name = loc.srs_name;
            }
            true
        }
        b"elev" => {
            if let Ok(v) = text.trim().parse::<f64>()
                && v.is_finite()
            {
                feed.r#where
                    .get_or_insert_with(|| Box::new(GeoLocation::default()))
                    .elev = Some(v);
            }
            true
        }
        b"featuretypetag" => {
            feed.r#where
                .get_or_insert_with(|| Box::new(GeoLocation::default()))
                .feature_type_tag = Some(text.to_string());
            true
        }
        b"featurename" => {
            feed.r#where
                .get_or_insert_with(|| Box::new(GeoLocation::default()))
                .feature_name = Some(text.to_string());
            true
        }
        b"relationshiptag" => {
            feed.r#where
                .get_or_insert_with(|| Box::new(GeoLocation::default()))
                .relationship_tag = Some(text.to_string());
            true
        }
        _ => false,
    }
}

/// Parse georss:point element
///
/// Format: "lat lon" (space-separated)
/// Example: "45.256 -71.92"
fn parse_point(text: &str) -> Option<GeoLocation> {
    let coords = parse_coordinates(text)?;
    if coords.len() == 1 {
        Some(GeoLocation {
            geo_type: GeoType::Point,
            coordinates: coords,
            ..Default::default()
        })
    } else {
        None
    }
}

/// Parse georss:line element
///
/// Format: "lat1 lon1 lat2 lon2 ..." (space-separated)
/// Example: "45.256 -71.92 46.0 -72.0"
fn parse_line(text: &str) -> Option<GeoLocation> {
    let coords = parse_coordinates(text)?;
    if coords.len() >= 2 {
        Some(GeoLocation {
            geo_type: GeoType::Line,
            coordinates: coords,
            ..Default::default()
        })
    } else {
        None
    }
}

/// Parse georss:polygon element
///
/// Format: "lat1 lon1 lat2 lon2 lat3 lon3 ..." (space-separated)
/// Example: "45.0 -71.0 46.0 -71.0 46.0 -72.0 45.0 -71.0"
fn parse_polygon(text: &str) -> Option<GeoLocation> {
    let coords = parse_coordinates(text)?;
    if coords.len() >= 3 {
        Some(GeoLocation {
            geo_type: GeoType::Polygon,
            coordinates: coords,
            ..Default::default()
        })
    } else {
        None
    }
}

/// Parse georss:box element
///
/// Format: space-separated values (lower-left, upper-right)
/// Example: "45.0 -72.0 46.0 -71.0"
fn parse_box(text: &str) -> Option<GeoLocation> {
    let coords = parse_coordinates(text)?;
    if coords.len() == 2 {
        Some(GeoLocation {
            geo_type: GeoType::Box,
            coordinates: coords,
            ..Default::default()
        })
    } else {
        None
    }
}

/// Parse space-separated coordinate pairs
///
/// Format: "lat1 lon1 lat2 lon2 ..." (pairs of floats)
fn parse_coordinates(text: &str) -> Option<Vec<(f64, f64)>> {
    let parts: Vec<&str> = text.split_whitespace().collect();

    // Must have even number of values (lat/lon pairs)
    if parts.is_empty() || !parts.len().is_multiple_of(2) {
        return None;
    }

    let mut coords = Vec::with_capacity(parts.len() / 2);

    for chunk in parts.chunks(2) {
        let lat = chunk[0].parse::<f64>().ok()?;
        let lon = chunk[1].parse::<f64>().ok()?;

        // Basic validation: latitude should be -90 to 90, longitude -180 to 180
        if !(-90.0..=90.0).contains(&lat) || !(-180.0..=180.0).contains(&lon) {
            return None;
        }

        coords.push((lat, lon));
    }

    Some(coords)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_point() {
        let loc = parse_point("45.256 -71.92").unwrap();
        assert_eq!(loc.geo_type, GeoType::Point);
        assert_eq!(loc.coordinates.len(), 1);
        assert_eq!(loc.coordinates[0], (45.256, -71.92));
    }

    #[test]
    fn test_parse_point_invalid() {
        assert!(parse_point("45.256").is_none());
        assert!(parse_point("45.256 -71.92 extra").is_none());
        assert!(parse_point("not numbers").is_none());
        assert!(parse_point("").is_none());
    }

    #[test]
    fn test_parse_line() {
        let loc = parse_line("45.256 -71.92 46.0 -72.0").unwrap();
        assert_eq!(loc.geo_type, GeoType::Line);
        assert_eq!(loc.coordinates.len(), 2);
        assert_eq!(loc.coordinates[0], (45.256, -71.92));
        assert_eq!(loc.coordinates[1], (46.0, -72.0));
    }

    #[test]
    fn test_parse_line_single_point() {
        // Line needs at least 2 points
        assert!(parse_line("45.256 -71.92").is_none());
    }

    #[test]
    fn test_parse_polygon() {
        let loc = parse_polygon("45.0 -71.0 46.0 -71.0 46.0 -72.0 45.0 -71.0").unwrap();
        assert_eq!(loc.geo_type, GeoType::Polygon);
        assert_eq!(loc.coordinates.len(), 4);
        assert_eq!(loc.coordinates[0], (45.0, -71.0));
        assert_eq!(loc.coordinates[3], (45.0, -71.0)); // Closed polygon
    }

    #[test]
    fn test_parse_box() {
        let loc = parse_box("45.0 -72.0 46.0 -71.0").unwrap();
        assert_eq!(loc.geo_type, GeoType::Box);
        assert_eq!(loc.coordinates.len(), 2);
        assert_eq!(loc.coordinates[0], (45.0, -72.0)); // Lower-left
        assert_eq!(loc.coordinates[1], (46.0, -71.0)); // Upper-right
    }

    #[test]
    fn test_parse_box_invalid() {
        // Box needs exactly 2 points (4 values)
        assert!(parse_box("45.0 -72.0").is_none());
        assert!(parse_box("45.0 -72.0 46.0 -71.0 extra values").is_none());
    }

    #[test]
    fn test_coordinate_validation() {
        // Invalid latitude (> 90)
        assert!(parse_point("91.0 0.0").is_none());
        // Invalid latitude (< -90)
        assert!(parse_point("-91.0 0.0").is_none());
        // Invalid longitude (> 180)
        assert!(parse_point("0.0 181.0").is_none());
        // Invalid longitude (< -180)
        assert!(parse_point("0.0 -181.0").is_none());
    }

    #[test]
    fn test_handle_entry_element_point() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled = handle_entry_element(b"point", "45.256 -71.92", &mut entry, &limits);
        assert!(handled);
        assert!(entry.r#where.is_some());

        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.geo_type, GeoType::Point);
        assert_eq!(geo.coordinates[0], (45.256, -71.92));
    }

    #[test]
    fn test_handle_entry_element_line() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled =
            handle_entry_element(b"line", "45.256 -71.92 46.0 -72.0", &mut entry, &limits);
        assert!(handled);
        assert!(entry.r#where.is_some());
        assert_eq!(entry.r#where.as_ref().unwrap().geo_type, GeoType::Line);
    }

    #[test]
    fn test_handle_entry_element_unknown() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled = handle_entry_element(b"unknown", "data", &mut entry, &limits);
        assert!(!handled);
        assert!(entry.r#where.is_none());
    }

    #[test]
    fn test_geo_location_constructors() {
        let point = GeoLocation::point(45.0, -71.0);
        assert_eq!(point.geo_type, GeoType::Point);
        assert_eq!(point.coordinates.len(), 1);

        let line = GeoLocation::line(vec![(45.0, -71.0), (46.0, -72.0)]);
        assert_eq!(line.geo_type, GeoType::Line);
        assert_eq!(line.coordinates.len(), 2);

        let polygon = GeoLocation::polygon(vec![(45.0, -71.0), (46.0, -71.0), (45.0, -71.0)]);
        assert_eq!(polygon.geo_type, GeoType::Polygon);
        assert_eq!(polygon.coordinates.len(), 3);

        let bbox = GeoLocation::bbox(45.0, -72.0, 46.0, -71.0);
        assert_eq!(bbox.geo_type, GeoType::Box);
        assert_eq!(bbox.coordinates.len(), 2);
    }

    #[test]
    fn test_whitespace_handling() {
        let loc = parse_point("  45.256   -71.92  ").unwrap();
        assert_eq!(loc.coordinates[0], (45.256, -71.92));
    }

    #[test]
    fn test_handle_feed_element_point() {
        let mut feed = FeedMeta::default();
        let limits = ParserLimits::default();

        let handled = handle_feed_element(b"point", "45.256 -71.92", &mut feed, &limits);
        assert!(handled);
        assert!(feed.r#where.is_some());

        let geo = feed.r#where.as_ref().unwrap();
        assert_eq!(geo.geo_type, GeoType::Point);
        assert_eq!(geo.coordinates[0], (45.256, -71.92));
    }

    #[test]
    fn test_handle_feed_element_line() {
        let mut feed = FeedMeta::default();
        let limits = ParserLimits::default();

        let handled = handle_feed_element(b"line", "45.256 -71.92 46.0 -72.0", &mut feed, &limits);
        assert!(handled);
        assert!(feed.r#where.is_some());
        assert_eq!(feed.r#where.as_ref().unwrap().geo_type, GeoType::Line);
    }

    #[test]
    fn test_handle_feed_element_polygon() {
        let mut feed = FeedMeta::default();
        let limits = ParserLimits::default();

        let handled = handle_feed_element(
            b"polygon",
            "45.0 -71.0 46.0 -71.0 46.0 -72.0 45.0 -71.0",
            &mut feed,
            &limits,
        );
        assert!(handled);
        assert!(feed.r#where.is_some());
        assert_eq!(feed.r#where.as_ref().unwrap().geo_type, GeoType::Polygon);
    }

    #[test]
    fn test_handle_feed_element_box() {
        let mut feed = FeedMeta::default();
        let limits = ParserLimits::default();

        let handled = handle_feed_element(b"box", "45.0 -72.0 46.0 -71.0", &mut feed, &limits);
        assert!(handled);
        assert!(feed.r#where.is_some());
        assert_eq!(feed.r#where.as_ref().unwrap().geo_type, GeoType::Box);
    }

    #[test]
    fn test_handle_feed_element_unknown() {
        let mut feed = FeedMeta::default();
        let limits = ParserLimits::default();

        let handled = handle_feed_element(b"unknown", "data", &mut feed, &limits);
        assert!(!handled);
        assert!(feed.r#where.is_none());
    }

    #[test]
    fn test_handle_feed_element_invalid_data() {
        let mut feed = FeedMeta::default();
        let limits = ParserLimits::default();

        let handled = handle_feed_element(b"point", "invalid data", &mut feed, &limits);
        assert!(handled);
        assert!(feed.r#where.is_none());
    }

    #[test]
    fn test_handle_entry_element_elev() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled = handle_entry_element(b"elev", "1337.5", &mut entry, &limits);
        assert!(handled);
        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.elev, Some(1337.5));
    }

    #[test]
    fn test_handle_entry_element_feature_name() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled = handle_entry_element(b"featurename", "Mont Mégantic", &mut entry, &limits);
        assert!(handled);
        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.feature_name.as_deref(), Some("Mont Mégantic"));
    }

    #[test]
    fn test_handle_entry_element_feature_type_tag() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled = handle_entry_element(b"featuretypetag", "mountain", &mut entry, &limits);
        assert!(handled);
        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.feature_type_tag.as_deref(), Some("mountain"));
    }

    #[test]
    fn test_handle_entry_element_relationship_tag() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled =
            handle_entry_element(b"relationshiptag", "is-located-at", &mut entry, &limits);
        assert!(handled);
        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.relationship_tag.as_deref(), Some("is-located-at"));
    }

    #[test]
    fn test_extended_attrs_without_geometry() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        handle_entry_element(b"featurename", "Unknown Location", &mut entry, &limits);
        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.feature_name.as_deref(), Some("Unknown Location"));
        assert!(geo.coordinates.is_empty());
    }

    #[test]
    fn test_extended_attrs_invalid_elev() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        let handled = handle_entry_element(b"elev", "not-a-number", &mut entry, &limits);
        assert!(handled);
        // GeoLocation not created because elev parse failed
        assert!(entry.r#where.is_none());
    }

    #[test]
    fn test_extended_attrs_elev_non_finite_ignored() {
        let limits = ParserLimits::default();

        for value in ["NaN", "Infinity", "-Infinity"] {
            let mut entry = Entry::default();
            let handled = handle_entry_element(b"elev", value, &mut entry, &limits);
            assert!(handled, "element must be recognized for value {value}");
            assert!(
                entry.r#where.is_none(),
                "non-finite elev '{value}' must not create GeoLocation"
            );
        }
    }

    #[test]
    fn test_extended_attrs_before_geometry() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        handle_entry_element(b"featurename", "Reverse Order", &mut entry, &limits);
        handle_entry_element(b"elev", "500.0", &mut entry, &limits);
        handle_entry_element(b"point", "40.0 -74.0", &mut entry, &limits);

        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.geo_type, GeoType::Point);
        assert_eq!(geo.coordinates[0], (40.0, -74.0));
        assert_eq!(geo.feature_name.as_deref(), Some("Reverse Order"));
        assert_eq!(geo.elev, Some(500.0));
    }

    #[test]
    fn test_extended_attrs_after_geometry() {
        let mut entry = Entry::default();
        let limits = ParserLimits::default();

        handle_entry_element(b"point", "45.256 -71.92", &mut entry, &limits);
        handle_entry_element(b"featurename", "Mont Mégantic", &mut entry, &limits);
        handle_entry_element(b"elev", "1337.5", &mut entry, &limits);

        let geo = entry.r#where.as_ref().unwrap();
        assert_eq!(geo.geo_type, GeoType::Point);
        assert_eq!(geo.coordinates[0], (45.256, -71.92));
        assert_eq!(geo.feature_name.as_deref(), Some("Mont Mégantic"));
        assert_eq!(geo.elev, Some(1337.5));
    }
}