From e94930d76754906ad3305c4fc656c45dc0f5d043 Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 27 Jan 2023 14:45:17 +0100 Subject: [PATCH] add limit to returned elements --- src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 17dd5cd..c5b129b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ mod data; mod filter; mod parser; +const RESULT_LIMIT: usize = 100; + static CARDS: LazyLock> = LazyLock::new(|| { serde_json::from_reader::<_, CardInfo>(BufReader::new(File::open("cards.json").expect("cards.json not found"))) .expect("Could not deserialize cards") @@ -58,8 +60,12 @@ async fn search(q: Option, web::Form>>) -> Resul if let Some(q) = q { let query = parser::parse_filters(&q)?; let now = Instant::now(); - let matches: Vec<&Card> = - SEARCH_CARDS.iter().filter(|card| query.iter().all(|q| q(card))).map(|c| CARDS_BY_ID.get(&c.id).unwrap()).collect(); + let matches: Vec<&Card> = SEARCH_CARDS + .iter() + .filter(|card| query.iter().all(|q| q(card))) + .map(|c| CARDS_BY_ID.get(&c.id).unwrap()) + .take(RESULT_LIMIT) + .collect(); write!(res, "Showing {} results (took {:?})

", matches.len(), now.elapsed())?; for card in matches { res.push_str(&card.to_string());