From d0ef5c17abc28185e94397278f2952652b11b2d6 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Thu, 29 Feb 2024 23:38:57 -0300 Subject: [PATCH] feat(bin): Receive language as an input instead of guessing it --- bin/src/cli_args.rs | 4 ++++ bin/src/language.rs | 35 ++++------------------------------- bin/src/main.rs | 2 +- bin/tests/cli.rs | 2 ++ 4 files changed, 11 insertions(+), 32 deletions(-) diff --git a/bin/src/cli_args.rs b/bin/src/cli_args.rs index 44f601e..6837ce2 100644 --- a/bin/src/cli_args.rs +++ b/bin/src/cli_args.rs @@ -18,4 +18,8 @@ pub struct CliArgs { /// Path where the merged file should be written #[arg(short, long)] pub(crate) merge_path: std::path::PathBuf, + + /// The language that the files being merged are written in + #[arg(long)] + pub(crate) language: String, } diff --git a/bin/src/language.rs b/bin/src/language.rs index a7528cd..41b88ee 100644 --- a/bin/src/language.rs +++ b/bin/src/language.rs @@ -1,33 +1,6 @@ -pub fn get_language_by_file_path(file_path: &std::path::Path) -> Result { - file_path - .extension() - .and_then(std::ffi::OsStr::to_str) - .and_then(|extension| match extension { - "java" => Some(model::Language::Java), - _ => None, - }) - .ok_or(format!( - "Could not retrieve parsing configuration for file {}", - file_path.display() - )) -} - -#[cfg(test)] -mod tests { - use crate::language::get_language_by_file_path; - - #[test] - fn if_the_file_extension_has_no_parser_available_it_returns_error() { - let file_path = std::path::PathBuf::from("/path/without/extension"); - assert!(get_language_by_file_path(&file_path).is_err()) - } - - #[test] - fn if_the_file_extension_has_a_parser_available_it_returns_a_parser_configuration() { - let file_path = std::path::PathBuf::from("/path/for/java/file/Example.java"); - assert_eq!( - get_language_by_file_path(&file_path).unwrap(), - model::Language::Java - ) +pub fn get_language_from_name(name: &str) -> Result { + match name { + "java" => Ok(model::Language::Java), + _ => Err("Invalid language provided"), } } diff --git a/bin/src/main.rs b/bin/src/main.rs index 85e2402..4c2cc0c 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -23,7 +23,7 @@ fn main() { std::process::exit(cli_exit_codes::READING_FILE_ERROR) }); - let language = language::get_language_by_file_path(&args.base_path).unwrap_or_else(|error| { + let language = language::get_language_from_name(&args.language).unwrap_or_else(|error| { log::error!("Error while guessing language: {}", error); std::process::exit(cli_exit_codes::GUESS_LANGUAGE_ERROR) }); diff --git a/bin/tests/cli.rs b/bin/tests/cli.rs index df15a20..e446ae7 100644 --- a/bin/tests/cli.rs +++ b/bin/tests/cli.rs @@ -9,6 +9,7 @@ fn if_there_is_a_conflict_it_returns_valid_exit_code() { .arg("--left-path=tests/scenarios/smoke_java/left.java") .arg("--right-path=tests/scenarios/smoke_java/right.java") .arg("--merge-path=tests/scenarios/smoke_java/merge.java") + .arg("--language=java") .assert() .code(bin::SUCCESS_WITH_CONFLICTS); } @@ -20,6 +21,7 @@ fn if_there_is_no_conflict_it_returns_valid_exit_code() { .arg("--left-path=tests/scenarios/no_conflicts/left.java") .arg("--right-path=tests/scenarios/no_conflicts/right.java") .arg("--merge-path=tests/scenarios/no_conflicts/merge.java") + .arg("--language=java") .assert() .code(bin::SUCCESS_WITHOUT_CONFLICTS); }