#[derive(Debug, Clone)]
pub struct Config {
pub(crate) auth_endpoint: AuthEndPoint,
pub(crate) client_id: ClientID,
pub(crate) client_secret: ClientSecret,
pub(crate) token_endpoint: TokenEndPoint,
pub(crate) redirect_uri: RedirectURI,
}
impl Config {
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}
pub fn auth_endpoint(&self) -> &AuthEndPoint {
&self.auth_endpoint
}
pub fn client_id(&self) -> &ClientID {
&self.client_id
}
pub fn client_secret(&self) -> &ClientSecret {
&self.client_secret
}
pub fn token_endpoint(&self) -> &TokenEndPoint {
&self.token_endpoint
}
pub fn redirect_uri(&self) -> &RedirectURI {
&self.redirect_uri
}
}
#[derive(Debug, Clone, Default)]
pub struct ConfigBuilder {
auth_endpoint: AuthEndPoint,
client_id: ClientID,
client_secret: ClientSecret,
token_endpoint: TokenEndPoint,
redirect_uri: RedirectURI,
}
impl ConfigBuilder {
pub fn new() -> Self {
ConfigBuilder::default()
}
pub fn auth_endpoint<T: Into<AuthEndPoint>>(mut self, auth_endpoint: T) -> ConfigBuilder {
self.auth_endpoint = auth_endpoint.into();
self
}
pub fn client_id<T: Into<ClientID>>(mut self, client_id: T) -> Self {
self.client_id = client_id.into();
self
}
pub fn client_secret<T: Into<ClientSecret>>(mut self, client_secret: T) -> Self {
self.client_secret = client_secret.into();
self
}
pub fn token_endpoint<T: Into<TokenEndPoint>>(mut self, token_endpoint: T) -> Self {
self.token_endpoint = token_endpoint.into();
self
}
pub fn redirect_uri<T: Into<RedirectURI>>(mut self, redirect_uri: T) -> Self {
self.redirect_uri = redirect_uri.into();
self
}
pub fn build(self) -> Config {
Config {
auth_endpoint: self.auth_endpoint,
client_id: self.client_id,
client_secret: self.client_secret,
token_endpoint: self.token_endpoint,
redirect_uri: self.redirect_uri,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct AuthEndPoint(pub(crate) String);
impl AuthEndPoint {
pub fn new(endpoint: String) -> Self {
AuthEndPoint(endpoint)
}
pub fn value(&self) -> &str {
&self.0
}
}
impl From<&str> for AuthEndPoint {
fn from(value: &str) -> Self {
AuthEndPoint(value.to_string())
}
}
impl From<String> for AuthEndPoint {
fn from(value: String) -> Self {
AuthEndPoint(value)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ClientID(pub(crate) String);
impl ClientID {
pub fn new(id: String) -> Self {
ClientID(id)
}
pub fn value(&self) -> &str {
&self.0
}
}
impl From<&str> for ClientID {
fn from(value: &str) -> Self {
ClientID(value.to_string())
}
}
impl From<String> for ClientID {
fn from(value: String) -> Self {
ClientID(value)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ClientSecret(pub(crate) String);
impl ClientSecret {
pub fn new(secret: String) -> Self {
ClientSecret(secret)
}
pub fn value(&self) -> &str {
&self.0
}
}
impl From<&str> for ClientSecret {
fn from(value: &str) -> Self {
ClientSecret(value.to_string())
}
}
impl From<String> for ClientSecret {
fn from(value: String) -> Self {
ClientSecret(value)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TokenEndPoint(pub(crate) String);
impl TokenEndPoint {
pub fn new(endpoint: String) -> Self {
TokenEndPoint(endpoint)
}
pub fn value(&self) -> &str {
&self.0
}
}
impl From<&str> for TokenEndPoint {
fn from(value: &str) -> Self {
TokenEndPoint(value.to_string())
}
}
impl From<String> for TokenEndPoint {
fn from(value: String) -> Self {
TokenEndPoint(value)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RedirectURI(pub(crate) String);
impl RedirectURI {
pub fn new(uri: String) -> Self {
RedirectURI(uri)
}
pub fn value(&self) -> &str {
&self.0
}
}
impl From<&str> for RedirectURI {
fn from(value: &str) -> Self {
RedirectURI(value.to_string())
}
}
impl From<String> for RedirectURI {
fn from(value: String) -> Self {
RedirectURI(value)
}
}
#[cfg(test)]
mod tests {
use crate::config::Config;
use super::ConfigBuilder;
#[test]
fn test_config_builder() {
let auth_endpoint = "https://auth.example.com/auth";
let client_id = "my_client_id";
let client_secret = "my_secret";
let token_endpoint = "https://token.example.com";
let redirect_uri = "https://redirect.example.com";
let config = ConfigBuilder::new()
.auth_endpoint(auth_endpoint)
.client_id(client_id)
.client_secret(client_secret)
.token_endpoint(token_endpoint)
.redirect_uri(redirect_uri)
.build();
assert_eq!(config.auth_endpoint.0, auth_endpoint);
assert_eq!(config.client_id.0, client_id);
assert_eq!(config.client_secret.0, client_secret);
assert_eq!(config.token_endpoint.0, token_endpoint);
assert_eq!(config.redirect_uri.0, redirect_uri);
}
#[test]
fn test_config_builder_default() {
let config_builder = ConfigBuilder::default();
assert_eq!(config_builder.auth_endpoint.0, "");
assert_eq!(config_builder.client_id.0, "");
assert_eq!(config_builder.client_secret.0, "");
assert_eq!(config_builder.token_endpoint.0, "");
assert_eq!(config_builder.redirect_uri.0, "");
}
#[test]
fn test_config_builder_method_chain() {
let auth_endpoint = "https://auth.example.com/auth";
let client_id = "my_client_id";
let client_secret = "my_secret";
let token_endpoint = "https://token.example.com";
let redirect_uri = "https://redirect.example.com";
let config = Config::builder()
.auth_endpoint(auth_endpoint)
.client_id(client_id)
.client_secret(client_secret)
.token_endpoint(token_endpoint)
.redirect_uri(redirect_uri)
.build();
assert_eq!(config.auth_endpoint.0, auth_endpoint);
assert_eq!(config.client_id.0, client_id);
assert_eq!(config.client_secret.0, client_secret);
assert_eq!(config.token_endpoint.0, token_endpoint);
assert_eq!(config.redirect_uri.0, redirect_uri);
}
}