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
//! The trait representing the interface that returns home page.

use crate::response::home::GetHomeResponse;
use crate::ResponseError;
use axum::async_trait;

/// The trait representing the interface that returns home page.
#[async_trait]
pub trait HomeHandlerTrait: Send + Sync {
    /// Handles the request to get home page.
    async fn handle_home(&self) -> Result<Vec<u8>, ResponseError>;
}

/// The new type to wrap [`HomeHandlerTrait`].
#[derive(Clone)]
pub(crate) struct HomeHandler<T>(T)
where
    T: HomeHandlerTrait;

impl<T> HomeHandler<T>
where
    T: HomeHandlerTrait,
{
    /// The constructor.
    pub fn new(inner: T) -> Self {
        Self(inner)
    }

    /// Handles the request to get home page.
    pub async fn handle(&self) -> Result<impl axum::response::IntoResponse, ResponseError> {
        tracing::info!("get home: start to process");

        let resp = self.0.handle_home().await?;

        Ok(GetHomeResponse::new(resp))
    }
}