(
subdir: &str,
)
| 41 | impl Error for LoadConfigError {} |
| 42 | |
| 43 | fn load_json_configs<T: DeserializeOwned>( |
| 44 | subdir: &str, |
| 45 | ) -> Result<HashMap<String, T>, Report<[LoadConfigError]>> { |
| 46 | // Real configs live under benches/config/<category> |
| 47 | let dir = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 48 | .join("config") |
| 49 | .join("producers") |
| 50 | .join(subdir); |
| 51 | |
| 52 | fs::read_dir(&dir) |
| 53 | .change_context(LoadConfigError::Io)? |
| 54 | .map(|entry| { |
| 55 | let entry = entry.change_context(LoadConfigError::Io)?; |
| 56 | if entry |
| 57 | .file_type() |
| 58 | .change_context(LoadConfigError::Io)? |
| 59 | .is_dir() |
| 60 | { |
| 61 | return Ok::<_, Report<LoadConfigError>>(None); |
| 62 | } |
| 63 | let path = entry.path(); |
| 64 | if path.extension().and_then(|ext| ext.to_str()) != Some("json") { |
| 65 | return Ok(None); |
| 66 | } |
| 67 | let stem = path |
| 68 | .file_stem() |
| 69 | .expect("file has stem") |
| 70 | .to_string_lossy() |
| 71 | .into_owned(); |
| 72 | let file = File::open(&path).change_context(LoadConfigError::Io)?; |
| 73 | let reader = BufReader::new(file); |
| 74 | let cfg = serde_json::from_reader(reader) |
| 75 | .change_context(LoadConfigError::Parse) |
| 76 | .attach_with(|| format!("Failed to parse config file: `{}`", path.display()))?; |
| 77 | let key = format!("{subdir}/{stem}"); |
| 78 | Ok(Some((key, cfg))) |
| 79 | }) |
| 80 | .filter_map(Result::transpose) |
| 81 | .try_collect_reports() |
| 82 | } |
| 83 | |
| 84 | pub(crate) fn load_configs_map<T: DeserializeOwned>(subdir: &str) -> HashMap<String, T> { |
| 85 | load_json_configs(subdir).expect("should be able to collect configs") |
no test coverage detected