From 593a08e017fec07c0610f068115c89701cce5662 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 28 Nov 2015 16:40:31 -0500 Subject: [PATCH] Factor out Client site constructors with macro --- src/client.rs | 68 ++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/src/client.rs b/src/client.rs index 9131da7..94ee043 100644 --- a/src/client.rs +++ b/src/client.rs @@ -17,6 +17,32 @@ pub struct Client { redirect_uri: Option, } +macro_rules! site_constructors { + ( + $( + #[$attr:meta] + $ident:ident => ($auth_uri:expr, $token_uri:expr) + ),* + ) => { + $( + #[$attr] + pub fn $ident>( + client_id: S, + client_secret: S, + redirect_uri: Option + ) -> Self { + Client { + auth_uri: String::from($auth_uri), + token_uri: String::from($token_uri), + client_id: client_id.into(), + client_secret: client_secret.into(), + redirect_uri: redirect_uri.map(Into::into), + } + } + )* + } +} + impl Client { /// Creates an OAuth 2.0 client. pub fn new>( @@ -32,40 +58,22 @@ impl Client { client_id: client_id.into(), client_secret: client_secret.into(), - redirect_uri: redirect_uri.map(Into::::into), + redirect_uri: redirect_uri.map(Into::into), } } - /// Creates a Google OAuth 2.0 client. - pub fn google>( - client_id: S, - client_secret: S, - redirect_uri: Option - ) -> Self { - Client { - auth_uri: String::from("https://accounts.google.com/o/oauth2/auth"), - token_uri: String::from("https://accounts.google.com/o/oauth2/token"), + site_constructors!{ + #[doc = "Creates a Google OAuth 2.0 client."] + google => ( + "https://accounts.google.com/o/oauth2/auth", + "https://accounts.google.com/o/oauth2/token" + ), - client_id: client_id.into(), - client_secret: client_secret.into(), - redirect_uri: redirect_uri.map(Into::::into), - } - } - - /// Creates a GitHub OAuth 2.0 client. - pub fn github>( - client_id: S, - client_secret: S, - redirect_uri: Option - ) -> Self { - Client { - auth_uri: String::from("https://github.com/login/oauth/authorize"), - token_uri: String::from("https://github.com/login/oauth/access_token"), - - client_id: client_id.into(), - client_secret: client_secret.into(), - redirect_uri: redirect_uri.map(Into::::into), - } + #[doc = "Creates a GitHub OAuth 2.0 client."] + github => ( + "https://github.com/login/oauth/authorize", + "https://github.com/login/oauth/access_token" + ) } /// Constructs an authorization request URI.