From 0c0ddafd031a0038824dd5431a5a3a80e5a0ee70 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 28 May 2021 21:19:26 +0000 Subject: [PATCH] Search for rooms closure as a separate variable. --- src/commands/rooms.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/commands/rooms.rs b/src/commands/rooms.rs index 7f368c0..eaa147b 100644 --- a/src/commands/rooms.rs +++ b/src/commands/rooms.rs @@ -40,19 +40,21 @@ impl Fuseable for RoomNameAndId { /// returned, or None if no matches were found. fn search_for_room<'a>( rooms_for_user: &'a [RoomNameAndId], - query: &str, + search_for: &str, ) -> Option<&'a RoomNameAndId> { //Lowest score is the best match. + let best_fuzzy_match = || -> Option<&RoomNameAndId> { + Fuse::default() + .search_text_in_fuse_list(search_for, &rooms_for_user) + .into_iter() + .min_by(|r1, r2| r1.score.partial_cmp(&r2.score).unwrap()) + .and_then(|result| rooms_for_user.get(result.index)) + }; + rooms_for_user .iter() - .find(|room| room.id == query) - .or_else(|| { - Fuse::default() - .search_text_in_fuse_list(query, &rooms_for_user) - .into_iter() - .min_by(|r1, r2| r1.score.partial_cmp(&r2.score).unwrap()) - .and_then(|result| rooms_for_user.get(result.index)) - }) + .find(|room| room.id == search_for) + .or_else(best_fuzzy_match) } async fn get_rooms_for_user(