From 0754f4070655e80c725380ccd4f15a4239398e80 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 24 Apr 2023 11:44:02 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20do=20partial=20matching=20for?= =?UTF-8?q?=20set=20filters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/filter.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/filter.rs b/src/filter.rs index 2ab7c7b..877dae6 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -62,9 +62,7 @@ fn get_field_value(card: &SearchCard, field: Field) -> Value { Field::Level => Value::Numerical(card.level.unwrap_or(0)), Field::LinkRating => Value::Numerical(card.link_rating.unwrap_or(0)), Field::Year => Value::Numerical(card.original_year.unwrap_or(0)), - // Search in any of the sets. This can lead to false positives if Konami ever decides to print LOB2 because `set:LOB` would also match that. - // On the bright side, this means `set:HA` matches all Hidden Arsenals (plus reprints in HAC), but also all other set codes that contain HA. - Field::Set => Value::String(card.sets.join(" ")), + Field::Set => Value::Multiple(card.sets.clone().into_iter().map(Value::String).collect()), Field::Type => Value::String(card.r#type.clone()), Field::Attribute => Value::String(card.attribute.clone().unwrap_or_default()), Field::Class => Value::String(card.card_type.clone()), @@ -81,6 +79,12 @@ fn filter_value(op: &Operator, field_value: &Value, query_value: &Value) -> bool Operator::NotEqual => !field.contains(query), _ => false, }, + // Currently only for sets the card was released in. + (Value::Multiple(field), query @ Value::String(_)) => match op { + Operator::Equal => field.iter().any(|f| f == query), + Operator::NotEqual => !field.iter().any(|f| f == query), + _ => false, + }, _ => false, } } @@ -108,14 +112,31 @@ mod tests { let lacooda = SearchCard::from(&serde_json::from_str::(RAW_MONSTER).unwrap()); let lacooda_but_level_4 = SearchCard { level: Some(4), ..lacooda.clone() }; - let filter_level_3 = parse_filters("l=3").unwrap(); - assert!(filter_level_3.1[0](&lacooda)); + let filter_level_3 = parse_filters("l=3").unwrap().1; + assert!(filter_level_3[0](&lacooda)); - let filter_level_3_4 = parse_filters("l=3|4").unwrap(); - assert!(filter_level_3_4.1[0](&lacooda)); - assert!(filter_level_3_4.1[0](&lacooda_but_level_4)); + let filter_level_3_4 = parse_filters("l=3|4").unwrap().1; + assert!(filter_level_3_4[0](&lacooda)); + assert!(filter_level_3_4[0](&lacooda_but_level_4)); - let filter_level_5 = parse_filters("l=5").unwrap(); - assert!(!filter_level_5.1[0](&lacooda)); + let filter_level_5 = parse_filters("l=5").unwrap().1; + assert!(!filter_level_5[0](&lacooda)); + } + + #[test] + fn set_filter_test() { + let lacooda = SearchCard::from(&serde_json::from_str::(RAW_MONSTER).unwrap()); + + let astral_pack_filter = parse_filters("set:ap03").unwrap().1; + assert!(astral_pack_filter[0](&lacooda)); + + let partial_filter = parse_filters("set:ap0").unwrap().1; + assert!(!partial_filter[0](&lacooda)); + + let not_astral_pack_filter = parse_filters("set!=ap03").unwrap().1; + assert!(!not_astral_pack_filter[0](&lacooda)); + + let astral_pack_4_filter = parse_filters("set:ap04").unwrap().1; + assert!(!astral_pack_4_filter[0](&lacooda)); } }