Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify code
Browse files Browse the repository at this point in the history
jpedroh committed Jan 5, 2024
1 parent a2d2479 commit bf3e722
Showing 4 changed files with 37 additions and 105 deletions.
93 changes: 20 additions & 73 deletions matching_handlers/src/java/field_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use super::utils::find_identifier;
use model::{
cst_node::{NonTerminal, Terminal},
CSTNode,
};

fn find_variable_declarator<'a>(
node_children: &'a Vec<CSTNode<'a>>,
) -> Option<&'a NonTerminal<'a>> {
node_children
.iter()
.find(|node| node.kind() == "variable_declarator")
.and_then(|node| match node {
CSTNode::NonTerminal(non_terminal) => Some(non_terminal),
CSTNode::Terminal(_) => None,
})
}

pub fn compute_matching_score_for_field_declaration<'a>(
left: &'a CSTNode,
right: &'a CSTNode,
) -> usize {
match (left, right) {
(
CSTNode::Terminal(Terminal {
kind: kind_left,
value: value_left,
..
}),
CSTNode::Terminal(Terminal {
kind: kind_right,
value: value_right,
..
}),
) => (kind_left == kind_right && value_left == value_right).into(),
(
CSTNode::NonTerminal(NonTerminal {
children: children_left,
@@ -31,68 +32,14 @@ pub fn compute_matching_score_for_field_declaration<'a>(
}),
) => {
// Try to find an identifier on children, and compare them
let variable_declarator_left = children_left
.iter()
.find(|node| match node {
CSTNode::NonTerminal(NonTerminal { kind, .. }) => {
kind == &"variable_declarator"
}
_ => false,
})
.map(|node| match node {
CSTNode::NonTerminal(non_terminal) => non_terminal,
CSTNode::Terminal(_) => panic!("Invalid configuration reached"),
});

let variable_declarator_right = children_right
.iter()
.find(|node| match node {
CSTNode::NonTerminal(NonTerminal { kind, .. }) => {
kind == &"variable_declarator"
}
_ => false,
})
.map(|node| match node {
CSTNode::NonTerminal(non_terminal) => non_terminal,
CSTNode::Terminal(_) => panic!("Invalid configuration reached"),
});

// Try to find an identifier on children, and compare them
let identifier_left = variable_declarator_left
.unwrap()
.children
.iter()
.find(|node| match node {
CSTNode::Terminal(Terminal { kind, .. }) => kind == &"identifier",
_ => false,
});

let identifier_right =
variable_declarator_right
.unwrap()
.children
.iter()
.find(|node| match node {
CSTNode::Terminal(Terminal { kind, .. }) => kind == &"identifier",
_ => false,
});
let identifier_left = find_variable_declarator(&children_left)
.and_then(|node| find_identifier(&node.children))
.map(|node| node.value);
let identifier_right = find_variable_declarator(&children_right)
.and_then(|node| find_identifier(&node.children))
.map(|node| node.value);

match (identifier_left, identifier_right) {
(Some(identifier_left), Some(identifier_right)) => {
match (identifier_left, identifier_right) {
(
CSTNode::Terminal(Terminal {
value: value_left, ..
}),
CSTNode::Terminal(Terminal {
value: value_right, ..
}),
) if value_left == value_right => 1,
(_, _) => 0,
}
}
(_, _) => 0,
}
(identifier_left.is_some() && identifier_left == identifier_right).into()
}
(_, _) => 0,
}
37 changes: 5 additions & 32 deletions matching_handlers/src/java/method_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use model::{
cst_node::{NonTerminal, Terminal},
CSTNode,
};
use super::utils::find_identifier;
use model::{cst_node::NonTerminal, CSTNode};

pub fn compute_matching_score_for_method_declaration<'a>(
left: &'a CSTNode,
@@ -19,35 +17,10 @@ pub fn compute_matching_score_for_method_declaration<'a>(
}),
) => {
// Try to find an identifier on children, and compare them
let identifier_left = children_left
.iter()
.find(|node| node.kind() == "identifier")
.and_then(|node| match node {
CSTNode::Terminal(terminal) => Some(terminal),
CSTNode::NonTerminal(_) => None,
});
let identifier_left = find_identifier(children_left).map(|node| node.value);
let identifier_right = find_identifier(children_right).map(|node| node.value);

let identifier_right = children_right
.iter()
.find(|node| node.kind() == "identifier")
.and_then(|node| match node {
CSTNode::Terminal(terminal) => Some(terminal),
CSTNode::NonTerminal(_) => None,
});

match (identifier_left, identifier_right) {
(
Some(Terminal {
value: value_left, ..
}),
Some(Terminal {
value: value_right, ..
}),
) => {
return (value_left == value_right).into();
}
(_, _) => 0,
}
(identifier_left.is_some() && identifier_left == identifier_right).into()
}
(_, _) => 0,
}
1 change: 1 addition & 0 deletions matching_handlers/src/java/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod field_declaration;
mod method_declaration;
mod utils;

use crate::MatchingHandlers;

11 changes: 11 additions & 0 deletions matching_handlers/src/java/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use model::{cst_node::Terminal, CSTNode};

pub fn find_identifier<'a>(node_children: &'a Vec<CSTNode<'a>>) -> Option<&'a Terminal<'a>> {
node_children
.iter()
.find(|node| node.kind() == "identifier")
.and_then(|node| match node {
CSTNode::Terminal(terminal) => Some(terminal),
CSTNode::NonTerminal(_) => None,
})
}

0 comments on commit bf3e722

Please sign in to comment.