1use std::path::Path;
2
3use chrono::{DateTime, Utc};
4use crate_meta::CrateMeta;
5use kellnr_common::crate_data::CrateData;
6use kellnr_common::crate_overview::CrateOverview;
7use kellnr_common::cratesio_prefetch_msg::CratesioPrefetchMsg;
8use kellnr_common::index_metadata::IndexMetadata;
9use kellnr_common::normalized_name::NormalizedName;
10use kellnr_common::original_name::OriginalName;
11use kellnr_common::prefetch::Prefetch;
12use kellnr_common::publish_metadata::PublishMetadata;
13use kellnr_common::version::Version;
14use kellnr_common::webhook::{Webhook, WebhookEvent, WebhookQueue};
15use sea_orm::prelude::async_trait::async_trait;
16
17use crate::error::DbError;
18use crate::{AuthToken, CrateSummary, DocQueueEntry, Group, User, crate_meta};
19
20pub type DbResult<T> = Result<T, DbError>;
21#[derive(Debug, PartialEq, Eq)]
22pub enum PrefetchState {
23 UpToDate,
24 NeedsUpdate(Prefetch),
25 NotFound,
26}
27
28#[async_trait]
29pub trait DbProvider: Send + Sync {
30 async fn get_last_updated_crate(&self) -> DbResult<Option<(OriginalName, Version)>>;
31 async fn authenticate_user(&self, name: &str, pwd: &str) -> DbResult<User>;
32 async fn increase_download_counter(
33 &self,
34 crate_name: &NormalizedName,
35 crate_version: &Version,
36 ) -> DbResult<()>;
37 async fn increase_cached_download_counter(
38 &self,
39 crate_name: &NormalizedName,
40 crate_version: &Version,
41 ) -> DbResult<()>;
42 async fn validate_session(&self, session_token: &str) -> DbResult<(String, bool)>;
43 async fn add_session_token(&self, name: &str, session_token: &str) -> DbResult<()>;
44 async fn add_crate_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<()>;
45 async fn add_owner(&self, crate_name: &NormalizedName, owner: &str) -> DbResult<()>;
46 async fn is_download_restricted(&self, crate_name: &NormalizedName) -> DbResult<bool>;
47 async fn change_download_restricted(
48 &self,
49 crate_name: &NormalizedName,
50 restricted: bool,
51 ) -> DbResult<()>;
52 async fn is_crate_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>;
53 async fn is_owner(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>;
54 async fn get_crate_id(&self, crate_name: &NormalizedName) -> DbResult<Option<i64>>;
55 async fn get_crate_owners(&self, crate_name: &NormalizedName) -> DbResult<Vec<User>>;
56 async fn get_crate_users(&self, crate_name: &NormalizedName) -> DbResult<Vec<User>>;
57 async fn get_crate_versions(&self, crate_name: &NormalizedName) -> DbResult<Vec<Version>>;
58 async fn delete_session_token(&self, session_token: &str) -> DbResult<()>;
59 async fn delete_user(&self, user_name: &str) -> DbResult<()>;
60 async fn change_pwd(&self, user_name: &str, new_pwd: &str) -> DbResult<()>;
61 async fn change_read_only_state(&self, user_name: &str, state: bool) -> DbResult<()>;
62 async fn crate_version_exists(&self, crate_id: i64, version: &str) -> DbResult<bool>;
63 async fn get_max_version_from_id(&self, crate_id: i64) -> DbResult<Version>;
64 async fn get_max_version_from_name(&self, crate_name: &NormalizedName) -> DbResult<Version>;
65 async fn update_max_version(&self, crate_id: i64, version: &Version) -> DbResult<()>;
66 async fn add_auth_token(&self, name: &str, token: &str, user: &str) -> DbResult<()>;
67 async fn get_user_from_token(&self, token: &str) -> DbResult<User>;
68 async fn get_user(&self, name: &str) -> DbResult<User>;
69 async fn get_auth_tokens(&self, user_name: &str) -> DbResult<Vec<AuthToken>>;
70 async fn delete_auth_token(&self, id: i32) -> DbResult<()>;
71 async fn delete_owner(&self, crate_name: &str, owner: &str) -> DbResult<()>;
72 async fn delete_crate_user(&self, crate_name: &str, user: &str) -> DbResult<()>;
73 async fn add_user(
74 &self,
75 name: &str,
76 pwd: &str,
77 salt: &str,
78 is_admin: bool,
79 is_read_only: bool,
80 ) -> DbResult<()>;
81 async fn get_users(&self) -> DbResult<Vec<User>>;
82 async fn add_group(&self, name: &str) -> DbResult<()>;
83 async fn get_group(&self, name: &str) -> DbResult<Group>;
84 async fn get_groups(&self) -> DbResult<Vec<Group>>;
85 async fn delete_group(&self, name: &str) -> DbResult<()>;
86 async fn add_group_user(&self, group_name: &str, user: &str) -> DbResult<()>;
87 async fn delete_group_user(&self, group_name: &str, user: &str) -> DbResult<()>;
88 async fn get_group_users(&self, group_name: &str) -> DbResult<Vec<User>>;
89 async fn is_group_user(&self, group_name: &str, group: &str) -> DbResult<bool>;
90 async fn add_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>;
91 async fn delete_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>;
92 async fn get_crate_groups(&self, crate_name: &NormalizedName) -> DbResult<Vec<Group>>;
93 async fn is_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<bool>;
94 async fn is_crate_group_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>;
95 async fn get_total_unique_crates(&self) -> DbResult<u32>;
96 async fn get_total_crate_versions(&self) -> DbResult<u32>;
97 async fn get_total_downloads(&self) -> DbResult<u64>;
98 async fn get_top_crates_downloads(&self, top: u32) -> DbResult<Vec<(String, u64)>>;
99 async fn get_total_unique_cached_crates(&self) -> DbResult<u64>;
100 async fn get_total_cached_crate_versions(&self) -> DbResult<u64>;
101 async fn get_total_cached_downloads(&self) -> DbResult<u64>;
102 async fn get_crate_summaries(&self) -> DbResult<Vec<CrateSummary>>;
103 async fn add_doc_queue(
104 &self,
105 krate: &NormalizedName,
106 version: &Version,
107 path: &Path,
108 ) -> DbResult<()>;
109 async fn delete_doc_queue(&self, id: i64) -> DbResult<()>;
110 async fn get_doc_queue(&self) -> DbResult<Vec<DocQueueEntry>>;
111 async fn delete_crate(&self, krate: &NormalizedName, version: &Version) -> DbResult<()>;
112 async fn get_crate_meta_list(&self, crate_name: &NormalizedName) -> DbResult<Vec<CrateMeta>>;
113 async fn update_last_updated(&self, id: i64, last_updated: &DateTime<Utc>) -> DbResult<()>;
114 async fn search_in_crate_name(
115 &self,
116 contains: &str,
117 cache: bool,
118 ) -> DbResult<Vec<CrateOverview>>;
119 async fn get_crate_overview_list(
120 &self,
121 limit: u64,
122 offset: u64,
123 cache: bool,
124 ) -> DbResult<Vec<CrateOverview>>;
125 async fn get_crate_data(&self, crate_name: &NormalizedName) -> DbResult<CrateData>;
126 async fn add_empty_crate(&self, name: &str, created: &DateTime<Utc>) -> DbResult<i64>;
127 async fn add_crate(
128 &self,
129 pub_metadata: &PublishMetadata,
130 cksum: &str,
131 created: &DateTime<Utc>,
132 owner: &str,
133 ) -> DbResult<i64>;
134 async fn update_docs_link(
135 &self,
136 crate_name: &NormalizedName,
137 version: &Version,
138 docs_link: &str,
139 ) -> DbResult<()>;
140 async fn add_crate_metadata(
141 &self,
142 pub_metadata: &PublishMetadata,
143 created: &str,
144 crate_id: i64,
145 ) -> DbResult<()>;
146 async fn get_prefetch_data(&self, crate_name: &str) -> DbResult<Prefetch>;
147 async fn is_cratesio_cache_up_to_date(
148 &self,
149 crate_name: &NormalizedName,
150 if_none_match: Option<String>,
151 if_modified_since: Option<String>,
152 ) -> DbResult<PrefetchState>;
153 async fn add_cratesio_prefetch_data(
154 &self,
155 crate_name: &OriginalName,
156 etag: &str,
157 last_modified: &str,
158 description: Option<String>,
159 indices: &[IndexMetadata],
160 ) -> DbResult<Prefetch>;
161 async fn get_cratesio_index_update_list(&self) -> DbResult<Vec<CratesioPrefetchMsg>>;
162 async fn unyank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()>;
163 async fn yank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()>;
164 async fn register_webhook(&self, webhook: Webhook) -> DbResult<String>;
165 async fn delete_webhook(&self, id: &str) -> DbResult<()>;
166 async fn get_webhook(&self, id: &str) -> DbResult<Webhook>;
167 async fn get_all_webhooks(&self) -> DbResult<Vec<Webhook>>;
168 async fn add_webhook_queue(
172 &self,
173 event: WebhookEvent,
174 payload: serde_json::Value,
175 ) -> DbResult<()>;
176 async fn get_pending_webhook_queue_entries(
178 &self,
179 timestamp: DateTime<Utc>,
180 ) -> DbResult<Vec<WebhookQueue>>;
181 async fn update_webhook_queue(
182 &self,
183 id: &str,
184 last_attempt: DateTime<Utc>,
185 next_attempt: DateTime<Utc>,
186 ) -> DbResult<()>;
187 async fn delete_webhook_queue(&self, id: &str) -> DbResult<()>;
188}
189
190pub mod mock {
191 use chrono::DateTime;
192 use mockall::predicate::*;
193 use mockall::*;
194
195 use super::*;
196
197 mock! {
198 pub Db {}
199
200 #[async_trait]
201 impl DbProvider for Db {
202
203 async fn get_last_updated_crate(&self) -> DbResult<Option<(OriginalName, Version)>> {
204 unimplemented!()
205 }
206
207 async fn authenticate_user(&self, _name: &str, _pwd: &str) -> DbResult<User> {
208 unimplemented!()
209 }
210
211 async fn increase_download_counter(
212 &self,
213 _crate_name: &NormalizedName,
214 _crate_version: &Version,
215 ) -> DbResult<()> {
216 unimplemented!()
217 }
218
219 async fn increase_cached_download_counter(
220 &self,
221 _crate_name: &NormalizedName,
222 _crate_version: &Version,
223 ) -> DbResult<()> {
224 unimplemented!()
225 }
226
227 async fn validate_session(&self, _session_token: &str) -> DbResult<(String, bool)> {
228 unimplemented!()
229 }
230
231 async fn add_session_token(&self, _name: &str, _session_token: &str) -> DbResult<()> {
232 unimplemented!()
233 }
234
235 async fn add_crate_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<()> {
236 unimplemented!()
237 }
238
239 async fn add_owner(&self, _crate_name: &NormalizedName, _owner: &str) -> DbResult<()> {
240 unimplemented!()
241 }
242
243 async fn is_download_restricted(&self, crate_name: &NormalizedName) -> DbResult<bool> {
244 unimplemented!()
245 }
246
247 async fn change_download_restricted(
248 &self,
249 crate_name: &NormalizedName,
250 restricted: bool,
251 ) -> DbResult<()> {
252 unimplemented!()
253 }
254
255 async fn is_crate_user(&self, _crate_name: &NormalizedName, _user: &str) -> DbResult<bool> {
256 unimplemented!()
257 }
258
259 async fn is_owner(&self, _crate_name: &NormalizedName, _user: &str) -> DbResult<bool> {
260 unimplemented!()
261 }
262
263 async fn get_crate_id(&self, _crate_name: &NormalizedName) -> DbResult<Option<i64>> {
264 unimplemented!()
265 }
266
267 async fn get_crate_owners(&self, _crate_name: &NormalizedName) -> DbResult<Vec<User>> {
268 unimplemented!()
269 }
270
271 async fn get_crate_users(&self, _crate_name: &NormalizedName) -> DbResult<Vec<User>> {
272 unimplemented!()
273 }
274
275 async fn get_crate_versions(&self, _crate_name: &NormalizedName) -> DbResult<Vec<Version>> {
276 unimplemented!()
277 }
278
279 async fn delete_session_token(&self, _session_token: &str) -> DbResult<()> {
280 unimplemented!()
281 }
282
283 async fn delete_user(&self, _user_name: &str) -> DbResult<()> {
284 unimplemented!()
285 }
286
287 async fn change_pwd(&self, _user_name: &str, _new_pwd: &str) -> DbResult<()> {
288 unimplemented!()
289 }
290
291 async fn change_read_only_state(&self, _user_name: &str, _state: bool) -> DbResult<()> {
292 unimplemented!()
293 }
294
295 async fn crate_version_exists(&self, _crate_id: i64, _version: &str) -> DbResult<bool> {
296 unimplemented!()
297 }
298
299 async fn get_max_version_from_id(&self, crate_id: i64) -> DbResult<Version> {
300 unimplemented!()
301 }
302
303 async fn get_max_version_from_name(&self, crate_name: &NormalizedName) -> DbResult<Version> {
304 unimplemented!()
305 }
306
307 async fn update_max_version(&self, crate_id: i64, version: &Version) -> DbResult<()> {
308 unimplemented!()
309 }
310
311 async fn add_auth_token(&self, _name: &str, _token: &str, _user: &str) -> DbResult<()> {
312 unimplemented!()
313 }
314
315 async fn get_user_from_token(&self, _token: &str) -> DbResult<User> {
316 unimplemented!()
317 }
318
319 async fn get_user(&self, _name: &str) -> DbResult<User> {
320 unimplemented!()
321 }
322
323 async fn get_auth_tokens(&self, _user_name: &str) -> DbResult<Vec<AuthToken>> {
324 unimplemented!()
325 }
326
327 async fn delete_auth_token(&self, _id: i32) -> DbResult<()> {
328 unimplemented!()
329 }
330
331 async fn delete_owner(&self, _crate_name: &str, _owner: &str) -> DbResult<()> {
332 unimplemented!()
333 }
334
335 async fn delete_crate_user(&self, crate_name: &str, user: &str) -> DbResult<()>{
336 unimplemented!()
337 }
338
339 async fn add_user(&self, _name: &str, _pwd: &str, _salt: &str, _is_admin: bool, _is_read_only: bool) -> DbResult<()> {
340 unimplemented!()
341 }
342
343 async fn get_users(&self) -> DbResult<Vec<User>> {
344 unimplemented!()
345 }
346
347 async fn get_total_unique_crates(&self) -> DbResult<u32> {
348 unimplemented!()
349 }
350
351 async fn get_total_crate_versions(&self) -> DbResult<u32> {
352 unimplemented!()
353 }
354
355 async fn get_top_crates_downloads(&self, _top: u32) -> DbResult<Vec<(String, u64)>> {
356 unimplemented!()
357 }
358
359 async fn get_total_unique_cached_crates(&self) -> DbResult<u64> {
360 unimplemented!()
361 }
362
363 async fn get_total_cached_crate_versions(&self) -> DbResult<u64> {
364 unimplemented!()
365 }
366
367 async fn get_total_cached_downloads(&self) -> DbResult<u64> {
368 unimplemented!()
369 }
370
371 async fn get_crate_summaries(&self) -> DbResult<Vec<CrateSummary >> {
372 unimplemented!()
373 }
374
375 async fn add_doc_queue(&self, krate: &NormalizedName, version: &Version, path: &Path) -> DbResult<()>{
376 unimplemented!()
377 }
378
379 async fn delete_doc_queue(&self, id: i64) -> DbResult<()>{
380 unimplemented!()
381 }
382
383 async fn get_doc_queue(&self) -> DbResult<Vec<DocQueueEntry>> {
384 unimplemented!()
385 }
386
387 async fn delete_crate(&self, krate: &NormalizedName, version: &Version) -> DbResult<()> {
388 unimplemented!()
389 }
390
391 async fn get_total_downloads(&self) -> DbResult<u64>{
392 unimplemented!()
393 }
394
395 async fn get_crate_meta_list(&self, crate_name: &NormalizedName) -> DbResult<Vec<CrateMeta>>{
396 unimplemented!()
397 }
398
399 async fn update_last_updated(&self, id: i64, last_updated: &DateTime<Utc>) -> DbResult<()>{
400 unimplemented!()
401 }
402
403 async fn search_in_crate_name(&self, contains: &str, cache: bool) -> DbResult<Vec<CrateOverview>> {
404 unimplemented!()
405 }
406
407 async fn get_crate_overview_list(&self, limit: u64, offset: u64, cache: bool) -> DbResult<Vec<CrateOverview >> {
408 unimplemented!()
409 }
410
411 async fn get_crate_data(&self, crate_name: &NormalizedName) -> DbResult<CrateData> {
412 unimplemented!()
413 }
414
415 async fn add_empty_crate(&self, name: &str, created: &DateTime<Utc>) -> DbResult<i64> {
416 unimplemented!()
417 }
418
419 async fn add_crate(&self, pub_metadata: &PublishMetadata, sha256: &str, created: &DateTime<Utc>, owner: &str) -> DbResult<i64> {
420 unimplemented!()
421 }
422
423 async fn update_docs_link(&self, crate_name: &NormalizedName, version: &Version, docs_link: &str) -> DbResult<()> {
424 unimplemented!()
425 }
426
427 async fn add_crate_metadata(&self, pub_metadata: &PublishMetadata, created: &str, crate_id: i64,) -> DbResult<()> {
428 unimplemented!()
429 }
430
431 async fn get_prefetch_data(&self, crate_name: &str) -> DbResult<Prefetch> {
432 unimplemented!()
433 }
434
435 async fn is_cratesio_cache_up_to_date(&self, crate_name: &NormalizedName, etag: Option<String>, last_modified: Option<String>) -> DbResult<PrefetchState> {
436 unimplemented!()
437 }
438
439 async fn add_cratesio_prefetch_data(
440 &self,
441 crate_name: &OriginalName,
442 etag: &str,
443 last_modified: &str,
444 description: Option<String>,
445 indices: &[IndexMetadata],
446 ) -> DbResult<Prefetch> {
447 unimplemented!()
448 }
449
450 async fn get_cratesio_index_update_list(&self) -> DbResult<Vec<CratesioPrefetchMsg>> {
451 unimplemented!()
452 }
453
454 async fn unyank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()> {
455 unimplemented!()
456 }
457
458 async fn yank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()> {
459 unimplemented!()
460 }
461
462 async fn add_group(&self, name: &str) -> DbResult<()> {
463 unimplemented!()
464 }
465 async fn get_group(&self, name: &str) -> DbResult<Group>{
466 unimplemented!()
467 }
468 async fn get_groups(&self) -> DbResult<Vec<Group>>{
469 unimplemented!()
470 }
471 async fn delete_group(&self, name: &str) -> DbResult<()>{
472 unimplemented!()
473 }
474 async fn add_group_user(&self, group_name: &str, user: &str) -> DbResult<()>{
475 unimplemented!()
476 }
477 async fn delete_group_user(&self, group_name: &str, user: &str) -> DbResult<()>{
478 unimplemented!()
479 }
480 async fn get_group_users(&self, group_name: &str) -> DbResult<Vec<User>> {
481 unimplemented!()
482 }
483 async fn is_group_user(&self, group_name: &str, user: &str) -> DbResult<bool> {
484 unimplemented!()
485 }
486
487 async fn add_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>{
488 unimplemented!()
489 }
490 async fn delete_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>{
491 unimplemented!()
492 }
493 async fn get_crate_groups(&self, crate_name: &NormalizedName) -> DbResult<Vec<Group>>{
494 unimplemented!()
495 }
496 async fn is_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<bool>{
497 unimplemented!()
498 }
499
500 async fn is_crate_group_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>{
501 unimplemented!()
502 }
503
504 async fn register_webhook(
505 &self,
506 webhook: Webhook
507 ) -> DbResult<String> {
508 unimplemented!()
509 }
510 async fn delete_webhook(&self, id: &str) -> DbResult<()> {
511 unimplemented!()
512 }
513 async fn get_webhook(&self, id: &str) -> DbResult<Webhook> {
514 unimplemented!()
515 }
516 async fn get_all_webhooks(&self) -> DbResult<Vec<Webhook>> {
517 unimplemented!()
518 }
519 async fn add_webhook_queue(&self, event: WebhookEvent, payload: serde_json::Value) -> DbResult<()> {
520 unimplemented!()
521 }
522 async fn get_pending_webhook_queue_entries(&self, timestamp: DateTime<Utc>) -> DbResult<Vec<WebhookQueue>> {
523 unimplemented!()
524 }
525 async fn update_webhook_queue(&self, id: &str, last_attempt: DateTime<Utc>, next_attempt: DateTime<Utc>) -> DbResult<()> {
526 unimplemented!()
527 }
528 async fn delete_webhook_queue(&self, id: &str) -> DbResult<()> {
529 unimplemented!()
530 }
531 }
532 }
533}