finance-query 2.8.0

A Rust library for querying financial data
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
//! Minimal, dependency-free RSS 2.0 / Atom 1.0 entry extractor.
//!
//! We only need to identify a fixed handful of well-known tag names such as
//! `title`,`link`, `pubDate`/`published`/`updated`,
//! `description`/`summary`/`content`/`content:encoded`) and the `href`
//! attribute on `<link>` — a small enough surface that owning the tokenizer
//! outright removes an entire class of transitive dependency risk (see
//! RUSTSEC-2026-0195, which lived in a third-party XML crate's namespace
//! resolution — logic this parser never had and now cannot reintroduce).
//! There is also no DTD/entity-expansion support (only the 5 predefined XML
//! entities + numeric character refs, each capped at 10 bytes), so billion-laughs-style
//! entity bombs aren't a parseable construct here at all.
//!
//! Input is assumed to already be valid UTF-8 (it comes from
//! `reqwest::Response::text()`, which performs charset decoding), so byte
//! offsets from ASCII delimiter scans (`<`, `>`, `&`, `;`, quotes) are always
//! safe UTF-8 boundaries — none of those bytes can appear inside a
//! multi-byte UTF-8 continuation sequence.

use chrono::DateTime;

use super::FeedEntry;
use crate::error::{FinanceError, Result};

#[derive(Default)]
struct PartialEntry {
    title: Option<String>,
    url: Option<String>,
    published_raw: Option<String>,
    updated_raw: Option<String>,
    summary_raw: Option<String>,
    content_raw: Option<String>,
}

impl PartialEntry {
    fn finish(self, source: &str) -> Option<FeedEntry> {
        let title = self.title?.trim().to_string();
        if title.is_empty() {
            return None;
        }
        let url = self.url?.trim().to_string();
        if url.is_empty() {
            return None;
        }
        let published = self
            .published_raw
            .or(self.updated_raw)
            .and_then(|raw| parse_date(&raw));
        let summary = self
            .summary_raw
            .or(self.content_raw)
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty());

        Some(FeedEntry {
            title,
            url,
            published,
            summary,
            source: source.to_string(),
        })
    }
}

/// Which leaf field an open tag's text content should accumulate into.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Field {
    Title,
    Link,
    Published,
    Updated,
    Summary,
    Content,
}

fn field_for(tag: &[u8]) -> Option<Field> {
    match tag {
        b"title" => Some(Field::Title),
        b"pubDate" | b"published" => Some(Field::Published),
        b"updated" => Some(Field::Updated),
        b"description" | b"summary" => Some(Field::Summary),
        b"content" | b"content:encoded" => Some(Field::Content),
        _ => None,
    }
}

fn append(entry: &mut PartialEntry, field: Field, text: String) {
    let slot = match field {
        Field::Title => &mut entry.title,
        Field::Link => &mut entry.url,
        Field::Published => &mut entry.published_raw,
        Field::Updated => &mut entry.updated_raw,
        Field::Summary => &mut entry.summary_raw,
        Field::Content => &mut entry.content_raw,
    };
    match slot {
        Some(existing) => existing.push_str(&text),
        None => *slot = Some(text),
    }
}

/// RSS 2.0 `pubDate` is RFC 2822; Atom `published`/`updated` is RFC 3339.
/// Invalid or unrecognized dates are dropped rather than propagated, matching
/// prior (feed-rs-based) behavior where an unparsable date left the field `None`.
fn parse_date(raw: &str) -> Option<String> {
    let raw = raw.trim();
    if raw.is_empty() {
        return None;
    }
    // Some real-world feed generators emit a weekday name that doesn't match
    // the actual date (an off-by-one bug in the generator); chrono's rfc2822
    // parser validates day-name/date agreement and rejects the whole value
    // on a mismatch. We never use the weekday, so strip it before parsing
    // rather than losing an otherwise-valid date over a cosmetic error.
    let without_weekday = raw
        .find(',')
        .filter(|&comma| comma <= 4)
        .map_or(raw, |comma| raw[comma + 1..].trim_start());

    DateTime::parse_from_rfc2822(without_weekday)
        .or_else(|_| DateTime::parse_from_rfc3339(raw))
        .ok()
        .map(|dt| dt.to_rfc3339())
}

fn find(haystack: &[u8], from: usize, needle: &[u8]) -> Option<usize> {
    if from > haystack.len() {
        return None;
    }
    haystack[from..]
        .windows(needle.len())
        .position(|w| w == needle)
        .map(|p| p + from)
}

