From 339a792cefdd401ea7631cfee76abbb10b8be099 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Mon, 23 Oct 2023 06:49:05 +0200 Subject: [PATCH] DATABASE: Finish to implement the new search thingy (need to test it!) --- lektor_nkdb/src/search/mod.rs | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lektor_nkdb/src/search/mod.rs b/lektor_nkdb/src/search/mod.rs index 3aec9841..cce5b95d 100644 --- a/lektor_nkdb/src/search/mod.rs +++ b/lektor_nkdb/src/search/mod.rs @@ -56,6 +56,29 @@ impl SearchBy { _ => vec![], } } + + /// A match function. + pub(crate) fn matches(&self, kara: &Kara) -> bool { + match &self { + SearchBy::Query(regex) => regex.is_match(&kara.to_title_string()), + SearchBy::Id(id) => kara.id.local_id().eq(id), + SearchBy::SongType(ty) => kara.song_type.eq(ty), + SearchBy::SongOrigin(ori) => kara.song_origin.eq(ori), + SearchBy::Author(author) => kara.kara_makers.contains(author.as_str()), + SearchBy::Tag((key, None)) => kara.tags.contains_key(key.as_str()), + SearchBy::Tag((key, Some(value))) => kara + .tags + .get(key.as_str()) + .map(|v| v.iter().any(|v| v.as_ref().eq(value.as_str()))) + .unwrap_or_default(), + + // Recursive thing to apply multiple filters. + SearchBy::Multiple(filters) => filters.iter().all(|filter| filter.matches(kara)), + + // Handled after... + SearchBy::Playlist(_) => true, + } + } } impl Search { @@ -73,19 +96,6 @@ impl Search { /// A match function. pub(crate) fn matches(&self, kara: &Kara) -> bool { - match &self.0 { - SearchBy::Query(regex) => regex.is_match(&kara.to_title_string()), - SearchBy::Id(id) => kara.id.local_id().eq(id), - SearchBy::SongType(ty) => kara.song_type.eq(ty), - SearchBy::SongOrigin(ori) => kara.song_origin.eq(ori), - - SearchBy::Author(_) => todo!(), - SearchBy::Tag(_) => todo!(), - - SearchBy::Multiple(_) => todo!(), - - // Handled after... - SearchBy::Playlist(_) => true, - } + self.0.matches(kara) } } -- GitLab