kermit_bench/
manager.rs

1use {
2    super::{benchmarks::Benchmark, downloader::Downloader},
3    std::path::PathBuf,
4};
5
6pub struct BenchmarkManager {
7    dir: PathBuf,
8    datasets: Vec<Benchmark>,
9}
10
11impl BenchmarkManager {
12    pub fn new<P: Into<PathBuf>>(benchmark_dir: P) -> Self {
13        let path = benchmark_dir.into();
14        if !path.exists() {
15            std::fs::create_dir_all(&path).expect("Failed to create dataset directory");
16        }
17        Self {
18            dir: path,
19            datasets: vec![],
20        }
21    }
22
23    pub fn add_benchmark(
24        &mut self, benchmark: Benchmark,
25    ) -> Result<(), Box<dyn std::error::Error>> {
26        if self.datasets.iter().any(|d| d == &benchmark) {
27            let name = benchmark.config().metadata().name;
28            return Err(format!("Benchmark '{}' already exists in manager", name).into());
29        }
30
31        let config = benchmark.config();
32        let dl_spec = &config.metadata().download_spec;
33        let source = Downloader::download(dl_spec)?;
34        config.load(&source, self.dir.as_path())?;
35        Downloader::clean(dl_spec);
36        self.datasets.push(benchmark);
37        Ok(())
38    }
39
40    pub fn rm_benchmark(&mut self, benchmark: Benchmark) -> Result<(), Box<dyn std::error::Error>> {
41        let config = benchmark.config();
42        let dl_spec = &config.metadata().download_spec;
43        let dest = self.dir.join(dl_spec.name);
44        if dest.exists() {
45            std::fs::remove_dir_all(&dest)?;
46        }
47        self.datasets.retain(|d| d != &benchmark);
48        Ok(())
49    }
50}