fn find_byte(haystack: &[u8], from: usize, byte: u8) -> Option<usize> {
    if from > haystack.len() {
        return None;
    }
    haystack[from..]
        .iter()
        .position(|&b| b == byte)
        .map(|p| p + from)
}

fn trim_ascii(bytes: &[u8]) -> &[u8] {
    let start = bytes
        .iter()
        .position(|b| !b.is_ascii_whitespace())
        .unwrap_or(bytes.len());
    let end = bytes[start..]
        .iter()
        .rposition(|b| !b.is_ascii_whitespace())
        .map(|p| start + p + 1)
        .unwrap_or(start);
    &bytes[start..end]
}

/// Decode the 5 predefined XML entities and numeric character references
/// (`&#NN;` / `&#xHH;`). Unrecognized or malformed entities are left as a
/// literal `&` rather than erroring — real-world feeds are inconsistent here.
fn unescape(raw: &[u8]) -> String {
    let mut out = String::with_capacity(raw.len());
    let mut i = 0;
    loop {
        match find_byte(raw, i, b'&') {
            Some(amp) => {
                out.push_str(&String::from_utf8_lossy(&raw[i..amp]));
                let decoded = find_byte(raw, amp, b';')
                    .filter(|&semi| semi - amp <= 10)
                    .and_then(|semi| decode_entity(&raw[amp + 1..semi]).map(|ch| (ch, semi)));
                match decoded {
                    Some((ch, semi)) => {
                        out.push(ch);
                        i = semi + 1;
                    }
                    None => {
                        out.push('&');
                        i = amp + 1;
                    }
                }
            }
            None => {
                out.push_str(&String::from_utf8_lossy(&raw[i..]));
                break;
            }
        }
    }
    out
}

fn decode_entity(entity: &[u8]) -> Option<char> {
    match entity {
        b"amp" => Some('&'),
        b"lt" => Some('<'),
        b"gt" => Some('>'),
        b"quot" => Some('"'),
        b"apos" => Some('\''),
        _ if entity.starts_with(b"#x") || entity.starts_with(b"#X") => {
            let hex = std::str::from_utf8(&entity[2..]).ok()?;
            u32::from_str_radix(hex, 16).ok().and_then(char::from_u32)
        }
        _ if entity.starts_with(b"#") => {
            let dec = std::str::from_utf8(&entity[1..]).ok()?;
            dec.parse::<u32>().ok().and_then(char::from_u32)
        }
        _ => None,
    }
}

/// Find an attribute's value within a start tag's raw bytes (after the tag
/// name). Handles both `"` and `'` quoting and arbitrary intervening
/// whitespace/newlines; requires a whitespace boundary before the attribute
/// name so e.g. `xhref` doesn't match a lookup for `href`.
fn find_attr(tag_bytes: &[u8], attr: &[u8]) -> Option<String> {
    let mut i = 0;
    while i < tag_bytes.len() {
        if tag_bytes[i..].starts_with(attr) {
            let boundary_ok = i == 0 || tag_bytes[i - 1].is_ascii_whitespace();
            if boundary_ok {
                let mut j = i + attr.len();
                while tag_bytes.get(j).is_some_and(u8::is_ascii_whitespace) {
                    j += 1;
                }
                if tag_bytes.get(j) == Some(&b'=') {
                    j += 1;
                    while tag_bytes.get(j).is_some_and(u8::is_ascii_whitespace) {
                        j += 1;
                    }
                    if let Some(&quote) = tag_bytes.get(j)
                        && (quote == b'"' || quote == b'\'')
                        && let Some(end) = find_byte(tag_bytes, j + 1, quote)
                    {
                        return Some(unescape(&tag_bytes[j + 1..end]));
                    }
                }
            }
        }
        i += 1;
    }
    None
}

