covert_framework/
lib.rs

1#![forbid(unsafe_code)]
2#![forbid(clippy::unwrap_used)]
3#![deny(clippy::pedantic)]
4#![deny(clippy::get_unwrap)]
5#![allow(clippy::module_name_repetitions)]
6
7pub mod extract;
8mod handler;
9mod method_router;
10mod router;
11mod sync_service;
12
13use std::sync::Arc;
14
15use covert_storage::{
16    migrator::{migrate, MigrationError, MigrationScript},
17    EncryptedPool,
18};
19use tower::ServiceExt;
20
21pub use method_router::*;
22pub use router::Router;
23pub use sync_service::SyncService;
24
25use covert_types::{
26    backend::{BackendCategory, BackendType},
27    error::ApiError,
28    request::Request,
29    response::Response,
30};
31
32#[derive(Debug)]
33pub struct Backend {
34    pub handler: SyncService<Request, Response>,
35    pub category: BackendCategory,
36    pub variant: BackendType,
37    pub migrations: Vec<MigrationScript>,
38}
39
40impl Backend {
41    /// Call the backend with a [`Request`].
42    ///
43    /// # Errors
44    ///
45    /// Returns error if the backend fails to handle the [`Request`].
46    pub async fn handle_request(&self, req: Request) -> Result<Response, ApiError> {
47        let handler = self.handler.clone();
48        handler.oneshot(req).await
49    }
50
51    #[must_use]
52    pub fn category(&self) -> BackendCategory {
53        self.category
54    }
55
56    #[must_use]
57    pub fn variant(&self) -> BackendType {
58        self.variant
59    }
60
61    /// Run migration for backend.
62    ///
63    /// # Errors
64    ///
65    /// Returns error if migration fails.
66    #[tracing::instrument(skip(self, pool))]
67    pub async fn migrate(
68        &self,
69        pool: Arc<EncryptedPool>,
70        mount_id: &str,
71        prefix: &str,
72    ) -> Result<(), MigrationError> {
73        migrate(pool.as_ref(), &self.migrations, mount_id, prefix).await
74    }
75}