MCPcopy Create free account
hub / github.com/hashintel/hash / load_json_configs

Function load_json_configs

tests/graph/benches/graph/config/mod.rs:43–82  ·  view source on GitHub ↗
(
    subdir: &str,
)

Source from the content-addressed store, hash-verified

41impl Error for LoadConfigError {}
42
43fn 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
84pub(crate) fn load_configs_map<T: DeserializeOwned>(subdir: &str) -> HashMap<String, T> {
85 load_json_configs(subdir).expect("should be able to collect configs")

Callers 1

load_configs_mapFunction · 0.85

Calls 10

OkInterface · 0.85
pathMethod · 0.80
to_strMethod · 0.80
joinMethod · 0.45
try_collect_reportsMethod · 0.45
mapMethod · 0.45
change_contextMethod · 0.45
into_ownedMethod · 0.45
expectMethod · 0.45
attach_withMethod · 0.45

Tested by

no test coverage detected