From 9d54c41a2b47c171b0bd6eed6a596028b8f495af Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Tue, 9 Jan 2024 22:16:36 +0100 Subject: [PATCH] Fix melisearch name replacements with operators other than : (#845) * Fix melisearch name replacements with operators other than : * Pass facet by refrence --------- Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com> --- src/routes/v2/projects.rs | 53 ++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/routes/v2/projects.rs b/src/routes/v2/projects.rs index f22102ce..f904ce01 100644 --- a/src/routes/v2/projects.rs +++ b/src/routes/v2/projects.rs @@ -78,7 +78,7 @@ pub async fn project_search( }) .collect_vec(); - // These loaders speciically used to be combined with 'mod' to be a plugin, but now + // These loaders specifically used to be combined with 'mod' to be a plugin, but now // they are their own loader type. We will convert 'mod' to 'mod' OR 'plugin' // as it essentially was before. let facets = v2_reroute::convert_plugin_loaders_v3(facets); @@ -93,17 +93,13 @@ pub async fn project_search( facets .into_iter() .map(|facet| { - let val = match facet.split(':').nth(1) { - Some(val) => val, - None => return facet.to_string(), - }; - - if facet.starts_with("versions:") { - format!("game_versions:{}", val) - } else if facet.starts_with("project_type:") { - format!("project_types:{}", val) - } else if facet.starts_with("title:") { - format!("name:{}", val) + if let Some((key, operator, val)) = parse_facet(&facet) { + format!("{}{}{}", match key.as_str() { + "versions" => "game_versions", + "project_type" => "project_types", + "title" => "name", + x => x, + }, operator, val) } else { facet.to_string() } @@ -130,6 +126,39 @@ pub async fn project_search( Ok(HttpResponse::Ok().json(results)) } +/// Parses a facet into a key, operator, and value +fn parse_facet(facet: &String) -> Option<(String, String, String)> { + let mut key = String::new(); + let mut operator = String::new(); + let mut val = String::new(); + + let mut iterator = facet.chars(); + while let Some(char) = iterator.next() { + match char { + ':' | '=' => { + operator.push(char); + val = iterator.collect::(); + return Some((key, operator, val)); + } + '<' | '>' => { + operator.push(char); + if let Some(next_char) = iterator.next() { + if next_char == '=' { + operator.push(next_char); + } else { + val.push(next_char); + } + } + val.push_str(&iterator.collect::()); + return Some((key, operator, val)); + } + _ => key.push(char), + } + } + + None +} + #[derive(Deserialize, Validate)] pub struct RandomProjects { #[validate(range(min = 1, max = 100))]