Skip to content

Commit

Permalink
Break out FMatchError construction into a function.
Browse files Browse the repository at this point in the history
This has become so voluminous that, in the main matching loop, it's
distracting.
  • Loading branch information
ltratt committed Jun 3, 2024
1 parent 98e3fce commit ab150bf
Showing 1 changed file with 45 additions and 66 deletions.
111 changes: 45 additions & 66 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,13 @@ impl<'a> FMatcher<'a> {
text_i += 1;
}
if !succ {
return Err(FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i,
text_line_off: text_lines_off + text_i_orig,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
});
return Err(self.fmatch_err(
ptn_i,
text,
text_lines_off,
text_i_orig,
names,
));
}
}
None => return Ok(()),
Expand All @@ -301,17 +297,13 @@ impl<'a> FMatcher<'a> {
match (self.ptn_lines.get(ptn_i), text_lines.get(text_i)) {
(None, None) => return Ok(()),
(None, Some(_)) => {
return Err(FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i_orig,
text_line_off: text_lines_off + text_i_orig,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
})
return Err(self.fmatch_err(
ptn_i_orig,
text,
text_lines_off,
text_i_orig,
names,
));
}
(Some(x), _)
if *x == GROUP_ANCHOR_WILDCARD
Expand All @@ -321,17 +313,13 @@ impl<'a> FMatcher<'a> {
break;
}
(Some(_), None) => {
return Err(FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i_orig,
text_line_off: text_lines_off + text_i_orig,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
});
return Err(self.fmatch_err(
ptn_i_orig,
text,
text_lines_off,
text_i_orig,
names,
));
}
(Some(x), Some(y)) => {
if self.match_line(&mut names, x, y) {
Expand All @@ -352,17 +340,7 @@ impl<'a> FMatcher<'a> {
ptn_i += 1;
text_i += 1;
} else {
return Err(FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i,
text_line_off: text_lines_off + text_i,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
});
return Err(self.fmatch_err(ptn_i, text, text_lines_off, text_i, names));
}
}
(None, None) => return Ok(()),
Expand All @@ -377,31 +355,11 @@ impl<'a> FMatcher<'a> {
return Ok(());
}
} else {
return Err(FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i,
text_line_off: text_lines_off + text_i,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
});
return Err(self.fmatch_err(ptn_i, text, text_lines_off, text_i, names));
}
}
(None, Some(_)) => {
return Err(FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i,
text_line_off: text_lines_off + text_i,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
});
return Err(self.fmatch_err(ptn_i, text, text_lines_off, text_i, names));
}
}
}
Expand Down Expand Up @@ -495,6 +453,27 @@ impl<'a> FMatcher<'a> {
}
}
}

fn fmatch_err(
&self,
ptn_i: usize,
text: &str,
text_lines_off: usize,
text_i: usize,
names: HashMap<&'a str, &'a str>,
) -> FMatchError {
FMatchError {
output_formatter: self.options.output_formatter,
ptn: self.orig_ptn.to_owned(),
text: text.to_owned(),
ptn_line_off: self.ptn_lines_off + ptn_i,
text_line_off: text_lines_off + text_i,
names: names
.iter()
.map(|(x, y)| (x.to_string(), y.to_string()))
.collect::<HashMap<_, _>>(),
}
}
}

/// An error indicating a failed match.
Expand Down

0 comments on commit ab150bf

Please sign in to comment.