Skip to content

Commit

Permalink
refactor: make some options non-exhaustive
Browse files Browse the repository at this point in the history
BREAKING-CHANGE: In order to allow adding new options in the future,
 the structs LoginOptions and LogoutOptions have been made
 non-exhaustive. Adding some documentation on how to work with them.
  • Loading branch information
ctron committed Jan 25, 2024
1 parent 3c44274 commit 066e0d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/agent/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::LoginOptions;
use crate::agent::Client;
use std::time::Duration;

use super::LoginOptions;

#[derive(Clone, Debug)]
pub struct AgentConfiguration<C: Client> {
pub config: C::Configuration,
Expand Down
36 changes: 31 additions & 5 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,29 @@ use wasm_bindgen::JsValue;
use wasm_bindgen_futures::spawn_local;
use yew::Callback;

/// Options for the login process
///
/// ## Non-exhaustive struct
///
/// The struct is "non exhaustive", which means that it is possible to add fields without breaking the API.
///
/// In order to create an instance, follow the following pattern:
///
/// ```rust
/// # use reqwest::Url;
/// # use yew_oauth2::prelude::LoginOptions;
/// # let url = Url::parse("https://example.com").unwrap();
/// let opts = LoginOptions::default().with_redirect_url(url);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LoginOptions {
pub query: HashMap<String, String>,
pub(crate) query: HashMap<String, String>,

/// Defines the redirect URL.
///
/// If this field is empty, the current URL is used as a redirect URL.
pub redirect_url: Option<Url>,
pub(crate) redirect_url: Option<Url>,
}

impl LoginOptions {
Expand All @@ -53,18 +68,29 @@ impl LoginOptions {
self
}

pub fn with_redirect_url(mut self, redirect_url: Url) -> Self {
self.redirect_url = Some(redirect_url);
pub fn with_redirect_url(mut self, redirect_url: impl Into<Url>) -> Self {
self.redirect_url = Some(redirect_url.into());
self
}
}

/// Options for the logout process
///
///**NOTE**: This is a non-exhaustive struct. See [`LoginOptions`] for an example on how to work with this.
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LogoutOptions {
/// An optional target to navigate to after the user was logged out.
///
/// This would override any settings from the client configuration.
pub target: Option<Url>,
pub(crate) target: Option<Url>,
}

impl LogoutOptions {
pub fn with_target(mut self, target: impl Into<Url>) -> Self {
self.target = Some(target.into());
self
}
}

pub enum Msg<C>
Expand Down
6 changes: 2 additions & 4 deletions src/components/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ mod agent;

pub use agent::*;

use crate::context::LatestAccessToken;
use crate::prelude::LoginOptions;
use crate::{
agent::{AgentConfiguration, Client, OAuth2Operations},
context::OAuth2Context,
agent::{AgentConfiguration, Client, LoginOptions, OAuth2Operations},
context::{LatestAccessToken, OAuth2Context},
};
use agent::Agent as AgentContext;
use std::time::Duration;
Expand Down

0 comments on commit 066e0d7

Please sign in to comment.