From ef4f1ef02f369d32aed8ba6e7002f5e67db65455 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Mon, 8 Feb 2021 20:14:11 +0000 Subject: [PATCH] Small code cleanup in matrix.rs --- src/matrix.rs | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 968f684..525466e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,40 +1,38 @@ use log::error; -use matrix_sdk::events::{ - room::message::{MessageEventContent::Notice, NoticeMessageEventContent}, - AnyMessageEventContent::RoomMessage, -}; use matrix_sdk::Error as MatrixError; +use matrix_sdk::{ + events::{ + room::message::{MessageEventContent::Notice, NoticeMessageEventContent}, + AnyMessageEventContent::RoomMessage, + }, + RoomMember, +}; use matrix_sdk::{identifiers::RoomId, Client}; /// Extracts more detailed error messages out of a matrix SDK error. fn extract_error_message(error: MatrixError) -> String { use matrix_sdk::{Error::Http, HttpError}; - match error { - Http(http_err) => match http_err { - HttpError::FromHttpResponse(ruma_err) => ruma_err.to_string(), - _ => http_err.to_string(), - }, - _ => error.to_string(), + if let Http(HttpError::FromHttpResponse(ruma_err)) = error { + ruma_err.to_string() + } else { + error.to_string() } } /// Retrieve a list of users in a given room. pub async fn get_users_in_room(client: &Client, room_id: &RoomId) -> Vec { if let Some(joined_room) = client.get_joined_room(room_id) { - joined_room - .joined_members() - .await - .ok() - .unwrap_or_default() - .into_iter() - .map(|member| { - format!( - "@{}:{}", - member.user_id().localpart(), - member.user_id().server_name() - ) - }) - .collect() + let members: Vec = joined_room.joined_members().await.ok().unwrap_or_default(); + + let to_username = |member: RoomMember| { + format!( + "@{}:{}", + member.user_id().localpart(), + member.user_id().server_name() + ) + }; + + members.into_iter().map(to_username).collect() } else { vec![] }