Skip to main content

rustauth_core/user/
input.rs

1use time::OffsetDateTime;
2
3use crate::db::{DbRecord, DbValue};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct CreateUserInput {
7    pub id: Option<String>,
8    pub name: String,
9    pub email: String,
10    pub email_verified: bool,
11    pub image: Option<String>,
12    pub username: Option<String>,
13    pub display_username: Option<String>,
14    pub additional_fields: DbRecord,
15}
16
17impl CreateUserInput {
18    pub fn new(name: impl Into<String>, email: impl Into<String>) -> Self {
19        Self {
20            id: None,
21            name: name.into(),
22            email: email.into(),
23            email_verified: false,
24            image: None,
25            username: None,
26            display_username: None,
27            additional_fields: DbRecord::new(),
28        }
29    }
30
31    #[must_use]
32    pub fn id(mut self, id: impl Into<String>) -> Self {
33        self.id = Some(id.into());
34        self
35    }
36
37    #[must_use]
38    pub fn email_verified(mut self, email_verified: bool) -> Self {
39        self.email_verified = email_verified;
40        self
41    }
42
43    #[must_use]
44    pub fn image(mut self, image: impl Into<String>) -> Self {
45        self.image = Some(image.into());
46        self
47    }
48
49    #[must_use]
50    pub fn username(mut self, username: impl Into<String>) -> Self {
51        self.username = Some(username.into());
52        self
53    }
54
55    #[must_use]
56    pub fn display_username(mut self, display_username: impl Into<String>) -> Self {
57        self.display_username = Some(display_username.into());
58        self
59    }
60
61    #[must_use]
62    pub fn additional_fields(mut self, additional_fields: DbRecord) -> Self {
63        self.additional_fields = additional_fields;
64        self
65    }
66}
67
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct CreateCredentialAccountInput {
70    pub id: Option<String>,
71    pub user_id: String,
72    pub password_hash: String,
73}
74
75impl CreateCredentialAccountInput {
76    pub fn new(user_id: impl Into<String>, password_hash: impl Into<String>) -> Self {
77        Self {
78            id: None,
79            user_id: user_id.into(),
80            password_hash: password_hash.into(),
81        }
82    }
83
84    #[must_use]
85    pub fn id(mut self, id: impl Into<String>) -> Self {
86        self.id = Some(id.into());
87        self
88    }
89}
90
91#[derive(Debug, Clone, PartialEq, Eq)]
92pub struct CreateOAuthAccountInput {
93    pub id: Option<String>,
94    pub provider_id: String,
95    pub account_id: String,
96    pub user_id: String,
97    pub access_token: Option<String>,
98    pub refresh_token: Option<String>,
99    pub id_token: Option<String>,
100    pub access_token_expires_at: Option<OffsetDateTime>,
101    pub refresh_token_expires_at: Option<OffsetDateTime>,
102    pub scope: Option<String>,
103}
104
105#[derive(Debug, Clone, Default, PartialEq, Eq)]
106pub struct UpdateAccountInput {
107    pub access_token: Option<Option<String>>,
108    pub refresh_token: Option<Option<String>>,
109    pub id_token: Option<Option<String>>,
110    pub access_token_expires_at: Option<Option<OffsetDateTime>>,
111    pub refresh_token_expires_at: Option<Option<OffsetDateTime>>,
112    pub scope: Option<Option<String>>,
113}
114
115#[derive(Debug, Clone, Default, PartialEq)]
116pub struct UpdateUserInput {
117    pub name: Option<String>,
118    pub image: Option<Option<String>>,
119    pub username: Option<Option<String>>,
120    pub display_username: Option<Option<String>>,
121    pub fields: Vec<(String, DbValue)>,
122    pub additional_fields: DbRecord,
123}
124
125impl UpdateUserInput {
126    pub fn new() -> Self {
127        Self::default()
128    }
129
130    #[must_use]
131    pub fn name(mut self, name: impl Into<String>) -> Self {
132        self.name = Some(name.into());
133        self
134    }
135
136    #[must_use]
137    pub fn image(mut self, image: Option<String>) -> Self {
138        self.image = Some(image);
139        self
140    }
141
142    #[must_use]
143    pub fn username(mut self, username: Option<String>) -> Self {
144        self.username = Some(username);
145        self
146    }
147
148    #[must_use]
149    pub fn display_username(mut self, display_username: Option<String>) -> Self {
150        self.display_username = Some(display_username);
151        self
152    }
153
154    #[must_use]
155    pub fn field(mut self, field: impl Into<String>, value: DbValue) -> Self {
156        self.fields.push((field.into(), value));
157        self
158    }
159
160    pub fn is_empty(&self) -> bool {
161        self.name.is_none()
162            && self.image.is_none()
163            && self.username.is_none()
164            && self.display_username.is_none()
165            && self.fields.is_empty()
166            && self.additional_fields.is_empty()
167    }
168
169    #[must_use]
170    pub fn additional_fields(mut self, additional_fields: DbRecord) -> Self {
171        self.additional_fields = additional_fields;
172        self
173    }
174}