rustai-core 0.4.0

Zero-cost local research pipeline: async search routing, TLS-fingerprint-aware fetching, SIMD DOM denoising and BM25 context slimming for local LLMs.
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
//! Scholarly sources: arXiv, OpenAlex and Crossref.
//!
//! All three are free, keyless, documented and stable — which is why they are
//! here and a general web search is not a substitute. A query for a paper on a
//! web search engine returns blog posts about the paper; these return the paper.
//!
//! OpenAlex and Crossref both run a "polite pool" that is faster and more
//! reliable for callers who identify themselves. Set
//! [`SearchConfig::contact_email`](crate::search::SearchConfig::contact_email)
//! and they will use it.

use percent_encoding::{NON_ALPHANUMERIC, utf8_percent_encode};
use quick_xml::events::Event;
use quick_xml::{Reader, XmlVersion};

use crate::error::{Error, Result};
use crate::http::Fetcher;
use crate::search::RawHit;
use crate::text;

/// Snippets longer than this are truncated: an abstract is context, not content.
const MAX_SNIPPET_CHARS: usize = 1200;

// ------------------------------------------------------------------- arXiv

/// Search arXiv's Atom API.
pub(crate) async fn arxiv(fetcher: &Fetcher, query: &str, limit: usize) -> Result<Vec<RawHit>> {
    let q = utf8_percent_encode(query, NON_ALPHANUMERIC);
    // `all:` searches title, abstract, authors and comments at once, which is
    // what a natural-language query actually wants.
    let url = format!(
        "https://export.arxiv.org/api/query?search_query=all:{q}&start=0&max_results={limit}\
         &sortBy=relevance&sortOrder=descending"
    );
    let page = fetcher.fetch_api(&url).await?;
    let hits = parse_arxiv(&page.body);
    if hits.is_empty() {
        return Err(Error::provider("arxiv", "no entries in the Atom response"));
    }
    Ok(hits)
}

/// Parse an arXiv Atom feed.
///
/// Written by hand rather than reusing the generic feed reader because arXiv's
/// entries carry authors and a `rel="alternate"` abstract link that the generic
/// path would flatten away — and the author list is most of what makes a
/// scholarly snippet worth reading.
pub(crate) fn parse_arxiv(xml: &str) -> Vec<RawHit> {
    let mut reader = Reader::from_str(xml);
    let config = reader.config_mut();
    config.trim_text(false);
    config.check_end_names = false;
    config.allow_dangling_amp = true;

    let mut hits = Vec::new();
    let mut in_entry = false;
    let mut in_author = false;
    let mut field: Option<&'static str> = None;
    let mut buf = String::new();
    let (mut title, mut link, mut summary, mut published) =
        (String::new(), String::new(), String::new(), String::new());
    let mut authors: Vec<String> = Vec::new();

    while let Ok(event) = reader.read_event() {
        match event {
            Event::Start(e) | Event::Empty(e) => {
                let name = local(e.name().as_ref());
                match name.as_str() {
                    "entry" => {
                        in_entry = true;
                        title.clear();
                        link.clear();
                        summary.clear();
                        published.clear();
                        authors.clear();
                    }
                    "author" if in_entry => in_author = true,
                    "link" if in_entry => {
                        // The alternate link is the abstract page; `related` is
                        // the PDF, which this pipeline cannot read.
                        let rel = attr(&e, "rel").unwrap_or_default();
                        if rel == "alternate"
                            && let Some(href) = attr(&e, "href")
                        {
                            link = href;
                        }
                    }
                    "id" | "title" | "summary" | "published" | "name" if in_entry => {
                        field = Some(match name.as_str() {
                            "id" => "id",
                            "title" => "title",
                            "summary" => "summary",
                            "published" => "published",
                            _ => "name",
                        });
                        buf.clear();
                    }
                    _ => {}
                }
            }
            Event::Text(t) if field.is_some() => {
                buf.push_str(&t.xml_content(XmlVersion::Implicit1_0))
            }
            Event::CData(c) if field.is_some() => buf.push_str(c.as_ref()),
            Event::GeneralRef(r) if field.is_some() => buf.push_str(&resolve_ref(&r)),
            Event::End(e) => {
                let name = local(e.name().as_ref());
                if let Some(key) = field.take() {
                    let value = text::normalize_ws(&buf);
                    match key {
                        "title" if title.is_empty() => title = value,
                        "summary" if summary.is_empty() => summary = value,
                        "published" if published.is_empty() => published = value,
                        "name" if in_author => authors.push(value),
                        "id" if link.is_empty() && value.starts_with("http") => {
                            link = value.replace("http://", "https://");
                        }
                        _ => {}
                    }
                    buf.clear();
                }
                match name.as_str() {
                    "author" => in_author = false,
                    "entry" => {
                        in_entry = false;
                        if !link.is_empty() && !title.is_empty() {
                            hits.push(RawHit {
                                title: title.clone(),
                                url: link.replace("http://", "https://"),
                                snippet: scholarly_snippet(&authors, &published, &summary),
                            });
                        }
                    }
                    _ => {}
                }
            }
            Event::Eof => break,
            _ => {}
        }
    }
    hits
}

