L18 is our main function, but it seems a little too bulky. We should split the download and installation into separate functions to prevent confusion within our codebase.
L19 and L149 have the same function; we should use some Enum value to condense them into one package (see below for the proposed solution). One problem with this solution is that now we have a constant return value of Vec<String> so we will have to account for that within our other functions
#[derive(Debug)]
enum JsonKey {
BuildSteps,
ExecutableDir,
}
fn get_data_from_json(path: String, key: JsonKey) -> Result<Vec<String>, Box<dyn Error>> {
let file_path = Path::new(&path);
let file = File::open(&file_path)?;
let reader = BufReader::new(file);
let json: Value = serde_json::from_reader(reader)?;
match key {
JsonKey::BuildSteps => {
// Extract the install steps
let steps = json["build_steps"]
.as_array()
.ok_or("build_steps is not an array")?
.iter()
.map(|step| {
step.as_str()
.ok_or("step is not a string")
.map(|s| s.to_string())
})
.collect::<Result<Vec<String>, _>>()?;
Ok(steps)
}
JsonKey::ExecutableDir => {
let execdir = json["executable_dir"].as_str()
.ok_or("Executable dir not found in json")?;
Ok(vec![execdir.to_string()])
}
}
}
L172 should be moved to another function as it is a helper function—something such as helpers.rs or similar.
L18 is our main function, but it seems a little too bulky. We should split the download and installation into separate functions to prevent confusion within our codebase.
L19 and L149 have the same function; we should use some Enum value to condense them into one package (see below for the proposed solution). One problem with this solution is that now we have a constant return value of
Vec<String>so we will have to account for that within our other functionsL172 should be moved to another function as it is a helper function—something such as
helpers.rsor similar.