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
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

//! Integration tests for `GeoRSS` and Creative Commons namespace parsing.
//!
//! Exercises the full `parse()` path end-to-end for:
//! - `GeoRSS` Simple (`georss:point`, `georss:polygon`, feed-level geo, invalid coordinates)
//! - Creative Commons (`creativeCommons:license`, `cc:license`, both on same field)

use feedparser_rs::namespace::georss::GeoType;
use feedparser_rs::parse;

// ──────────────────────────────────────────────────────────────────────────────
// GeoRSS integration tests
// ──────────────────────────────────────────────────────────────────────────────

#[test]
fn test_georss_point_in_entry() {
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:georss="http://www.georss.org/georss">
        <channel>
            <title>Geo Feed</title>
            <link>http://example.com</link>
            <item>
                <title>Location Post</title>
                <link>http://example.com/1</link>
                <georss:point>45.256 -71.92</georss:point>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);
    assert_eq!(feed.entries.len(), 1);

    let geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.geo should be set");
    assert_eq!(geo.geo_type, GeoType::Point);
    assert_eq!(geo.coordinates.len(), 1);
    assert_eq!(geo.coordinates[0], (45.256, -71.92));
}

#[test]
fn test_georss_polygon_in_entry() {
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:georss="http://www.georss.org/georss">
        <channel>
            <title>Geo Feed</title>
            <link>http://example.com</link>
            <item>
                <title>Region Post</title>
                <link>http://example.com/2</link>
                <georss:polygon>45.0 -71.0 46.0 -71.0 46.0 -72.0 45.0 -71.0</georss:polygon>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);

    let geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.geo should be set");
    assert_eq!(geo.geo_type, GeoType::Polygon);
    assert_eq!(geo.coordinates.len(), 4);
    assert_eq!(geo.coordinates[0], (45.0, -71.0));
    assert_eq!(geo.coordinates[3], (45.0, -71.0)); // closed polygon
}

#[test]
fn test_georss_point_at_feed_level() {
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:georss="http://www.georss.org/georss">
        <channel>
            <title>Station Feed</title>
            <link>http://example.com</link>
            <georss:point>51.5074 -0.1278</georss:point>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);

    let geo = feed.feed.r#where.as_ref().expect("feed.geo should be set");
    assert_eq!(geo.geo_type, GeoType::Point);
    assert_eq!(geo.coordinates.len(), 1);
    assert_eq!(geo.coordinates[0], (51.5074, -0.1278));
}

#[test]
fn test_georss_invalid_coordinates_no_panic() {
    // Out-of-range latitude (> 90) must not panic and must leave geo as None.
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:georss="http://www.georss.org/georss">
        <channel>
            <title>Bad Geo Feed</title>
            <link>http://example.com</link>
            <item>
                <title>Post</title>
                <georss:point>999.0 -71.92</georss:point>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(
        feed.entries[0].r#where.is_none(),
        "invalid coordinates must produce None, not panic"
    );
}

#[test]
fn test_georss_malformed_text_no_panic() {
    // Non-numeric content must not panic and must leave geo as None.
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:georss="http://www.georss.org/georss">
        <channel>
            <title>Bad Geo Feed</title>
            <link>http://example.com</link>
            <item>
                <title>Post</title>
                <georss:point>not a number at all</georss:point>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(
        feed.entries[0].r#where.is_none(),
        "malformed coordinates must produce None, not panic"
    );
}

// ──────────────────────────────────────────────────────────────────────────────
// Creative Commons integration tests
// ──────────────────────────────────────────────────────────────────────────────

#[test]
fn test_creative_commons_license_on_feed() {
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
        <channel>
            <title>CC Feed</title>
            <link>http://example.com</link>
            <creativeCommons:license>https://creativecommons.org/licenses/by/4.0/</creativeCommons:license>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);
    assert_eq!(
        feed.feed.license.as_deref(),
        Some("https://creativecommons.org/licenses/by/4.0/")
    );
}

#[test]
fn test_cc_license_on_entry() {
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
        <channel>
            <title>CC Entry Feed</title>
            <link>http://example.com</link>
            <item>
                <title>Licensed Post</title>
                <link>http://example.com/post/1</link>
                <creativeCommons:license>https://creativecommons.org/licenses/by-sa/4.0/</creativeCommons:license>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);
    assert_eq!(feed.entries.len(), 1);
    assert_eq!(
        feed.entries[0].license.as_deref(),
        Some("https://creativecommons.org/licenses/by-sa/4.0/")
    );
}

