1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#![warn(missing_docs)]
pub mod errors;
pub mod ops;
#[cfg(feature = "test")]
pub mod tests;
pub use ops::GetConnection;
pub mod prelude {
pub use super::errors::*;
pub use super::ops::*;
pub use super::*;
}
pub mod dev {
pub use super::prelude::*;
pub use async_trait::async_trait;
}
#[derive(Clone, Debug)]
pub struct Creds {
pub username: String,
pub password: String,
}
#[derive(Clone, Debug)]
pub struct Password {
pub password: String,
}
pub struct EmailRegisterPayload<'a> {
pub username: &'a str,
pub password: &'a str,
pub email: &'a str,
pub secret: &'a str,
}
pub struct UsernameRegisterPayload<'a> {
pub username: &'a str,
pub password: &'a str,
pub secret: &'a str,
}
#[derive(Clone, Debug)]
pub struct UpdateEmailPayload<'a> {
pub username: &'a str,
pub email: &'a str,
}
pub struct UpdateUsernamePayload<'a> {
pub old_username: &'a str,
pub new_username: &'a str,
}
use dev::*;
#[async_trait]
pub trait LibAdminDatabase: std::marker::Send + std::marker::Sync {
async fn update_email(&self, payload: &UpdateEmailPayload) -> DBResult<()>;
async fn update_password(&self, payload: &Creds) -> DBResult<()>;
async fn email_exists(&self, email: &str) -> DBResult<bool>;
async fn delete_account(&self, username: &str) -> DBResult<()>;
async fn username_exists(&self, username: &str) -> DBResult<bool>;
async fn update_username(&self, payload: &UpdateUsernamePayload) -> DBResult<()>;
async fn update_secret(&self, username: &str, secret: &str) -> DBResult<()>;
async fn get_secret(&self, username: &str) -> DBResult<String>;
async fn email_login(&self, email: &str) -> DBResult<Creds>;
async fn username_login(&self, username: &str) -> DBResult<Password>;
async fn email_register(&self, payload: &EmailRegisterPayload) -> DBResult<()>;
async fn username_register(&self, payload: &UsernameRegisterPayload) -> DBResult<()>;
}
#[async_trait]
impl<T: LibAdminDatabase + ?Sized> LibAdminDatabase for Box<T> {
async fn update_email(&self, payload: &UpdateEmailPayload) -> DBResult<()> {
(**self).update_email(payload).await
}
async fn update_password(&self, payload: &Creds) -> DBResult<()> {
(**self).update_password(payload).await
}
async fn email_exists(&self, email: &str) -> DBResult<bool> {
(**self).email_exists(email).await
}
async fn delete_account(&self, username: &str) -> DBResult<()> {
(**self).delete_account(username).await
}
async fn username_exists(&self, username: &str) -> DBResult<bool> {
(**self).username_exists(username).await
}
async fn update_username(&self, payload: &UpdateUsernamePayload) -> DBResult<()> {
(**self).update_username(payload).await
}
async fn update_secret(&self, username: &str, secret: &str) -> DBResult<()> {
(**self).update_secret(username, secret).await
}
async fn get_secret(&self, username: &str) -> DBResult<String> {
(**self).get_secret(username).await
}
async fn email_login(&self, email: &str) -> DBResult<Creds> {
(**self).email_login(email).await
}
async fn username_login(&self, username: &str) -> DBResult<Password> {
(**self).username_login(username).await
}
async fn email_register(&self, payload: &EmailRegisterPayload) -> DBResult<()> {
(**self).email_register(payload).await
}
async fn username_register(&self, payload: &UsernameRegisterPayload) -> DBResult<()> {
(**self).username_register(payload).await
}
}