// ---------------------------------------------------------------- OpenAlex

/// Search OpenAlex, the open index of ~250M scholarly works.
/// Strip the characters OpenAlex reads as wildcard operators.
///
/// `?` and `*` are wildcards in a `search=` value, and supplying either
/// without an exact-match qualifier is rejected outright:
///
/// ```text
/// 400 {"error":"Invalid query parameters error.",
///      "message":"Wildcards (* or ?) require exact ..."}
/// ```
///
/// Which means a question — "how does BM25 normalise for document length?" —
/// fails on its question mark, and a research query is usually a question.
/// Removing them costs nothing: neither character carries meaning for a
/// relevance search, and the provider offers no way to escape them.
fn openalex_search_term(query: &str) -> String {
    let cleaned: String =
        query.chars().map(|c| if c == '?' || c == '*' { ' ' } else { c }).collect();
    cleaned.split_whitespace().collect::<Vec<_>>().join(" ")
}

pub(crate) async fn openalex(
    fetcher: &Fetcher,
    query: &str,
    limit: usize,
    contact: Option<&str>,
) -> Result<Vec<RawHit>> {
    let term = openalex_search_term(query);
    let q = utf8_percent_encode(&term, NON_ALPHANUMERIC);
    let mut url = format!("https://api.openalex.org/works?search={q}&per-page={limit}");
    if let Some(email) = contact {
        url.push_str(&format!("&mailto={}", utf8_percent_encode(email, NON_ALPHANUMERIC)));
    }
    let page = fetcher.fetch_api(&url).await?;
    parse_openalex(&page.body, limit)
}

/// Parse an OpenAlex `works` response.
pub(crate) fn parse_openalex(body: &str, limit: usize) -> Result<Vec<RawHit>> {
    let value: serde_json::Value = serde_json::from_str(body)
        .map_err(|e| Error::provider("openalex", format!("bad json: {e}")))?;
    let results = value
        .get("results")
        .and_then(|r| r.as_array())
        .ok_or_else(|| Error::provider("openalex", "response had no results array"))?;

    Ok(results
        .iter()
        .filter_map(|w| {
            let title = w.get("display_name")?.as_str()?.to_string();
            // Prefer somewhere the full text can actually be read; fall back to
            // the landing page, then to the DOI resolver.
            let url = w
                .get("best_oa_location")
                .and_then(|l| l.get("landing_page_url"))
                .and_then(|u| u.as_str())
                .or_else(|| w.get("open_access")?.get("oa_url")?.as_str())
                .or_else(|| w.get("primary_location")?.get("landing_page_url")?.as_str())
                .or_else(|| w.get("doi")?.as_str())?
                .to_string();

            let authors: Vec<String> = w
                .get("authorships")
                .and_then(|a| a.as_array())
                .map(|a| {
                    a.iter()
                        .filter_map(|x| {
                            Some(x.get("author")?.get("display_name")?.as_str()?.to_string())
                        })
                        .collect()
                })
                .unwrap_or_default();
            let year = w
                .get("publication_year")
                .and_then(|y| y.as_i64())
                .map(|y| y.to_string())
                .unwrap_or_default();
            let abstract_text = w
                .get("abstract_inverted_index")
                .and_then(|i| i.as_object())
                .map(reconstruct_abstract)
                .unwrap_or_default();

            Some(RawHit { title, url, snippet: scholarly_snippet(&authors, &year, &abstract_text) })
        })
        .take(limit)
        .collect())
}

