Skip to content

Commit

Permalink
feat: move behavior to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jul 20, 2024
1 parent c4943ab commit 76756d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
30 changes: 22 additions & 8 deletions matching/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod matches;
mod matching;
pub mod matching_configuration;
mod matching_entry;
Expand All @@ -11,24 +12,37 @@ pub use matchings::Matchings;
use model::{cst_node::Terminal, CSTNode};
use unordered_pair::UnorderedPair;

trait MatchingRepresentation {
fn get_matching_representation(&self) -> Vec<&str>;
pub trait Matches {
fn matches(&self, right: &CSTNode) -> bool;
}

impl MatchingRepresentation for CSTNode<'_> {
fn get_matching_representation(&self) -> Vec<&str> {
self.get_identifier()
.map(|f| f.to_owned())
.unwrap_or(vec![self.kind()])
impl Matches for CSTNode<'_> {
fn matches(&self, right: &CSTNode) -> bool {
match (self, right) {
(CSTNode::Terminal(left), CSTNode::Terminal(right)) => {
left.get_identifier() == right.get_identifier()
}
(CSTNode::NonTerminal(left), CSTNode::NonTerminal(right)) => {
if let (Some(left_identifier), Some(right_identifier)) =
(left.get_identifier(), right.get_identifier())
{
left_identifier == right_identifier
} else {
left.kind == right.kind
}
}
(_, _) => false,
}
}
}


pub fn calculate_matchings<'a>(
left: &'a model::CSTNode,
right: &'a model::CSTNode,
config: &'a MatchingConfiguration<'a>,
) -> Matchings<'a> {
if left.get_matching_representation() != right.get_matching_representation() {
if !left.matches(&right) {

Check failure on line 45 in matching/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler
return Matchings::empty();
}

Expand Down
25 changes: 25 additions & 0 deletions matching/src/matches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use model::CSTNode;

pub trait Matches {
fn matches(&self, right: &CSTNode) -> bool;

Check failure on line 4 in matching/src/matches.rs

View workflow job for this annotation

GitHub Actions / clippy

method `matches` is never used

Check warning on line 4 in matching/src/matches.rs

View workflow job for this annotation

GitHub Actions / build

method `matches` is never used
}

impl Matches for CSTNode<'_> {
fn matches(&self, right: &CSTNode) -> bool {
match (self, right) {
(CSTNode::Terminal(left), CSTNode::Terminal(right)) => {
left.get_identifier() == right.get_identifier()
}
(CSTNode::NonTerminal(left), CSTNode::NonTerminal(right)) => {
if let (Some(left_identifier), Some(right_identifier)) =
(left.get_identifier(), right.get_identifier())
{
left_identifier == right_identifier
} else {
left.kind == right.kind
}
}
(_, _) => false,
}
}
}
7 changes: 3 additions & 4 deletions matching/src/ordered/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry,
MatchingRepresentation, Matchings,
matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry, Matches,
Matchings,
};
use model::{cst_node::NonTerminal, CSTNode};
use unordered_pair::UnorderedPair;
Expand Down Expand Up @@ -37,8 +37,7 @@ pub fn calculate_matchings<'a>(
..
}),
) => {
let root_matching: usize =
(left.get_matching_representation() == right.get_matching_representation()).into();
let root_matching: usize = (left.matches(right)).into();

let m = children_left.len();
let n = children_right.len();
Expand Down

0 comments on commit 76756d8

Please sign in to comment.