diff options
author | Kornel <kornel@geekhood.net> | 2020-03-03 00:22:15 +0000 |
---|---|---|
committer | Kornel <kornel@geekhood.net> | 2020-03-03 00:42:10 +0000 |
commit | c1f1ab4b809f6a802a06fb9b828602e91c5961d2 (patch) | |
tree | 76bfcd9f0c045ef8289c3e27a349ca87416935fe /categories | |
parent | 340c0baec9416254e6a728f503b69f1327c4c24a (diff) |
Deep slug check
Diffstat (limited to 'categories')
-rw-r--r-- | categories/src/categories.rs | 9 | ||||
-rw-r--r-- | categories/src/tuning.rs | 4 |
2 files changed, 7 insertions, 6 deletions
diff --git a/categories/src/categories.rs b/categories/src/categories.rs index e03353d..3baba6c 100644 --- a/categories/src/categories.rs +++ b/categories/src/categories.rs @@ -60,7 +60,8 @@ impl Categories { }) } - pub fn from_slug<S: AsRef<str>>(&self, slug: S) -> impl Iterator<Item = &Category> { + /// true if full match + pub fn from_slug<S: AsRef<str>>(&self, slug: S) -> (Vec<&Category>, bool) { let mut out = Vec::new(); let mut cats = &self.root; for name in slug.as_ref().split("::") { @@ -69,10 +70,10 @@ impl Categories { cats = &cat.sub; out.push(cat); }, - None => break, + None => return (out, false), } } - out.into_iter() + (out, true) } fn categories_from_table(full_slug_start: &str, toml: Table) -> CResult<CategoryMap> { @@ -156,7 +157,7 @@ impl Categories { Some((depth, idx, Cow::Borrowed(s))) }) .filter(|(_, _, s)| { - if CATEGORIES.from_slug(s).next().is_none() { + if !CATEGORIES.from_slug(s).1 { println!("invalid cat name {}", s); return false; } diff --git a/categories/src/tuning.rs b/categories/src/tuning.rs index 025f192..a929066 100644 --- a/categories/src/tuning.rs +++ b/categories/src/tuning.rs @@ -1114,7 +1114,7 @@ pub fn adjusted_relevance(mut candidates: HashMap<String, f64>, keywords: &HashS Cond::Any(reqs) => reqs.iter().any(|&k| keywords.contains(k)), } { for &(slug, mul, add) in actions.iter() { - assert!(CATEGORIES.from_slug(slug).next().is_some(), slug); + assert!(CATEGORIES.from_slug(slug).1, slug); assert!(mul >= 1.0 || add < 0.0000001, slug); let score = candidates.entry(slug.to_string()).or_insert(0.); *score *= mul; @@ -1132,7 +1132,7 @@ pub fn adjusted_relevance(mut candidates: HashMap<String, f64>, keywords: &HashS let mut res: Vec<_> = candidates.into_iter() .filter(|&(_, v)| v >= min_category_match_threshold) - .filter(|&(ref k, _)| CATEGORIES.from_slug(k).next().is_some() /* FIXME: that checks top level only */) + .filter(|&(ref k, _)| CATEGORIES.from_slug(k).1) .take(max_num_categories) .map(|(k, v)| (v, k)) .collect(); |