ic_auth_client/util/
callback.rs

1//! Authentication callback handlers for login success and error scenarios.
2
3use crate::api::AuthResponseSuccess;
4use parking_lot::Mutex;
5use std::sync::Arc;
6
7pub(crate) type OnSuccessInner = Box<dyn FnMut(AuthResponseSuccess) + Send>;
8
9/// The callback executed upon successful login that takes [`AuthResponseSuccess`](crate::api::AuthResponseSuccess) as an argument.
10///
11/// # Usage
12/// ```
13/// use ic_auth_client::callback::OnSuccess;
14///
15/// let on_success = OnSuccess::from(|res| {
16///     // Handle successful login
17/// });
18/// ```
19#[derive(Clone)]
20pub struct OnSuccess(pub(crate) Arc<Mutex<OnSuccessInner>>);
21
22impl<F> From<F> for OnSuccess
23where
24    F: FnMut(AuthResponseSuccess) + Send + 'static,
25{
26    fn from(f: F) -> Self {
27        OnSuccess(Arc::new(Mutex::new(Box::new(f))))
28    }
29}
30
31pub(crate) type OnErrorInner = Box<dyn FnMut(Option<String>) + Send>;
32
33/// The callback executed upon failed login that takes [`Option<String>`](std::option::Option) as an argument.
34///
35/// # Usage
36/// ```
37/// use ic_auth_client::callback::OnError;
38///
39/// let on_error = OnError::from(|err| {
40///     // Handle failed login
41/// });
42/// ```
43#[derive(Clone)]
44pub struct OnError(pub(crate) Arc<Mutex<OnErrorInner>>);
45
46impl<F> From<F> for OnError
47where
48    F: FnMut(Option<String>) + Send + 'static,
49{
50    fn from(f: F) -> Self {
51        OnError(Arc::new(Mutex::new(Box::new(f))))
52    }
53}