-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Hoist implementation of tool
- Loading branch information
Showing
4 changed files
with
106 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use parsing::ParserConfiguration; | ||
|
||
#[derive(Debug)] | ||
pub enum ExecutionError { | ||
ParsingError, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ExecutionResult { | ||
WithConflicts(String), | ||
WithoutConflicts(String), | ||
} | ||
|
||
impl ToString for ExecutionResult { | ||
fn to_string(&self) -> String { | ||
match self { | ||
ExecutionResult::WithConflicts(value) => value.to_owned(), | ||
ExecutionResult::WithoutConflicts(value) => value.to_owned(), | ||
} | ||
} | ||
} | ||
|
||
pub fn run_tool_on_merge_scenario( | ||
language: model::Language, | ||
base: &str, | ||
left: &str, | ||
right: &str, | ||
) -> Result<ExecutionResult, ExecutionError> { | ||
if base == left { | ||
return Ok(ExecutionResult::WithoutConflicts(right.to_string())); | ||
} | ||
|
||
if base == right { | ||
return Ok(ExecutionResult::WithoutConflicts(left.to_string())); | ||
} | ||
|
||
let parser_configuration = ParserConfiguration::from(language); | ||
|
||
let base_tree = parsing::parse_string(&base, &parser_configuration) | ||
.map_err(|_| ExecutionError::ParsingError)?; | ||
let left_tree = parsing::parse_string(&left, &parser_configuration) | ||
.map_err(|_| ExecutionError::ParsingError)?; | ||
let right_tree = parsing::parse_string(&right, &parser_configuration) | ||
.map_err(|_| ExecutionError::ParsingError)?; | ||
|
||
let matchings_left_base = matching::calculate_matchings(&left_tree, &base_tree); | ||
let matchings_right_base = matching::calculate_matchings(&right_tree, &base_tree); | ||
let matchings_left_right = matching::calculate_matchings(&left_tree, &right_tree); | ||
|
||
let result = merge::merge( | ||
&base_tree, | ||
&left_tree, | ||
&right_tree, | ||
&matchings_left_base, | ||
&matchings_right_base, | ||
&matchings_left_right, | ||
); | ||
|
||
match has_conflict(&result) { | ||
true => Ok(ExecutionResult::WithConflicts(result.to_string())), | ||
false => Ok(ExecutionResult::WithoutConflicts(result.to_string())), | ||
} | ||
} | ||
|
||
fn has_conflict(result: &merge::MergedCSTNode) -> bool { | ||
match result { | ||
merge::MergedCSTNode::NonTerminal { children, .. } => { | ||
children.into_iter().any(|child| has_conflict(child)) | ||
} | ||
merge::MergedCSTNode::Terminal { .. } => false, | ||
merge::MergedCSTNode::Conflict { .. } => true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,40 @@ | ||
mod cli_args; | ||
mod parser_configuration; | ||
mod control; | ||
mod language; | ||
|
||
use clap::Parser; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let args = cli_args::CliArgs::parse(); | ||
|
||
let base = std::fs::read_to_string(&args.base_path)?; | ||
let left = std::fs::read_to_string(args.left_path)?; | ||
let right = std::fs::read_to_string(args.right_path)?; | ||
mod cli_exit_codes { | ||
pub const SUCCESS_WITHOUT_CONFLICTS: i32 = 0; | ||
pub const SUCCESS_WITH_CONFLICTS: i32 = 1; | ||
|
||
let parser_configuration = | ||
parser_configuration::get_parser_configuration_by_file_path(&args.base_path)?; | ||
|
||
if base == left { | ||
std::fs::write(args.merge_path, right)?; | ||
return Ok(()); | ||
} | ||
pub const READING_FILE_ERROR: i32 = 129; | ||
pub const GUESS_LANGUAGE_ERROR: i32 = 130; | ||
pub const WRITING_FILE_ERROR: i32 = 131; | ||
pub const INTERNAL_EXECUTION_ERROR: i32 = 132; | ||
} | ||
|
||
if base == right { | ||
std::fs::write(args.merge_path, left)?; | ||
return Ok(()); | ||
} | ||
fn main() { | ||
let args = cli_args::CliArgs::parse(); | ||
|
||
let base_tree = parsing::parse_string(&base, &parser_configuration).unwrap(); | ||
let left_tree = parsing::parse_string(&left, &parser_configuration).unwrap(); | ||
let right_tree = parsing::parse_string(&right, &parser_configuration).unwrap(); | ||
let base = std::fs::read_to_string(&args.base_path) | ||
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::READING_FILE_ERROR)); | ||
let left = std::fs::read_to_string(args.left_path) | ||
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::READING_FILE_ERROR)); | ||
let right = std::fs::read_to_string(args.right_path) | ||
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::READING_FILE_ERROR)); | ||
|
||
let matchings_left_base = matching::calculate_matchings(&left_tree, &base_tree); | ||
let matchings_right_base = matching::calculate_matchings(&right_tree, &base_tree); | ||
let matchings_left_right = matching::calculate_matchings(&left_tree, &right_tree); | ||
let language = language::get_language_by_file_path(&args.base_path) | ||
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::GUESS_LANGUAGE_ERROR)); | ||
|
||
let result = merge::merge( | ||
&base_tree, | ||
&left_tree, | ||
&right_tree, | ||
&matchings_left_base, | ||
&matchings_right_base, | ||
&matchings_left_right, | ||
); | ||
let result = control::run_tool_on_merge_scenario(language, &base, &left, &right) | ||
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::INTERNAL_EXECUTION_ERROR)); | ||
|
||
std::fs::write(args.merge_path, result.to_string())?; | ||
std::fs::write(args.merge_path, result.to_string()) | ||
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::WRITING_FILE_ERROR)); | ||
|
||
Ok(()) | ||
match result { | ||
control::ExecutionResult::WithConflicts(_) => std::process::exit(cli_exit_codes::SUCCESS_WITH_CONFLICTS), | ||
control::ExecutionResult::WithoutConflicts(_) => std::process::exit(cli_exit_codes::SUCCESS_WITHOUT_CONFLICTS), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters