From b4c42266776e7eed182928691e9e98fceec60b10 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Wed, 20 Jan 2021 00:52:10 +0100 Subject: [PATCH] Represent invalid tokens with an error instead of a boolean --- src/error.rs | 4 ++++ src/wrapper.rs | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 90325c8..2bec6c0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -27,6 +27,10 @@ pub enum Error { #[error("HTTP error")] Http(#[source] Box), + /// The token that was attempted to be used for authentication is invalid. + #[error("invalid authentication token")] + InvalidToken, + /// Tried to access a service that requires authentication. #[error("not authenticated")] NotAuthenticated, diff --git a/src/wrapper.rs b/src/wrapper.rs index 35dbb1a..414d86b 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -46,22 +46,23 @@ impl ListenBrainz { } /// Authenticate this client with the given token. - /// If the token is valid, authenticates the client and returns true, otherwise returns false. + /// If the token is valid, authenticates the client and returns [`Ok`]. /// /// # Errors /// + /// If the token was invalid, returns [`Error::InvalidToken`]. /// If there was an error while validating the token, that error is returned. /// See the Errors section of [`Client`] for more info on what errors might occur. - pub fn authenticate(&mut self, token: &str) -> Result { + pub fn authenticate(&mut self, token: &str) -> Result<(), Error> { let result = self.client.validate_token(token)?; if result.valid && result.user_name.is_some() { self.auth.replace(Auth { token: token.to_string(), user: result.user_name.unwrap(), }); - return Ok(true); + return Ok(()); } - Ok(false) + Err(Error::InvalidToken) } /// Helper method to submit a listen (either "single" or "playing now").