From bceb8480b5be3996f112f42f1d8b982ea226c7c8 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sun, 11 Dec 2022 21:55:39 +0000 Subject: [PATCH] Use thiserror for UserNotFoundError. --- client/src/user/map.rs | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/client/src/user/map.rs b/client/src/user/map.rs index cd999f1..7d5a966 100644 --- a/client/src/user/map.rs +++ b/client/src/user/map.rs @@ -1,27 +1,13 @@ use std::collections; -use std::error; -use std::fmt; + +use thiserror::Error; use solstice_proto::{User, UserStatus}; /// 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 /// interface to interact with it. @@ -54,9 +40,7 @@ impl UserMap { ) -> Result<&mut User, UserNotFoundError> { match self.map.get_mut(user_name) { Some(user) => Ok(user), - None => Err(UserNotFoundError { - user_name: user_name.to_string(), - }), + None => Err(UserNotFoundError(user_name.to_string())), } }