criterion/
fs.rs

1use serde::de::DeserializeOwned;
2use serde::Serialize;
3use std::ffi::OsStr;
4use std::fs::{self, File};
5use std::io::Read;
6use std::path::Path;
7use walkdir::{DirEntry, WalkDir};
8
9use crate::error::{Error, Result};
10use crate::report::BenchmarkId;
11
12pub fn load<A, P: ?Sized>(path: &P) -> Result<A>
13where
14    A: DeserializeOwned,
15    P: AsRef<Path>,
16{
17    let path = path.as_ref();
18    let mut f = File::open(path).map_err(|inner| Error::AccessError {
19        inner,
20        path: path.to_owned(),
21    })?;
22    let mut string = String::new();
23    let _ = f.read_to_string(&mut string);
24    let result: A = serde_json::from_str(string.as_str()).map_err(|inner| Error::SerdeError {
25        inner,
26        path: path.to_owned(),
27    })?;
28
29    Ok(result)
30}
31
32pub fn is_dir<P>(path: &P) -> bool
33where
34    P: AsRef<Path>,
35{
36    let path: &Path = path.as_ref();
37    path.is_dir()
38}
39
40pub fn mkdirp<P>(path: &P) -> Result<()>
41where
42    P: AsRef<Path>,
43{
44    fs::create_dir_all(path.as_ref()).map_err(|inner| Error::AccessError {
45        inner,
46        path: path.as_ref().to_owned(),
47    })?;
48    Ok(())
49}
50
51pub fn cp(from: &Path, to: &Path) -> Result<()> {
52    fs::copy(from, to).map_err(|inner| Error::CopyError {
53        inner,
54        from: from.to_owned(),
55        to: to.to_owned(),
56    })?;
57    Ok(())
58}
59
60pub fn save<D, P>(data: &D, path: &P) -> Result<()>
61where
62    D: Serialize,
63    P: AsRef<Path>,
64{
65    let buf = serde_json::to_string(&data).map_err(|inner| Error::SerdeError {
66        path: path.as_ref().to_owned(),
67        inner,
68    })?;
69    save_string(&buf, path)
70}
71
72pub fn save_string<P>(data: &str, path: &P) -> Result<()>
73where
74    P: AsRef<Path>,
75{
76    use std::io::Write;
77
78    File::create(path)
79        .and_then(|mut f| f.write_all(data.as_bytes()))
80        .map_err(|inner| Error::AccessError {
81            inner,
82            path: path.as_ref().to_owned(),
83        })?;
84
85    Ok(())
86}
87
88pub fn list_existing_benchmarks<P>(directory: &P) -> Result<Vec<BenchmarkId>>
89where
90    P: AsRef<Path>,
91{
92    fn is_benchmark(entry: &DirEntry) -> bool {
93        // Look for benchmark.json files inside folders named "new" (because we want to ignore
94        // the baselines)
95        entry.file_name() == OsStr::new("benchmark.json")
96            && entry.path().parent().unwrap().file_name().unwrap() == OsStr::new("new")
97    }
98
99    let mut ids = vec![];
100
101    for entry in WalkDir::new(directory)
102        .into_iter()
103        // Ignore errors.
104        .filter_map(::std::result::Result::ok)
105        .filter(is_benchmark)
106    {
107        let id: BenchmarkId = load(entry.path())?;
108        ids.push(id);
109    }
110
111    Ok(ids)
112}