/// Rebuild a readable abstract from OpenAlex's inverted index.
///
/// OpenAlex stores abstracts as `{word: [positions]}` for licensing reasons.
/// Inverting it back is a few lines and turns an unusable field into the single
/// most informative snippet any provider here returns.
fn reconstruct_abstract(index: &serde_json::Map<String, serde_json::Value>) -> String {
    let mut positioned: Vec<(u64, &str)> = Vec::new();
    for (word, slots) in index {
        let Some(slots) = slots.as_array() else { continue };
        for slot in slots {
            if let Some(p) = slot.as_u64() {
                positioned.push((p, word.as_str()));
            }
        }
    }
    positioned.sort_unstable_by_key(|(p, _)| *p);
    let mut out = String::new();
    for (_, word) in positioned {
        if !out.is_empty() {
            out.push(' ');
        }
        out.push_str(word);
        if out.len() > MAX_SNIPPET_CHARS {
            break;
        }
    }
    out
}

// ---------------------------------------------------------------- Crossref

/// Search Crossref, the DOI registration agency's metadata index.
pub(crate) async fn crossref(
    fetcher: &Fetcher,
    query: &str,
    limit: usize,
    contact: Option<&str>,
) -> Result<Vec<RawHit>> {
    let q = utf8_percent_encode(query, NON_ALPHANUMERIC);
    let mut url = format!("https://api.crossref.org/works?query={q}&rows={limit}");
    if let Some(email) = contact {
        url.push_str(&format!("&mailto={}", utf8_percent_encode(email, NON_ALPHANUMERIC)));
    }
    let page = fetcher.fetch_api(&url).await?;
    parse_crossref(&page.body, limit)
}

/// Parse a Crossref `works` response.
pub(crate) fn parse_crossref(body: &str, limit: usize) -> Result<Vec<RawHit>> {
    let value: serde_json::Value = serde_json::from_str(body)
        .map_err(|e| Error::provider("crossref", format!("bad json: {e}")))?;
    let items = value
        .get("message")
        .and_then(|m| m.get("items"))
        .and_then(|i| i.as_array())
        .ok_or_else(|| Error::provider("crossref", "response had no message.items array"))?;

    Ok(items
        .iter()
        .filter_map(|w| {
            let title = w
                .get("title")
                .and_then(|t| t.as_array())
                .and_then(|t| t.first())
                .and_then(|t| t.as_str())
                .map(text::normalize_ws)
                .filter(|t| !t.is_empty())?;
            let url = w
                .get("resource")
                .and_then(|r| r.get("primary"))
                .and_then(|p| p.get("URL"))
                .and_then(|u| u.as_str())
                .or_else(|| w.get("URL")?.as_str())?
                .to_string();

            let authors: Vec<String> = w
                .get("author")
                .and_then(|a| a.as_array())
                .map(|a| {
                    a.iter()
                        .filter_map(|x| {
                            let family = x.get("family")?.as_str()?;
                            Some(match x.get("given").and_then(|g| g.as_str()) {
                                Some(given) => format!("{given} {family}"),
                                None => family.to_string(),
                            })
                        })
                        .collect()
                })
                .unwrap_or_default();
            let year = w
                .get("issued")
                .and_then(|i| i.get("date-parts"))
                .and_then(|d| d.as_array())
                .and_then(|d| d.first())
                .and_then(|d| d.as_array())
                .and_then(|d| d.first())
                .and_then(|y| y.as_i64())
                .map(|y| y.to_string())
                .unwrap_or_default();
            // Crossref abstracts, when present, are JATS XML fragments.
            let summary =
                w.get("abstract").and_then(|a| a.as_str()).map(strip_tags).unwrap_or_default();

            Some(RawHit { title, url, snippet: scholarly_snippet(&authors, &year, &summary) })
        })
        .take(limit)
        .collect())
}

