add limit to returned elements

This commit is contained in:
kageru 2023-01-27 14:45:17 +01:00
parent 557d7c680e
commit e94930d767

@ -9,6 +9,8 @@ mod data;
mod filter; mod filter;
mod parser; mod parser;
const RESULT_LIMIT: usize = 100;
static CARDS: LazyLock<Vec<Card>> = LazyLock::new(|| { static CARDS: LazyLock<Vec<Card>> = LazyLock::new(|| {
serde_json::from_reader::<_, CardInfo>(BufReader::new(File::open("cards.json").expect("cards.json not found"))) serde_json::from_reader::<_, CardInfo>(BufReader::new(File::open("cards.json").expect("cards.json not found")))
.expect("Could not deserialize cards") .expect("Could not deserialize cards")
@ -58,8 +60,12 @@ async fn search(q: Option<Either<web::Query<Query>, web::Form<Query>>>) -> Resul
if let Some(q) = q { if let Some(q) = q {
let query = parser::parse_filters(&q)?; let query = parser::parse_filters(&q)?;
let now = Instant::now(); let now = Instant::now();
let matches: Vec<&Card> = let matches: Vec<&Card> = SEARCH_CARDS
SEARCH_CARDS.iter().filter(|card| query.iter().all(|q| q(card))).map(|c| CARDS_BY_ID.get(&c.id).unwrap()).collect(); .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 {:?})<br/><br/>", matches.len(), now.elapsed())?; write!(res, "Showing {} results (took {:?})<br/><br/>", matches.len(), now.elapsed())?;
for card in matches { for card in matches {
res.push_str(&card.to_string()); res.push_str(&card.to_string());