#[test]
fn test_cc_license_both_feed_and_entry() {
    // Both feed-level and entry-level licenses must be parsed independently.
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
        <channel>
            <title>Mixed CC Feed</title>
            <link>http://example.com</link>
            <creativeCommons:license>https://creativecommons.org/licenses/by/4.0/</creativeCommons:license>
            <item>
                <title>Post with own license</title>
                <link>http://example.com/post/2</link>
                <creativeCommons:license>https://creativecommons.org/licenses/by-nc/4.0/</creativeCommons:license>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);
    assert_eq!(
        feed.feed.license.as_deref(),
        Some("https://creativecommons.org/licenses/by/4.0/")
    );
    assert_eq!(
        feed.entries[0].license.as_deref(),
        Some("https://creativecommons.org/licenses/by-nc/4.0/")
    );
}

// ──────────────────────────────────────────────────────────────────────────────
// Combined namespace test
// ──────────────────────────────────────────────────────────────────────────────

#[test]
fn test_georss_and_cc_together() {
    // A feed that uses both GeoRSS and Creative Commons simultaneously.
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0"
         xmlns:georss="http://www.georss.org/georss"
         xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
        <channel>
            <title>Geo + CC Feed</title>
            <link>http://example.com</link>
            <georss:point>48.8566 2.3522</georss:point>
            <creativeCommons:license>https://creativecommons.org/licenses/by/4.0/</creativeCommons:license>
            <item>
                <title>Paris Event</title>
                <link>http://example.com/paris</link>
                <georss:point>48.8566 2.3522</georss:point>
                <creativeCommons:license>https://creativecommons.org/licenses/by-sa/4.0/</creativeCommons:license>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    assert!(!feed.bozo);

    // Feed-level assertions
    let feed_geo = feed.feed.r#where.as_ref().expect("feed.geo should be set");
    assert_eq!(feed_geo.geo_type, GeoType::Point);
    assert_eq!(feed_geo.coordinates[0], (48.8566, 2.3522));
    assert_eq!(
        feed.feed.license.as_deref(),
        Some("https://creativecommons.org/licenses/by/4.0/")
    );

    // Entry-level assertions
    assert_eq!(feed.entries.len(), 1);
    let entry_geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.geo should be set");
    assert_eq!(entry_geo.geo_type, GeoType::Point);
    assert_eq!(entry_geo.coordinates[0], (48.8566, 2.3522));
    assert_eq!(
        feed.entries[0].license.as_deref(),
        Some("https://creativecommons.org/licenses/by-sa/4.0/")
    );
}

// ──────────────────────────────────────────────────────────────────────────────
// Issue #291: georss:point/polygon not parsed in Atom entries
// ──────────────────────────────────────────────────────────────────────────────

#[test]
fn test_issue_291_atom_georss_point_in_entry() {
    let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom"
          xmlns:georss="http://www.georss.org/georss">
      <title>GeoRSS Atom Test</title>
      <id>urn:uuid:test</id>
      <updated>2024-01-01T00:00:00Z</updated>
      <entry>
        <id>urn:uuid:entry-1</id>
        <title>Entry with georss:point</title>
        <link href="https://example.com/entry-1"/>
        <updated>2024-01-01T00:00:00Z</updated>
        <georss:point>45.256 -71.92</georss:point>
      </entry>
    </feed>"#;

    let feed = parse(xml).expect("parse failed");
    assert!(!feed.bozo, "valid feed must not set bozo");
    assert_eq!(feed.entries.len(), 1);
    let geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.where should be set");
    assert_eq!(geo.geo_type, GeoType::Point);
    assert_eq!(geo.coordinates[0], (45.256, -71.92));
}

#[test]
fn test_issue_291_atom_georss_polygon_in_entry() {
    let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom"
          xmlns:georss="http://www.georss.org/georss">
      <title>GeoRSS Polygon Atom Test</title>
      <id>urn:uuid:test</id>
      <updated>2024-01-01T00:00:00Z</updated>
      <entry>
        <id>urn:uuid:entry-1</id>
        <title>Entry with georss:polygon</title>
        <link href="https://example.com/entry-1"/>
        <updated>2024-01-01T00:00:00Z</updated>
        <georss:polygon>45.0 -71.0 46.0 -71.0 46.0 -72.0 45.0 -71.0</georss:polygon>
      </entry>
    </feed>"#;

    let feed = parse(xml).expect("parse failed");
    assert!(!feed.bozo, "valid feed must not set bozo");
    let geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.where should be set");
    assert_eq!(geo.geo_type, GeoType::Polygon);
    assert_eq!(geo.coordinates.len(), 4);
}

// ──────────────────────────────────────────────────────────────────────────────
// Issue #248: geo:lat/geo:long (W3C Basic Geo) not implemented
// ──────────────────────────────────────────────────────────────────────────────

