From 2bdb47375a8e9effed6ba8f0a3c952eddbab9e6b Mon Sep 17 00:00:00 2001 From: Lewin Bormann Date: Tue, 20 Sep 2016 13:17:26 +0200 Subject: [PATCH] feat(refactor): Move StringError from authenticator to types module --- src/authenticator.rs | 44 +------------------------------------------- src/types.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/authenticator.rs b/src/authenticator.rs index 0df1fc4..66be97b 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -4,11 +4,10 @@ use std::hash::{SipHasher, Hash, Hasher}; use std::thread::sleep; use std::cmp::min; use std::error::Error; -use std::fmt; use std::convert::From; use authenticator_delegate::{AuthenticatorDelegate, PollError, PollInformation}; -use types::{RequestError, Token, FlowType, ApplicationSecret}; +use types::{RequestError, StringError, Token, FlowType, ApplicationSecret}; use device::DeviceFlow; use installed::{InstalledFlow, InstalledFlowReturnMethod}; use refresh::{RefreshResult, RefreshFlow}; @@ -41,47 +40,6 @@ pub struct Authenticator { secret: ApplicationSecret, } -#[derive(Debug)] -struct StringError { - error: String, -} - -impl fmt::Display for StringError { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - self.description().fmt(f) - } -} - -impl StringError { - fn new(error: String, desc: Option<&String>) -> StringError { - let mut error = error; - if let Some(d) = desc { - error.push_str(": "); - error.push_str(&*d); - } - - StringError { error: error } - } -} - -impl<'a> From<&'a Error> for StringError { - fn from(err: &'a Error) -> StringError { - StringError::new(err.description().to_string(), None) - } -} - -impl From for StringError { - fn from(value: String) -> StringError { - StringError::new(value, None) - } -} - -impl Error for StringError { - fn description(&self) -> &str { - &self.error - } -} - /// A provider for authorization tokens, yielding tokens valid for a given scope. /// The `api_key()` method is an alternative in case there are no scopes or /// if no user is involved. diff --git a/src/types.rs b/src/types.rs index 02d59cd..5af0f94 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,5 @@ use chrono::{DateTime, UTC, TimeZone}; +use std::error::Error; use std::fmt; use std::str::FromStr; use hyper; @@ -59,6 +60,47 @@ impl fmt::Display for RequestError { } } +#[derive(Debug)] +pub struct StringError { + error: String, +} + +impl fmt::Display for StringError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + self.description().fmt(f) + } +} + +impl StringError { + pub fn new(error: String, desc: Option<&String>) -> StringError { + let mut error = error; + if let Some(d) = desc { + error.push_str(": "); + error.push_str(&*d); + } + + StringError { error: error } + } +} + +impl<'a> From<&'a Error> for StringError { + fn from(err: &'a Error) -> StringError { + StringError::new(err.description().to_string(), None) + } +} + +impl From for StringError { + fn from(value: String) -> StringError { + StringError::new(value, None) + } +} + +impl Error for StringError { + fn description(&self) -> &str { + &self.error + } +} + /// Represents all implemented token types #[derive(Clone, PartialEq, Debug)]