diff options
author | Canop <cano.petrole@gmail.com> | 2021-02-03 07:54:27 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-02-03 07:54:27 +0100 |
commit | cc3b2c04521c6e5886f0bb2f56f1ca80a2b8e05a (patch) | |
tree | a648a50b9a4dbaaec9233eaade0ade99376c28b5 /src/pattern | |
parent | 5927ec4b7f4e6ce24714a607b4de7eddccd19b0f (diff) |
update bet to 0.4
Diffstat (limited to 'src/pattern')
-rw-r--r-- | src/pattern/composite_pattern.rs | 81 |
1 files changed, 28 insertions, 53 deletions
diff --git a/src/pattern/composite_pattern.rs b/src/pattern/composite_pattern.rs index 96a9081..7eec3ea 100644 --- a/src/pattern/composite_pattern.rs +++ b/src/pattern/composite_pattern.rs @@ -2,7 +2,6 @@ use { super::*, crate::{ content_search::ContentMatch, - errors::PatternError, }, bet::*, std::path::Path, @@ -21,12 +20,12 @@ impl CompositePattern { pub fn score_of_string(&self, candidate: &str) -> Option<i32> { use PatternOperator::*; - let composite_result: Result<Option<Option<i32>>, PatternError> = self.expr.eval( + let composite_result: Option<Option<i32>> = self.expr.eval( // score evaluation - |pat| Ok(pat.score_of_string(candidate)), + |pat| pat.score_of_string(candidate), // operator |op, a, b| { - Ok(match (op, a, b) { + match (op, a, b) { (And, None, _) => None, // normally not called due to short-circuit (And, Some(sa), Some(Some(sb))) => Some(sa + sb), (Or, None, Some(Some(sb))) => Some(sb), @@ -35,7 +34,7 @@ impl CompositePattern { (Not, Some(_), _) => None, (Not, None, _) => Some(1), _ => None, - }) + } }, // short-circuit. We don't short circuit on 'or' because // we want to use both scores @@ -44,27 +43,21 @@ impl CompositePattern { _ => false, }, ); - match composite_result { - Err(e) => { - warn!("unexpected error while evaluating composite: {:?}", e); - None - } - Ok(Some(r)) => r, - Ok(None) => { + composite_result + .unwrap_or_else(||{ warn!("unexpectedly missing result "); None - } - } + }) } pub fn score_of(&self, candidate: Candidate) -> Option<i32> { use PatternOperator::*; - let composite_result: Result<Option<Option<i32>>, PatternError> = self.expr.eval( + let composite_result: Option<Option<i32>> = self.expr.eval( // score evaluation - |pat| Ok(pat.score_of(candidate)), + |pat| pat.score_of(candidate), // operator |op, a, b| { - Ok(match (op, a, b) { + match (op, a, b) { (And, None, _) => None, // normally not called due to short-circuit (And, Some(sa), Some(Some(sb))) => Some(sa + sb), (Or, None, Some(Some(sb))) => Some(sb), @@ -73,7 +66,7 @@ impl CompositePattern { (Not, Some(_), _) => None, (Not, None, _) => Some(1), _ => None, - }) + } }, // short-circuit. We don't short circuit on 'or' because // we want to use both scores @@ -82,50 +75,38 @@ impl CompositePattern { _ => false, }, ); - match composite_result { - Err(e) => { - warn!("unexpected error while evaluating composite: {:?}", e); - None - } - Ok(Some(r)) => r, - Ok(None) => { + composite_result + .unwrap_or_else(||{ warn!("unexpectedly missing result "); None - } - } + }) } pub fn search_string(&self, candidate: &str) -> Option<NameMatch> { // an ideal algorithm would call score_of on patterns when the object is different // to deal with exclusions but I'll start today with something simpler use PatternOperator::*; - let composite_result: Result<Option<Option<NameMatch>>, PatternError> = self.expr.eval( + let composite_result: Option<Option<NameMatch>> = self.expr.eval( // score evaluation - |pat| Ok(pat.search_string(candidate)), + |pat| pat.search_string(candidate), // operator - |op, a, b| Ok(match (op, a, b) { + |op, a, b| match (op, a, b) { (Not, Some(_), _) => None, (_, Some(ma), _) => Some(ma), (_, None, Some(omb)) => omb, _ => None, - }), + }, |op, a| match (op, a) { (Or, Some(_)) => true, _ => false, }, ); // it's possible we didn't find a result because the composition - match composite_result { - Err(e) => { - warn!("unexpected error while evaluating composite: {:?}", e); - None - } - Ok(Some(r)) => r, - Ok(None) => { + composite_result + .unwrap_or_else(||{ warn!("unexpectedly missing result "); None - } - } + }) } pub fn search_content( @@ -134,32 +115,26 @@ impl CompositePattern { desired_len: usize, // available space for content match display ) -> Option<ContentMatch> { use PatternOperator::*; - let composite_result: Result<Option<Option<ContentMatch>>, PatternError> = self.expr.eval( + let composite_result: Option<Option<ContentMatch>> = self.expr.eval( // score evaluation - |pat| Ok(pat.search_content(candidate, desired_len)), + |pat| pat.search_content(candidate, desired_len), // operator - |op, a, b| Ok(match (op, a, b) { + |op, a, b| match (op, a, b) { (Not, Some(_), _) => None, (_, Some(ma), _) => Some(ma), (_, None, Some(omb)) => omb, _ => None, - }), + }, |op, a| match (op, a) { (Or, Some(_)) => true, _ => false, }, ); - match composite_result { - Err(e) => { - warn!("unexpected error while evaluating composite: {:?}", e); - None - } - Ok(Some(r)) => r, - Ok(None) => { + composite_result + .unwrap_or_else(||{ warn!("unexpectedly missing result "); None - } - } + }) } pub fn has_real_scores(&self) -> bool { |