searchfox_lib/
file_reader.rs1use crate::client::SearchfoxClient;
2use crate::utils::searchfox_url_repo;
3use anyhow::Result;
4use scraper::{Html, Selector};
5
6impl SearchfoxClient {
7 pub async fn get_file(&self, path: &str) -> Result<String> {
8 let url = format!(
9 "https://searchfox.org/{}/source/{}",
10 searchfox_url_repo(&self.repo),
11 path
12 );
13 let html = self.get_html(&url).await?;
14 let document = Html::parse_document(&html);
15 let selector = Selector::parse("code.source-line").expect("valid selector");
16 let lines: Vec<String> = document
17 .select(&selector)
18 .map(|el| el.text().collect::<String>())
19 .collect();
20 if lines.is_empty() {
21 anyhow::bail!("Could not find file content at {}", url);
22 }
23 Ok(lines.join(""))
24 }
25}