Skip to content

Commit

Permalink
Format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed Dec 24, 2024
1 parent 579f159 commit 904ae5f
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/translator/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::collections::HashMap;

use crate::parser::{Pattern, Patterns};

use super::{boundaries::{number_word, word_end, word_number, word_start}, Translation};
use super::{
boundaries::{number_word, word_end, word_number, word_start},
Translation,
};

#[derive(Debug, Default)]
pub struct Node {
Expand Down Expand Up @@ -45,7 +48,7 @@ impl Graph {

fn add_node(&mut self) -> NodeId {
self.nodes.push(Node::default());
self.nodes.len()-1 // because we added an empty root node
self.nodes.len() - 1 // because we added an empty root node
}

fn add_edge(&mut self, from: NodeId, transition: Transition, to: NodeId) {
Expand Down Expand Up @@ -122,9 +125,9 @@ impl Graph {
current_node = *node;
}
None => {
let next_node = self.add_node();
self.add_edge(current_node, Transition::Character(c), next_node);
current_node = next_node;
let next_node = self.add_node();
self.add_edge(current_node, Transition::Character(c), next_node);
current_node = next_node;
}
}
length += 1;
Expand Down Expand Up @@ -156,7 +159,11 @@ impl Graph {
// add an epsilon transition to mark the end of the
// pre-pattern. We need to know where the real match starts
let next_node = self.add_node();
self.add_edge(current_node, Transition::End(Boundary::PrePattern), next_node);
self.add_edge(
current_node,
Transition::End(Boundary::PrePattern),
next_node,
);
current_node = next_node;

for c in from.chars() {
Expand Down Expand Up @@ -275,7 +282,10 @@ impl Graph {
));
}
}
if let Some(node_id) = self.edges.get(&(node_id, Transition::Start(Boundary::Word))) {
if let Some(node_id) = self
.edges
.get(&(node_id, Transition::Start(Boundary::Word)))
{
if word_start(prev, c) {
matching_rules.extend(self.find_translations_from_node(
input,
Expand All @@ -286,7 +296,10 @@ impl Graph {
));
}
}
if let Some(node_id) = self.edges.get(&(node_id, Transition::Start(Boundary::NotWord))) {
if let Some(node_id) = self
.edges
.get(&(node_id, Transition::Start(Boundary::NotWord)))
{
if !word_start(prev, c) {
matching_rules.extend(self.find_translations_from_node(
input,
Expand All @@ -308,7 +321,10 @@ impl Graph {
));
}
}
if let Some(node_id) = self.edges.get(&(node_id, Transition::End(Boundary::NotWord))) {
if let Some(node_id) = self
.edges
.get(&(node_id, Transition::End(Boundary::NotWord)))
{
if !word_end(prev, c) {
matching_rules.extend(self.find_translations_from_node(
input,
Expand All @@ -319,7 +335,10 @@ impl Graph {
));
}
}
if let Some(node_id) = self.edges.get(&(node_id, Transition::End(Boundary::WordNumber))) {
if let Some(node_id) = self
.edges
.get(&(node_id, Transition::End(Boundary::WordNumber)))
{
if word_number(prev, c) {
matching_rules.extend(self.find_translations_from_node(
input,
Expand All @@ -330,7 +349,10 @@ impl Graph {
));
}
}
if let Some(node_id) = self.edges.get(&(node_id, Transition::Start(Boundary::NumberWord))) {
if let Some(node_id) = self
.edges
.get(&(node_id, Transition::Start(Boundary::NumberWord)))
{
if number_word(prev, c) {
matching_rules.extend(self.find_translations_from_node(
input,
Expand All @@ -341,7 +363,10 @@ impl Graph {
));
}
}
if let Some(node_id) = self.edges.get(&(node_id, Transition::End(Boundary::PrePattern))) {
if let Some(node_id) = self
.edges
.get(&(node_id, Transition::End(Boundary::PrePattern)))
{
matching_rules.extend(self.find_translations_from_node(
input,
prev,
Expand Down Expand Up @@ -493,8 +518,14 @@ mod tests {
Boundary::WordNumber,
);
assert_eq!(graph.find_translations("aaa", None), empty);
assert_eq!(graph.find_translations("aaa1", Some(' ')), vec![foo.clone()]);
assert_eq!(graph.find_translations("aaa1", Some('.')), vec![foo.clone()]);
assert_eq!(
graph.find_translations("aaa1", Some(' ')),
vec![foo.clone()]
);
assert_eq!(
graph.find_translations("aaa1", Some('.')),
vec![foo.clone()]
);
assert_eq!(graph.find_translations("aaa1", Some('c')), empty);
}

Expand Down Expand Up @@ -527,4 +558,3 @@ mod tests {
assert_eq!(graph.find_translations("foO", None), vec![foo.clone()]);
}
}

0 comments on commit 904ae5f

Please sign in to comment.