Skip to content

Commit

Permalink
51: Use Result instead of exiting from config mod
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusedwardhaslam committed Oct 20, 2023
1 parent 58462e2 commit dffdab5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
19 changes: 10 additions & 9 deletions crates/fta/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::structs::FtaConfig;
use std::fs::File;
use std::io::Read;
use std::io::{Error, Read};
use std::path::Path;
use std::process::exit;

mod tests;

pub fn read_config(config_path: String, path_specified_by_user: bool) -> FtaConfig {
pub fn read_config(config_path: String, path_specified_by_user: bool) -> Result<FtaConfig, Error> {
let default_config = FtaConfig {
extensions: Some(vec![
".js".to_string(),
Expand Down Expand Up @@ -35,7 +34,7 @@ pub fn read_config(config_path: String, path_specified_by_user: bool) -> FtaConf
file.read_to_string(&mut content).unwrap();
let provided_config: FtaConfig = serde_json::from_str(&content).unwrap_or_default();

return FtaConfig {
return Result::Ok(FtaConfig {
extensions: {
let mut extensions = default_config.extensions.unwrap();
if let Some(mut provided) = provided_config.extensions {
Expand All @@ -62,13 +61,15 @@ pub fn read_config(config_path: String, path_specified_by_user: bool) -> FtaConf
include_comments: provided_config
.include_comments
.or(default_config.include_comments),
};
});
}

if path_specified_by_user {
eprintln!("Config file not found at {}", config_path);
exit(1);
if !path_specified_by_user {
return Result::Ok(default_config);
}

default_config
Result::Err(Error::new(
std::io::ErrorKind::NotFound,
format!("Config file not found at: {}", config_path),
))
}
8 changes: 4 additions & 4 deletions crates/fta/src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod tests {
let temp_file = create_temp_file(valid_json);
let path = temp_file.path().to_str().unwrap();

let config = read_config(path.to_string(), false);
let config = read_config(path.to_string(), false).unwrap();

assert_eq!(
config.extensions,
Expand Down Expand Up @@ -73,7 +73,7 @@ mod tests {
let temp_file = create_temp_file(partial_json);
let path = temp_file.path().to_str().unwrap();

let config = read_config(path.to_string(), false);
let config = read_config(path.to_string(), false).unwrap();

assert_eq!(
config.extensions,
Expand Down Expand Up @@ -110,7 +110,7 @@ mod tests {
fn test_read_config_with_nonexistent_file() {
let nonexistent_path = "nonexistent_file.json";

let config = read_config(nonexistent_path.to_string(), false);
let config = read_config(nonexistent_path.to_string(), false).unwrap();

assert_eq!(
config.extensions,
Expand Down Expand Up @@ -156,7 +156,7 @@ mod tests {
let temp_file = create_temp_file(valid_json);
let path = temp_file.path().to_str().unwrap();

let config = read_config(path.to_string(), true);
let config = read_config(path.to_string(), true).unwrap();

assert_eq!(
config.extensions,
Expand Down
15 changes: 12 additions & 3 deletions crates/fta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ fn do_analysis(
}
}

pub fn analyze(repo_path: &String, config_path: Option<String>) -> Vec<FileData> {
pub fn analyze(
repo_path: &String,
config_path: Option<String>,
) -> Result<Vec<FileData>, std::io::Error> {
// Initialize the logger
let mut builder = env_logger::Builder::new();

Expand All @@ -128,7 +131,13 @@ pub fn analyze(repo_path: &String, config_path: Option<String>) -> Vec<FileData>
Some(config_path_arg) => (config_path_arg, true),
None => (format!("{}/fta.json", repo_path), false),
};
let config = read_config(config_path, path_specified_by_user);

let config = match read_config(config_path, path_specified_by_user) {
Ok(config) => config,
Err(err) => {
return Err(err);
}
};

let walk = WalkBuilder::new(repo_path)
.git_ignore(true)
Expand Down Expand Up @@ -182,5 +191,5 @@ pub fn analyze(repo_path: &String, config_path: Option<String>) -> Vec<FileData>
}
});

return file_data_list;
return Ok(file_data_list);
}
8 changes: 7 additions & 1 deletion crates/fta/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ pub fn main() {

let cli = Cli::parse();

let mut findings = analyze(&cli.project, cli.config_path);
let mut findings = match analyze(&cli.project, cli.config_path) {
Ok(findings) => findings,
Err(err) => {
eprintln!("{}", err);
std::process::exit(1);
}
};

findings.sort_unstable_by(|a, b| b.fta_score.partial_cmp(&a.fta_score).unwrap());

Expand Down

0 comments on commit dffdab5

Please sign in to comment.