Browse Source

Use thiserror for UserNotFoundError.

main
Titouan Rigoudy 2 years ago
parent
commit
bceb8480b5
1 changed files with 6 additions and 22 deletions
  1. +6
    -22
      client/src/user/map.rs

+ 6
- 22
client/src/user/map.rs View File

@ -1,27 +1,13 @@
use std::collections; use std::collections;
use std::error;
use std::fmt;
use thiserror::Error;
use solstice_proto::{User, UserStatus}; use solstice_proto::{User, UserStatus};
/// The error returned when a user name was not found in the user map. /// The error returned when a user name was not found in the user map.
#[derive(Debug)]
pub struct UserNotFoundError {
/// The name of the user that wasn't found.
user_name: String,
}
impl fmt::Display for UserNotFoundError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "user \"{}\" not found", self.user_name)
}
}
impl error::Error for UserNotFoundError {
fn description(&self) -> &str {
"user not found"
}
}
#[derive(Debug, Error)]
#[error("user {0:?} not found")]
pub struct UserNotFoundError(String);
/// Contains the mapping from user names to user data and provides a clean /// Contains the mapping from user names to user data and provides a clean
/// interface to interact with it. /// interface to interact with it.
@ -54,9 +40,7 @@ impl UserMap {
) -> Result<&mut User, UserNotFoundError> { ) -> Result<&mut User, UserNotFoundError> {
match self.map.get_mut(user_name) { match self.map.get_mut(user_name) {
Some(user) => Ok(user), Some(user) => Ok(user),
None => Err(UserNotFoundError {
user_name: user_name.to_string(),
}),
None => Err(UserNotFoundError(user_name.to_string())),
} }
} }


Loading…
Cancel
Save