// ------------------------------------------------------------------ shared

/// `Author, Author et al. (2024). Abstract…` — the shape a reader expects.
fn scholarly_snippet(authors: &[String], date: &str, body: &str) -> String {
    let mut out = String::new();
    if !authors.is_empty() {
        let shown = authors.iter().take(3).cloned().collect::<Vec<_>>().join(", ");
        out.push_str(&shown);
        if authors.len() > 3 {
            out.push_str(" et al.");
        }
    }
    let year: String = date.chars().take_while(|c| c.is_ascii_digit()).collect();
    if !year.is_empty() {
        out.push_str(&format!(" ({year})"));
    }
    if !out.is_empty() && !body.is_empty() {
        out.push_str(". ");
    }
    out.push_str(body);
    truncate(&text::normalize_ws(&out), MAX_SNIPPET_CHARS)
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        return s.to_string();
    }
    let mut out: String = s.chars().take(max).collect();
    out.push('…');
    out
}

fn strip_tags(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut in_tag = false;
    for c in s.chars() {
        match c {
            '<' => in_tag = true,
            '>' => in_tag = false,
            _ if !in_tag => out.push(c),
            _ => {}
        }
    }
    text::normalize_ws(&html_escape::decode_html_entities(&out))
}

fn local(raw: &str) -> String {
    raw.rsplit(':').next().unwrap_or(raw).to_ascii_lowercase()
}

fn attr(e: &quick_xml::events::BytesStart<'_>, key: &str) -> Option<String> {
    e.attributes()
        .flatten()
        .find(|a| local(a.key.as_ref()) == key)
        .map(|a| a.value.as_ref().trim().to_string())
        .filter(|v| !v.is_empty())
}

