Set Room Command #78

Manually merged
projectmoon merged 4 commits from set-room-command into master 2021-05-28 22:05:12 +00:00
1 changed files with 11 additions and 9 deletions
Showing only changes of commit 0c0ddafd03 - Show all commits

View File

@ -40,19 +40,21 @@ impl Fuseable for RoomNameAndId {
/// returned, or None if no matches were found. /// returned, or None if no matches were found.
fn search_for_room<'a>( fn search_for_room<'a>(
rooms_for_user: &'a [RoomNameAndId], rooms_for_user: &'a [RoomNameAndId],
query: &str, search_for: &str,
) -> Option<&'a RoomNameAndId> { ) -> Option<&'a RoomNameAndId> {
//Lowest score is the best match. //Lowest score is the best match.
rooms_for_user let best_fuzzy_match = || -> Option<&RoomNameAndId> {
.iter()
.find(|room| room.id == query)
.or_else(|| {
Fuse::default() Fuse::default()
.search_text_in_fuse_list(query, &rooms_for_user) .search_text_in_fuse_list(search_for, &rooms_for_user)
.into_iter() .into_iter()
.min_by(|r1, r2| r1.score.partial_cmp(&r2.score).unwrap()) .min_by(|r1, r2| r1.score.partial_cmp(&r2.score).unwrap())
.and_then(|result| rooms_for_user.get(result.index)) .and_then(|result| rooms_for_user.get(result.index))
}) };
rooms_for_user
.iter()
.find(|room| room.id == search_for)
.or_else(best_fuzzy_match)
} }
async fn get_rooms_for_user( async fn get_rooms_for_user(