#[test]
fn test_issue_248_rss_geo_latlong_in_entry() {
    let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
      <channel>
        <title>W3C Geo Feed</title>
        <link>https://example.com</link>
        <description>Feed with geo:lat/geo:long</description>
        <item>
          <title>Item at location</title>
          <link>https://example.com/item-1</link>
          <geo:lat>51.5074</geo:lat>
          <geo:long>-0.1278</geo:long>
        </item>
      </channel>
    </rss>"#;

    let feed = parse(xml).expect("parse failed");
    assert!(!feed.bozo, "valid feed must not set bozo");
    assert_eq!(feed.entries.len(), 1);
    assert_eq!(feed.entries[0].geo_lat.as_deref(), Some("51.5074"));
    assert_eq!(feed.entries[0].geo_long.as_deref(), Some("-0.1278"));
    let geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.where should be auto-constructed");
    assert_eq!(geo.geo_type, GeoType::Point);
    assert!((geo.coordinates[0].0 - 51.5074).abs() < 1e-6);
    assert!((geo.coordinates[0].1 - (-0.1278)).abs() < 1e-6);
}

#[test]
fn test_issue_248_atom_geo_latlong_in_entry() {
    let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom"
          xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
      <title>W3C Geo Atom Feed</title>
      <id>urn:uuid:atom-geo-test</id>
      <updated>2024-01-01T00:00:00Z</updated>
      <entry>
        <id>urn:uuid:entry-1</id>
        <title>Entry with geo:lat and geo:long</title>
        <link href="https://example.com/entry-1"/>
        <updated>2024-01-01T00:00:00Z</updated>
        <geo:lat>48.8566</geo:lat>
        <geo:long>2.3522</geo:long>
      </entry>
    </feed>"#;

    let feed = parse(xml).expect("parse failed");
    assert!(!feed.bozo, "valid feed must not set bozo");
    assert_eq!(feed.entries.len(), 1);
    assert_eq!(feed.entries[0].geo_lat.as_deref(), Some("48.8566"));
    assert_eq!(feed.entries[0].geo_long.as_deref(), Some("2.3522"));
    let geo = feed.entries[0]
        .r#where
        .as_ref()
        .expect("entry.where should be auto-constructed");
    assert_eq!(geo.geo_type, GeoType::Point);
    assert!((geo.coordinates[0].0 - 48.8566).abs() < 1e-6);
    assert!((geo.coordinates[0].1 - 2.3522).abs() < 1e-6);
}

#[test]
fn test_issue_248_geo_lat_only_no_where_constructed() {
    let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
      <channel>
        <title>Feed</title>
        <link>https://example.com</link>
        <item>
          <title>Item with lat only</title>
          <geo:lat>51.5074</geo:lat>
        </item>
      </channel>
    </rss>"#;

    let feed = parse(xml).expect("parse failed");
    assert_eq!(feed.entries[0].geo_lat.as_deref(), Some("51.5074"));
    assert!(feed.entries[0].geo_long.is_none());
    assert!(
        feed.entries[0].r#where.is_none(),
        "where should not be constructed without both lat and long"
    );
}

// ──────────────────────────────────────────────────────────────────────────────
// GeoRSS extended attributes tests (issue #355)
// ──────────────────────────────────────────────────────────────────────────────

#[test]
fn test_issue_355_georss_extended_attrs_fixture() {
    let xml = include_bytes!("../../../tests/fixtures/georss_extended_attributes.xml");
    let feed = parse(xml).expect("parse failed");
    assert!(!feed.bozo);
    assert_eq!(feed.entries.len(), 4);

    // Item 1: Mountain Peak — all extended attrs + geometry
    let geo0 = feed.entries[0]
        .r#where
        .as_ref()
        .expect("item 1 should have geo");
    assert_eq!(geo0.geo_type, GeoType::Point);
    assert_eq!(geo0.coordinates[0], (45.256, -71.92));
    assert_eq!(geo0.elev, Some(1337.5));
    assert_eq!(geo0.feature_type_tag.as_deref(), Some("mountain"));
    assert_eq!(geo0.feature_name.as_deref(), Some("Mont Mégantic"));
    assert_eq!(geo0.relationship_tag.as_deref(), Some("is-located-at"));

    // Item 2: City — partial extended attrs
    let geo1 = feed.entries[1]
        .r#where
        .as_ref()
        .expect("item 2 should have geo");
    assert_eq!(geo1.feature_type_tag.as_deref(), Some("city"));
    assert_eq!(geo1.feature_name.as_deref(), Some("Paris"));
    assert!(geo1.elev.is_none());

    // Item 3: Metadata Only — no geometry, only feature_name
    let geo2 = feed.entries[2]
        .r#where
        .as_ref()
        .expect("item 3 should have geo");
    assert_eq!(geo2.feature_name.as_deref(), Some("Unknown Location"));
    assert!(geo2.coordinates.is_empty());

    // Item 4: Attrs Before Geometry — regression test for merge pattern (C1)
    let geo3 = feed.entries[3]
        .r#where
        .as_ref()
        .expect("item 4 should have geo");
    assert_eq!(geo3.geo_type, GeoType::Point);
    assert_eq!(geo3.coordinates[0], (40.0, -74.0));
    assert_eq!(geo3.feature_name.as_deref(), Some("Reverse Order"));
    assert_eq!(geo3.elev, Some(500.0));
}