fn resolve_ref(r: &quick_xml::events::BytesRef<'_>) -> String {
    if let Ok(Some(c)) = r.resolve_char_ref() {
        return c.to_string();
    }
    html_escape::decode_html_entities(&format!("&{};", r.as_ref())).into_owned()
}

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

    const ARXIV: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
      <entry>
        <id>http://arxiv.org/abs/1706.03762v7</id>
        <title>Attention Is All You
  Need</title>
        <summary>The dominant sequence transduction models are based on complex recurrent
  networks &amp; attention.</summary>
        <published>2017-06-12T17:57:34Z</published>
        <author><name>Ashish Vaswani</name></author>
        <author><name>Noam Shazeer</name></author>
        <author><name>Niki Parmar</name></author>
        <author><name>Jakob Uszkoreit</name></author>
        <link href="https://arxiv.org/abs/1706.03762v7" rel="alternate" type="text/html"/>
        <link href="https://arxiv.org/pdf/1706.03762v7" rel="related" type="application/pdf"/>
      </entry>
    </feed>"#;

    #[test]
    fn arxiv_entries_parse_with_authors_and_abstract_links() {
        let hits = parse_arxiv(ARXIV);
        assert_eq!(hits.len(), 1, "{hits:#?}");
        assert_eq!(hits[0].title, "Attention Is All You Need", "newlines were not normalised");
        // The abstract page, never the PDF: this pipeline cannot read a PDF.
        assert_eq!(hits[0].url, "https://arxiv.org/abs/1706.03762v7");
        assert!(
            hits[0].snippet.starts_with("Ashish Vaswani, Noam Shazeer, Niki Parmar et al. (2017).")
        );
        assert!(hits[0].snippet.contains("recurrent networks & attention"));
    }

    #[test]
    fn arxiv_empty_feed_yields_nothing() {
        assert!(parse_arxiv("<feed></feed>").is_empty());
        assert!(parse_arxiv("").is_empty());
    }

    #[test]
    fn openalex_reconstructs_the_inverted_abstract() {
        let body = r#"{"results":[{
            "display_name":"Attention Is All You Need",
            "publication_year":2017,
            "doi":"https://doi.org/10.5555/x",
            "best_oa_location":{"landing_page_url":"https://arxiv.org/abs/1706.03762"},
            "authorships":[{"author":{"display_name":"Ashish Vaswani"}}],
            "abstract_inverted_index":{"The":[0],"dominant":[1],"models":[2],"are":[3],"complex":[4]}
        }]}"#;
        let hits = parse_openalex(body, 10).unwrap();
        assert_eq!(hits[0].url, "https://arxiv.org/abs/1706.03762", "OA link should win");
        assert!(hits[0].snippet.contains("The dominant models are complex"), "{}", hits[0].snippet);
        assert!(hits[0].snippet.starts_with("Ashish Vaswani (2017)."));
    }

    #[test]
    fn openalex_falls_back_to_the_doi() {
        let body = r#"{"results":[{"display_name":"T","doi":"https://doi.org/10.1/x"}]}"#;
        assert_eq!(parse_openalex(body, 5).unwrap()[0].url, "https://doi.org/10.1/x");
        assert!(parse_openalex("{}", 5).is_err());
        assert!(parse_openalex("not json", 5).is_err());
    }

    #[test]
    fn crossref_items_parse() {
        let body = r#"{"message":{"items":[{
            "title":["A Study of Things"],
            "URL":"https://doi.org/10.1/abc",
            "author":[{"given":"Ada","family":"Lovelace"},{"family":"Babbage"}],
            "issued":{"date-parts":[[1843,7]]},
            "abstract":"<jats:p>We show <jats:italic>things</jats:italic>.</jats:p>"
        }]}}"#;
        let hits = parse_crossref(body, 10).unwrap();
        assert_eq!(hits[0].title, "A Study of Things");
        assert_eq!(hits[0].url, "https://doi.org/10.1/abc");
        assert_eq!(hits[0].snippet, "Ada Lovelace, Babbage (1843). We show things.");
    }

    #[test]
    fn crossref_rejects_a_foreign_body() {
        assert!(parse_crossref(r#"{"results":[]}"#, 5).is_err());
    }

    #[test]
    fn snippets_are_truncated() {
        let long = "word ".repeat(600);
        let s = scholarly_snippet(&[], "", &long);
        assert!(s.chars().count() <= MAX_SNIPPET_CHARS + 1);
        assert!(s.ends_with('…'));
    }
}

#[cfg(test)]
mod openalex_query_tests {
    use super::openalex_search_term;

    /// A research query is usually a question, and OpenAlex rejects the
    /// question mark as an unqualified wildcard.
    #[test]
    fn strips_wildcards_openalex_rejects() {
        assert_eq!(
            openalex_search_term("How does BM25 normalise for document length?"),
            "How does BM25 normalise for document length"
        );
        assert_eq!(
            openalex_search_term("BM25는 문서 길이를 정규화하는가?"),
            "BM25는 문서 길이를 정규화하는가"
        );
        assert_eq!(openalex_search_term("wild*card"), "wild card");
        assert_eq!(openalex_search_term("what? why? how?"), "what why how");
    }

    /// Everything else is left alone, including punctuation the API accepts.
    #[test]
    fn leaves_ordinary_queries_untouched() {
        assert_eq!(openalex_search_term("BM25 ranking"), "BM25 ranking");
        assert_eq!(openalex_search_term("Müller & Sons: a study!"), "Müller & Sons: a study!");
    }
}

/// Europe PMC — the life-sciences literature, in one request.
///
/// Preferred over PubMed's E-utilities, which need two round trips (search
/// for identifiers, then fetch their summaries) to reach the same place.
/// Europe PMC indexes PubMed and PMC alongside preprints and patents, and
/// `resultType=core` returns the abstract with the metadata.
pub(crate) async fn europe_pmc(
    fetcher: &Fetcher,
    query: &str,
    limit: usize,
) -> Result<Vec<RawHit>> {
    let q = utf8_percent_encode(query, NON_ALPHANUMERIC);
    let url = format!(
        "https://www.ebi.ac.uk/europepmc/webservices/rest/search\
         ?query={q}&format=json&pageSize={limit}&resultType=core"
    );
    let page = fetcher.fetch_api(&url).await?;
    parse_europe_pmc(&page.body, limit)
}

/// Parse a Europe PMC `search` response.
pub(crate) fn parse_europe_pmc(body: &str, limit: usize) -> Result<Vec<RawHit>> {
    let value: serde_json::Value = serde_json::from_str(body)
        .map_err(|e| Error::provider("europepmc", format!("bad json: {e}")))?;
    let results = value
        .get("resultList")
        .and_then(|l| l.get("result"))
        .and_then(|r| r.as_array())
        .ok_or_else(|| Error::provider("europepmc", "response had no resultList.result"))?;

    Ok(results
        .iter()
        .filter_map(|item| {
            let field = |key: &str| {
                item.get(key).and_then(|v| v.as_str()).map(str::to_string).filter(|s| !s.is_empty())
            };
            let title = field("title")?;
            // Every record has a source and an id; a DOI is not guaranteed,
            // and a PMID even less so for preprints.
            let url = match (field("source"), field("id")) {
                (Some(source), Some(id)) => format!("https://europepmc.org/article/{source}/{id}"),
                _ => format!("https://doi.org/{}", field("doi")?),
            };
            // `authorString` is one comma-separated line, where the shared
            // snippet builder wants the names apart so it can decide where
            // to stop and add "et al.".
            let authors: Vec<String> = field("authorString")
                .unwrap_or_default()
                .split(',')
                .map(|a| a.trim().trim_end_matches('.').to_string())
                .filter(|a| !a.is_empty())
                .collect();
            let year = field("pubYear").unwrap_or_default();
            // Abstracts arrive as JATS, the same markup Crossref sends.
            let body = field("abstractText").map(|a| strip_tags(&a)).unwrap_or_default();
            Some(RawHit {
                title: strip_tags(&title),
                url,
                snippet: scholarly_snippet(&authors, &year, &body),
            })
        })
        .take(limit)
        .collect())
}

#[cfg(test)]
mod europe_pmc_tests {
    use super::parse_europe_pmc;

    #[test]
    fn builds_a_url_from_source_and_id() {
        let body = r#"{"resultList":{"result":[{"id":"42281096","source":"MED",
            "title":"Rare-disease diagnosis","authorString":"Islam MS, Jamal A, Alkhathlan A.",
            "pubYear":"2026","abstractText":"<title>Abstract</title><p>Diagnosis is hard.</p>"}]}}"#;
        let hits = parse_europe_pmc(body, 10).unwrap();
        assert_eq!(hits[0].url, "https://europepmc.org/article/MED/42281096");
        assert!(hits[0].snippet.contains("Islam MS, Jamal A, Alkhathlan A (2026)"));
        assert!(hits[0].snippet.contains("Diagnosis is hard."), "JATS survived");
        assert!(!hits[0].snippet.contains("<p>"));
    }

    /// A preprint has no PMID and often no DOI; source and id always exist.
    #[test]
    fn a_preprint_still_resolves() {
        let body = r#"{"resultList":{"result":[{"id":"PPR1302687","source":"PPR",
            "title":"BM25 and Dense Retrieval Are Complementary","pubYear":"2026"}]}}"#;
        let hits = parse_europe_pmc(body, 10).unwrap();
        assert_eq!(hits[0].url, "https://europepmc.org/article/PPR/PPR1302687");
    }

    #[test]
    fn an_empty_result_list_is_not_an_error() {
        let body = r#"{"resultList":{"result":[]}}"#;
        assert!(parse_europe_pmc(body, 10).unwrap().is_empty());
    }
}