openauth_plugins/anonymous/
options.rs1use std::future::Future;
2use std::pin::Pin;
3use std::sync::Arc;
4
5use openauth_core::error::OpenAuthError;
6
7use super::hooks::AnonymousLinkAccount;
8
9pub type AnonymousOptionFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;
10pub type GenerateRandomEmail = Arc<dyn Fn() -> AnonymousOptionFuture<String> + Send + Sync>;
11pub type GenerateName = Arc<dyn Fn() -> AnonymousOptionFuture<String> + Send + Sync>;
12pub type OnLinkAccount = Arc<
13 dyn Fn(AnonymousLinkAccount) -> AnonymousOptionFuture<Result<(), OpenAuthError>> + Send + Sync,
14>;
15
16#[derive(Clone, Default)]
17pub struct AnonymousOptions {
18 pub email_domain_name: Option<String>,
19 pub generate_random_email: Option<GenerateRandomEmail>,
20 pub generate_name: Option<GenerateName>,
21 pub disable_delete_anonymous_user: bool,
22 pub on_link_account: Option<OnLinkAccount>,
23 pub field_name: Option<String>,
24}
25
26impl AnonymousOptions {
27 pub(crate) fn storage_field_name(&self) -> &str {
28 self.field_name.as_deref().unwrap_or("is_anonymous")
29 }
30
31 #[must_use]
32 pub fn email_domain_name(mut self, domain: impl Into<String>) -> Self {
33 self.email_domain_name = Some(domain.into());
34 self
35 }
36
37 #[must_use]
38 pub fn generate_random_email<F>(mut self, generator: F) -> Self
39 where
40 F: Fn() -> String + Send + Sync + 'static,
41 {
42 self.generate_random_email = Some(Arc::new(move || {
43 let value = generator();
44 Box::pin(std::future::ready(value))
45 }));
46 self
47 }
48
49 #[must_use]
50 pub fn generate_random_email_async<F, Fut>(mut self, generator: F) -> Self
51 where
52 F: Fn() -> Fut + Send + Sync + 'static,
53 Fut: Future<Output = String> + Send + 'static,
54 {
55 self.generate_random_email = Some(Arc::new(move || Box::pin(generator())));
56 self
57 }
58
59 #[must_use]
60 pub fn generate_name<F>(mut self, generator: F) -> Self
61 where
62 F: Fn() -> String + Send + Sync + 'static,
63 {
64 self.generate_name = Some(Arc::new(move || {
65 let value = generator();
66 Box::pin(std::future::ready(value))
67 }));
68 self
69 }
70
71 #[must_use]
72 pub fn generate_name_async<F, Fut>(mut self, generator: F) -> Self
73 where
74 F: Fn() -> Fut + Send + Sync + 'static,
75 Fut: Future<Output = String> + Send + 'static,
76 {
77 self.generate_name = Some(Arc::new(move || Box::pin(generator())));
78 self
79 }
80
81 #[must_use]
82 pub fn disable_delete_anonymous_user(mut self, disabled: bool) -> Self {
83 self.disable_delete_anonymous_user = disabled;
84 self
85 }
86
87 #[must_use]
88 pub fn on_link_account<F>(mut self, callback: F) -> Self
89 where
90 F: Fn(AnonymousLinkAccount) -> Result<(), OpenAuthError> + Send + Sync + 'static,
91 {
92 self.on_link_account = Some(Arc::new(move |data| {
93 let result = callback(data);
94 Box::pin(std::future::ready(result))
95 }));
96 self
97 }
98
99 #[must_use]
100 pub fn on_link_account_async<F, Fut>(mut self, callback: F) -> Self
101 where
102 F: Fn(AnonymousLinkAccount) -> Fut + Send + Sync + 'static,
103 Fut: Future<Output = Result<(), OpenAuthError>> + Send + 'static,
104 {
105 self.on_link_account = Some(Arc::new(move |data| Box::pin(callback(data))));
106 self
107 }
108
109 #[must_use]
110 pub fn field_name(mut self, field_name: impl Into<String>) -> Self {
111 self.field_name = Some(field_name.into());
112 self
113 }
114}