From f95bb816f7346ae5d1b04e946f09da372e9c0a37 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 30 Apr 2015 17:08:31 +0200 Subject: [PATCH] fix(TokenStorage): `set()` returns `Result<(), _>` That we, we are conforming to the style postulated by the standard library, which, if in doubt, should always be preferred. Fixes #5 --- Cargo.toml | 2 +- src/helper.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ccb6e01b..4d3a22ca0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "yup-oauth2" -version = "0.4.0" +version = "0.4.1" authors = ["Sebastian Thiel "] repository = "https://github.com/Byron/yup-oauth2" description = "A partial oauth2 implementation, providing the 'device' authorization flow" diff --git a/src/helper.rs b/src/helper.rs index 154bf5152..999e22d8b 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -25,7 +25,7 @@ pub trait TokenStorage { /// If `token` is None, it is invalid or revoked and should be removed from storage. /// Otherwise, it should be saved. - fn set(&mut self, scope_hash: u64, scopes: &Vec<&str>, token: Option) -> Option; + fn set(&mut self, scope_hash: u64, scopes: &Vec<&str>, token: Option) -> Result<(), Self::Error>; /// A `None` result indicates that there is no token for the given scope_hash. fn get(&self, scope_hash: u64, scopes: &Vec<&str>) -> Result, Self::Error>; } @@ -50,7 +50,7 @@ impl fmt::Display for NullError { impl TokenStorage for NullStorage { type Error = NullError; - fn set(&mut self, _: u64, _: &Vec<&str>, _: Option) -> Option { None } + fn set(&mut self, _: u64, _: &Vec<&str>, _: Option) -> Result<(), NullError> { Ok(()) } fn get(&self, _: u64, _: &Vec<&str>) -> Result, NullError> { Ok(None) } } @@ -63,12 +63,12 @@ pub struct MemoryStorage { impl TokenStorage for MemoryStorage { type Error = NullError; - fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option) -> Option { + fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option) -> Result<(), NullError> { match token { Some(t) => self.tokens.insert(scope_hash, t), None => self.tokens.remove(&scope_hash), }; - None + Ok(()) } fn get(&self, scope_hash: u64, _: &Vec<&str>) -> Result, NullError> { @@ -308,8 +308,8 @@ impl GetToken for Authenticator self.delegate.token_refresh_failed(&err_str, &err_description); let storage_err = match self.storage.set(scope_key, &scopes, None) { - None => String::new(), - Some(err) => err.to_string(), + Ok(_) => String::new(), + Err(err) => err.to_string(), }; return Err(Box::new( StringError::new(storage_err + err_str, err_description.as_ref()))) @@ -317,7 +317,7 @@ impl GetToken for Authenticator RefreshResult::Success(ref new_t) => { t = new_t.clone(); loop { - if let Some(err) = self.storage.set(scope_key, &scopes, Some(t.clone())) { + if let Err(err) = self.storage.set(scope_key, &scopes, Some(t.clone())) { match self.delegate.token_storage_failure(true, &err) { Retry::Skip => break, Retry::Abort => return Err(Box::new(err)), @@ -346,7 +346,7 @@ impl GetToken for Authenticator { Ok(token) => { loop { - if let Some(err) = self.storage.set(scope_key, &scopes, Some(token.clone())) { + if let Err(err) = self.storage.set(scope_key, &scopes, Some(token.clone())) { match self.delegate.token_storage_failure(true, &err) { Retry::Skip => break, Retry::Abort => return Err(Box::new(err)),