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 26 additions and 1 deletions
Showing only changes of commit 7f0bdc1e82 - Show all commits

View File

@ -10,7 +10,7 @@ use std::convert::TryFrom;
/// Holds matrix room ID and display name as strings, for use with
/// searching. See search_for_room.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct RoomNameAndId {
id: String,
name: String,
@ -42,6 +42,7 @@ fn search_for_room<'a>(
rooms_for_user: &'a [RoomNameAndId],
query: &str,
) -> Option<&'a RoomNameAndId> {
//Lowest score is the best match.
rooms_for_user
.iter()
.find(|room| room.id == query)
@ -151,3 +152,27 @@ impl Command for SetRoomCommand {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn set_room_prefers_room_id_over_name() {
let rooms = vec![
RoomNameAndId {
id: "roomid".to_string(),
name: "room_name".to_string(),
},
RoomNameAndId {
id: "anotherone".to_string(),
name: "roomid".to_string(),
},
];
let found_room = search_for_room(&rooms, "roomid");
assert!(found_room.is_some());
assert_eq!(found_room.unwrap(), &rooms[0]);
}
}