aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/benches/packagist_fixture.rs
blob: 86184879954d5b9e93ac8897d2f3ac0a0c252027 (plain)
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
//! Real packagist p2 metadata for the benchmarks, fetched on first run and cached under the
//! target directory so later runs work offline.

use std::path::PathBuf;

pub fn fetch(package: &str) -> String {
    let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("packagist-p2");
    std::fs::create_dir_all(&dir).expect("failed to create the fixture directory");
    let path = dir.join(format!("{}.json", package.replace('/', "-")));
    if let Ok(fixture) = std::fs::read_to_string(&path) {
        return fixture;
    }

    let url = format!("https://repo.packagist.org/p2/{package}.json");
    let fixture = reqwest::blocking::get(&url)
        .and_then(|response| response.error_for_status())
        .and_then(|response| response.text())
        .unwrap_or_else(|e| {
            panic!(
                "failed to fetch {url}: {e}; download it to {} by hand to run this benchmark offline",
                path.display(),
            )
        });
    std::fs::write(&path, &fixture).expect("failed to cache the fixture");
    fixture
}