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 24, 2024
1 parent 3c44274 commit 0a66625
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 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
42 changes: 40 additions & 2 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,34 @@ 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 patterns:
///
/// ```rust
/// # use reqwest::Url;
/// # use yew_oauth2::prelude::LoginOptions;
/// # let url = Url::parse("https://example.com").unwrap();
/// let opts = LoginOptions {
/// redirect_url: Some(url),
/// ..Default::default()
/// };
/// ```
///
/// Or use the "with"-style 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>,

Expand Down Expand Up @@ -53,12 +80,16 @@ 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.
Expand All @@ -67,6 +98,13 @@ pub struct LogoutOptions {
pub 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>
where
C: Client,
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 0a66625

Please sign in to comment.