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
use crate::handler::Handler;
use reqwest::{Client, Request};
use tracing::{trace, instrument};
use std::fmt::{self, Debug, Display};
use tokio::sync::mpsc::Receiver;

/// Represents the current calculation state.
///
/// Because this enum provides univeral `From` implementations a caller should never have to
/// directly deal with this enum themselves. This has been left in the documentation largely to
/// help with understanding compilation error messages.
#[derive(Debug)]
pub enum Indeterminate<I: Debug, C> {
    /// A parsed item.
    Item(I),
    /// A callback to be invoked.
    Callback(Callback<I, C>),
}

impl<I: Debug, C> From<Callback<I, C>> for Indeterminate<I, C> {
    fn from(callback: Callback<I, C>) -> Self {
        Self::Callback(callback)
    }
}

impl<I: Debug, C> From<I> for Indeterminate<I, C> {
    fn from(item: I) -> Self {
        Self::Item(item)
    }
}

/// Transforms a `Request` into `Indeterminate`s.
#[derive(Debug)]
pub struct Callback<I, C> {
    request: Request,
    // A Box to provide type eraser for the handler
    handler: Box<dyn Handler<I, C>>,
    context: C,
}

impl<I: Debug, C: Debug> Callback<I, C> {
    /// Construct a new `Callback` to be processed.
    ///
    /// # Arguments
    /// - `handler`: The function to process the generated `Response`.
    /// - `request`: Used to generate a `Response`.
    /// - `context`: User-defined metadata passed to the `handler` function.
    pub fn new<H>(handler: H, request: Request, context: C) -> Self
    where
        H: Handler<I, C> + 'static,
    {
        Self {
            handler: Box::new(handler),
            request,
            context,
        }
    }

    /// Returns the `Request` that will be processed by the callback execution.
    pub fn target(&self) -> &Request {
        &self.request
    }

    /// Execute the callback with the provided client and logger.
    #[instrument]
    pub(crate) async fn run(
        self,
        client: Client,
    ) -> Result<Receiver<Indeterminate<I, C>>, reqwest::Error> {
        trace!(request = ?self.request, "Executing request");
        let resp = client.execute(self.request).await?;
        trace!(response = ?resp, "Got response");
        let result = self.handler.handle(client, resp, self.context);
        Ok(result)
    }
}

impl<I, C> Display for Callback<I, C> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} -> {}", self.handler, self.request.url())
    }
}