/// Parse already-fetched RSS/Atom bytes into entries.
pub(super) fn parse(bytes: &[u8], source: &str) -> Result<Vec<FeedEntry>> {
    let len = bytes.len();
    let mut i = 0usize;
    let mut entries = Vec::new();
    let mut current: Option<PartialEntry> = None;
    let mut capturing: Option<(Field, &[u8])> = None;
    let mut stack: Vec<&[u8]> = Vec::new();

    let err = |context: String| -> FinanceError {
        FinanceError::FeedParseError {
            url: source.to_string(),
            context,
        }
    };

    while i < len {
        if bytes[i] != b'<' {
            let end = find_byte(bytes, i, b'<').unwrap_or(len);
            if let (Some((field, _)), Some(cur)) = (capturing, current.as_mut()) {
                append(cur, field, unescape(&bytes[i..end]));
            }
            i = end;
            continue;
        }

        if bytes[i..].starts_with(b"<!--") {
            let end =
                find(bytes, i + 4, b"-->").ok_or_else(|| err("unterminated comment".into()))?;
            i = end + 3;
        } else if bytes[i..].starts_with(b"<![CDATA[") {
            let start = i + 9;
            let end = find(bytes, start, b"]]>")
                .ok_or_else(|| err("unterminated CDATA section".into()))?;
            if let (Some((field, _)), Some(cur)) = (capturing, current.as_mut()) {
                append(
                    cur,
                    field,
                    String::from_utf8_lossy(&bytes[start..end]).into_owned(),
                );
            }
            i = end + 3;
        } else if bytes[i..].starts_with(b"<?") {
            let end = find(bytes, i + 2, b"?>")
                .ok_or_else(|| err("unterminated processing instruction".into()))?;
            i = end + 2;
        } else if bytes[i..].starts_with(b"<!") {
            let end = find_byte(bytes, i + 2, b'>')
                .ok_or_else(|| err("unterminated declaration".into()))?;
            i = end + 1;
        } else if bytes.get(i + 1) == Some(&b'/') {
            let end =
                find_byte(bytes, i + 2, b'>').ok_or_else(|| err("unterminated end tag".into()))?;
            let name = trim_ascii(&bytes[i + 2..end]);

            match stack.pop() {
                Some(top) if top == name => {}
                Some(top) => {
                    return Err(err(format!(
                        "mismatched closing tag: expected </{}>, found </{}>",
                        String::from_utf8_lossy(top),
                        String::from_utf8_lossy(name)
                    )));
                }
                None => {
                    return Err(err(format!(
                        "unexpected closing tag </{}> with no open element",
                        String::from_utf8_lossy(name)
                    )));
                }
            }

            if name == b"item" || name == b"entry" {
                if let Some(cur) = current.take()
                    && let Some(entry) = cur.finish(source)
                {
                    entries.push(entry);
                }
                capturing = None;
            } else if let Some((_, tag)) = capturing
                && name == tag
            {
                capturing = None;
            }
            i = end + 1;
        } else {
            let end = find_byte(bytes, i + 1, b'>')
                .ok_or_else(|| err("unterminated start tag".into()))?;
            let self_closing = end > i + 1 && bytes[end - 1] == b'/';
            let content_end = if self_closing { end - 1 } else { end };
            let tag_bytes = &bytes[i + 1..content_end];
            let name_end = tag_bytes
                .iter()
                .position(u8::is_ascii_whitespace)
                .unwrap_or(tag_bytes.len());
            let name = &tag_bytes[..name_end];

            if !self_closing {
                stack.push(name);
            }

            if name == b"item" || name == b"entry" {
                current = Some(PartialEntry::default());
            } else if name == b"link" && current.is_some() {
                if let Some(href) = find_attr(tag_bytes, b"href") {
                    let cur = current.as_mut().expect("checked is_some above");
                    if cur.url.is_none() {
                        cur.url = Some(href);
                    }
                } else if !self_closing {
                    capturing = Some((Field::Link, name));
                }
            } else if current.is_some()
                && !self_closing
                && let Some(field) = field_for(name)
            {
                capturing = Some((field, name));
            }
            i = end + 1;
        }
    }

    if !stack.is_empty() {
        return Err(err(format!(
            "unexpected end of document with {} unclosed tag(s)",
            stack.len()
        )));
    }

    Ok(entries)
}

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

    #[test]
    fn parses_rss_2_0() {
        let xml = br#"<?xml version="1.0"?>
        <rss version="2.0">
          <channel>
            <title>Feed Title</title>
            <link>https://example.com</link>
            <item>
              <title>Fed holds rates steady</title>
              <link>https://example.com/news/fed-holds-rates</link>
              <pubDate>Sat, 13 Jun 2026 13:30:00 GMT</pubDate>
              <description>Some &amp; summary &lt;text&gt;</description>
            </item>
            <item>
              <title>Second item</title>
              <link>https://example.com/news/second</link>
              <pubDate>Sat, 13 Jun 2026 12:00:00 GMT</pubDate>
              <description><![CDATA[<p>HTML body</p>]]></description>
            </item>
          </channel>
        </rss>"#;

        let entries = parse(xml, "Test").unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].title, "Fed holds rates steady");
        assert_eq!(entries[0].url, "https://example.com/news/fed-holds-rates");
        assert_eq!(entries[0].source, "Test");
        assert_eq!(entries[0].summary.as_deref(), Some("Some & summary <text>"));
        assert_eq!(
            entries[0].published.as_deref(),
            Some("2026-06-13T13:30:00+00:00")
        );
        assert_eq!(entries[1].summary.as_deref(), Some("<p>HTML body</p>"));
    }

    #[test]
    fn parses_atom_1_0() {
        let xml = br#"<?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom">
          <title>Example Feed</title>
          <link href="https://example.com/"/>
          <entry>
            <title>Atom Entry</title>
            <link rel="alternate" href="https://example.com/entry1"/>
            <published>2026-06-13T13:30:00Z</published>
            <updated>2026-06-13T14:00:00Z</updated>
            <summary>An atom summary</summary>
          </entry>
          <entry>
            <title>No summary, has content</title>
            <link href="https://example.com/entry2"/>
            <updated>2026-06-13T15:00:00Z</updated>
            <content type="html">Body content</content>
          </entry>
        </feed>"#;

        let entries = parse(xml, "AtomSource").unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].url, "https://example.com/entry1");
        assert_eq!(
            entries[0].published.as_deref(),
            Some("2026-06-13T13:30:00+00:00")
        );
        assert_eq!(entries[0].summary.as_deref(), Some("An atom summary"));

        assert_eq!(entries[1].url, "https://example.com/entry2");
        // No <published>, falls back to <updated>.
        assert_eq!(
            entries[1].published.as_deref(),
            Some("2026-06-13T15:00:00+00:00")
        );
        // No <summary>, falls back to <content>.
        assert_eq!(entries[1].summary.as_deref(), Some("Body content"));
    }

    #[test]
    fn handles_comments_and_processing_instructions() {
        let xml = br#"<?xml version="1.0"?>
        <!-- top-level comment -->
        <rss version="2.0"><channel>
            <!-- another comment -->
            <item>
              <title>Has PI</title>
              <link>https://example.com/pi</link>
              <?some-pi data?>
              <description>text</description>
            </item>
        </channel></rss>"#;

        let entries = parse(xml, "Test").unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].title, "Has PI");
    }

    #[test]
    fn decodes_numeric_entities() {
        let xml = br#"<rss version="2.0"><channel>
            <item>
              <title>Caf&#233; &#x2014; numeric refs</title>
              <link>https://example.com/numeric</link>
            </item>
        </channel></rss>"#;

        let entries = parse(xml, "Test").unwrap();
        assert_eq!(entries[0].title, "Café — numeric refs");
    }

    #[test]
    fn skips_entries_missing_title_or_link() {
        let xml = br#"<rss version="2.0"><channel>
            <item><link>https://example.com/no-title</link></item>
            <item><title>No link</title></item>
            <item><title>  </title><link>https://example.com/blank-title</link></item>
        </channel></rss>"#;

        let entries = parse(xml, "Test").unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn invalid_date_is_dropped_not_propagated() {
        let xml = br#"<rss version="2.0"><channel>
            <item>
              <title>Bad date</title>
              <link>https://example.com/bad-date</link>
              <pubDate>not a date</pubDate>
            </item>
        </channel></rss>"#;

        let entries = parse(xml, "Test").unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].published, None);
    }

    #[test]
    fn tolerates_pub_date_weekday_mismatch() {
        // 2026-06-13 is actually a Saturday; some feed generators get the
        // weekday label wrong. chrono's strict rfc2822 parser rejects that
        // mismatch outright, so the parser strips the weekday before parsing.
        let xml = br#"<rss version="2.0"><channel>
            <item>
              <title>Wrong weekday label</title>
              <link>https://example.com/wrong-weekday</link>
              <pubDate>Fri, 13 Jun 2026 13:30:00 GMT</pubDate>
            </item>
        </channel></rss>"#;

        let entries = parse(xml, "Test").unwrap();
        assert_eq!(
            entries[0].published.as_deref(),
            Some("2026-06-13T13:30:00+00:00")
        );
    }

    #[test]
    fn malformed_xml_is_an_error() {
        let xml = b"<rss><channel><item><title>Unclosed";
        assert!(parse(xml, "Test").is_err());
    }

    #[test]
    fn mismatched_closing_tag_is_an_error() {
        let xml = b"<rss><channel></item></channel></rss>";
        assert!(parse(xml, "Test").is_err());
    }
}