Skip to content

Commit

Permalink
refactor: simplify pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jun 16, 2024
1 parent 987cd58 commit 9c1065b
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions malva/src/line_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,22 @@ impl LineBounds {
"end {end} must be greater than or equal start {start}"
);

let start = self
.0
.iter()
.try_fold(0, |i, offset| match start.cmp(offset) {
Ordering::Less => ControlFlow::Break(i),
Ordering::Equal => ControlFlow::Continue(i),
Ordering::Greater => ControlFlow::Continue(i + 1),
});
let end = self
.0
.iter()
.try_fold(0, |i, offset| match end.cmp(offset) {
Ordering::Less => ControlFlow::Break(i),
Ordering::Equal => ControlFlow::Continue(i),
Ordering::Greater => ControlFlow::Continue(i + 1),
});

match (start, end) {
(ControlFlow::Break(start), ControlFlow::Break(end)) => end - start,
(ControlFlow::Break(start), ControlFlow::Continue(end)) => end - start,
(ControlFlow::Continue(start), ControlFlow::Break(end)) => end - start,
(ControlFlow::Continue(start), ControlFlow::Continue(end)) => end - start,
}
let (ControlFlow::Break(start) | ControlFlow::Continue(start)) =
self.0
.iter()
.try_fold(0, |i, offset| match start.cmp(offset) {
Ordering::Less => ControlFlow::Break(i),
Ordering::Equal => ControlFlow::Continue(i),
Ordering::Greater => ControlFlow::Continue(i + 1),
});
let (ControlFlow::Break(end) | ControlFlow::Continue(end)) =
self.0
.iter()
.try_fold(0, |i, offset| match end.cmp(offset) {
Ordering::Less => ControlFlow::Break(i),
Ordering::Equal => ControlFlow::Continue(i),
Ordering::Greater => ControlFlow::Continue(i + 1),
});
end - start
}
}

0 comments on commit 9c1065b

Please sign in to comment.