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
use futures::future::BoxFuture;
use futures::FutureExt;
use std::fmt::{self};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tower::Service;

use crate::context::JobContext;
use crate::error::JobError;
use crate::job::Job;
use crate::request::JobRequest;
use crate::response::{IntoJobResponse, JobResult};

/// Returns a new [`JobFn`] with the given closure.
///
/// This lets you build a [`Job`] from an async function that returns a [`Result`].
///
/// # Example
///
/// ```rust,ignore
/// use apalis_core::{job_fn, Job};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), BoxError> {
/// async fn handle(request: JobRequest) -> Result<JobResult, BoxError> {
///     Ok(JobResult::Ok)
/// }
///
/// let mut job = job_fn(handle);
///
/// let response = job
///     .ready()
///     .await?
///     .call(JobRequest::new())
///     .await?;
///
/// assert_eq!(JobResult::Ok, response);
/// #
/// # Ok(())
/// # }
/// ```
pub fn job_fn<T>(f: T) -> JobFn<T> {
    JobFn { f }
}

/// A [`Job`] implemented by a closure.
///
/// See [`job_fn`] for more details.
#[derive(Copy, Clone)]
pub struct JobFn<T> {
    f: T,
}

impl<T> fmt::Debug for JobFn<T>
where
    T: Job,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JobFn")
            .field("f", &format_args!("{}", T::NAME))
            .finish()
    }
}

pin_project_lite::pin_project! {
    /// The Future returned from [`JobFn`] service.
    pub struct JobFnHttpFuture<F> {
        #[pin]
        future: F,
    }
}

impl<F, Res> Future for JobFnHttpFuture<F>
where
    F: Future<Output = Res> + 'static,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let slf = self.project();
        slf.future.poll(cx)
    }
}

impl<T, F, Res, Request> Service<JobRequest<Request>> for JobFn<T>
where
    Request: 'static,
    T: Fn(Request, JobContext) -> F,
    Res: IntoJobResponse,
    F: Future<Output = Res> + 'static + Send,
    Request: Job,
{
    type Response = JobResult;
    type Error = JobError;
    // TODO: Improve this to remove the send
    type Future = JobFnHttpFuture<BoxFuture<'static, Result<JobResult, JobError>>>;

    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, job: JobRequest<Request>) -> Self::Future {
        let fut = (self.f)(job.job, job.context).map(|res| res.into_response());

        JobFnHttpFuture {
            future: Box::pin(fut),
        }
    }
}