Return a Result instead of a vector

merge-requests/254/head
Torsten Flammiger 2 years ago
parent fb19114bd9
commit 91eb6c4d08

@ -95,12 +95,16 @@ impl Admin {
match event {
AdminCommand::ListLocalUsers => {
let users = guard.users.get_local_users();
let mut msg: String = format!("Found {} local user account(s):\n", users.len());
msg += &users.join("\n");
send_message(RoomMessageEventContent::text_plain(&msg), guard, &state_lock);
match guard.users.get_local_users() {
Ok(users) => {
let mut msg: String = format!("Found {} local user account(s):\n", users.len());
msg += &users.join("\n");
send_message(RoomMessageEventContent::text_plain(&msg), guard, &state_lock);
}
Err(e) => {
send_message(RoomMessageEventContent::text_plain(e.to_string()), guard, &state_lock);
}
}
}
AdminCommand::RegisterAppservice(yaml) => {
guard.appservice.register_appservice(yaml).unwrap(); // TODO handle error

@ -84,7 +84,6 @@ impl Users {
Ok(self.userid_password.iter().count())
}
/// Find out which user an access token belongs to.
#[tracing::instrument(skip(self, token))]
pub fn find_from_token(&self, token: &str) -> Result<Option<(Box<UserId>, String)>> {
@ -129,16 +128,21 @@ impl Users {
/// If utils::string_from_bytes returns an error that username will be skipped
/// and the function will log the error
#[tracing::instrument(skip(self))]
pub fn get_local_users(&self) -> Vec<String> {
self.userid_password.iter().filter(|(_, pw)| pw.len() > 0).map(|(username, _)| {
match utils::string_from_bytes(&username) {
pub fn get_local_users(&self) -> Result<Vec<String>> {
self.userid_password
.iter()
.filter(|(_, pw)| pw.len() > 0)
.map(|(username, _)| match utils::string_from_bytes(&username) {
Ok(s) => s,
Err(e) => {
Error::bad_database(format!("Failed to parse username: {}", e.to_string()));
Error::bad_database(format!(
"Failed to parse username while calling get_local_users(): {}",
e.to_string()
));
None
}
}
}).collect::<Vec<String>>()
})
.collect::<Result<Vec<String>>>()
}
/// Returns the password hash for the given user.

Loading…
Cancel
Save