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