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
use std::{future::Future, marker::PhantomData, sync::Arc};

use crate::{
    core::{
        context::Context,
        convert::TryFromInput,
        handler::{Handler, HandlerInput, IntoHandlerResult},
    },
    handler::UpdateHandler,
    types::Update,
};

#[cfg(test)]
mod tests;

/// The main entry point.
///
/// Implements the [`UpdateHandler`] trait, so you can use it
/// in [`crate::handler::LongPoll`] or [crate::handler::WebhookServer].
///
/// Wraps an update into the [`HandlerInput`] struct and passes it to the inner handler.
///
/// Use [`crate::Chain`] struct to configure multiple handlers.
#[derive(Clone)]
pub struct App<H, HI> {
    context: Arc<Context>,
    handler: H,
    handler_input: PhantomData<HI>,
}

impl<H, HI, HO> App<H, HI>
where
    H: Handler<HI, Output = HO>,
    HI: TryFromInput,
    HI::Error: 'static,
    HO: IntoHandlerResult,
{
    /// Creates a new `App`.
    ///
    /// # Arguments
    ///
    /// * `context` - A context responsible for storing shared state.
    /// * `handler` - A handler responsible for processing updates.
    pub fn new(context: Context, handler: H) -> Self {
        Self {
            context: Arc::new(context),
            handler,
            handler_input: PhantomData,
        }
    }

    fn handle_update(&self, update: Update) -> impl Future<Output = ()> {
        let input = HandlerInput {
            update,
            context: self.context.clone(),
        };
        let handler = self.handler.clone();
        async move {
            let input = match HI::try_from_input(input).await {
                Ok(Some(input)) => input,
                Ok(None) => return,
                Err(err) => {
                    log::error!("Failed to convert input: {}", err);
                    return;
                }
            };
            let future = handler.handle(input);
            if let Err(err) = future.await.into_result() {
                log::error!("An error has occurred: {}", err);
            }
        }
    }
}

impl<H, HI, HO> UpdateHandler for App<H, HI>
where
    H: Handler<HI, Output = HO> + Sync + 'static,
    HI: TryFromInput + Sync + 'static,
    HI::Error: 'static,
    HO: IntoHandlerResult + Send + 'static,
{
    async fn handle(&self, update: Update) {
        self.handle_update(update).await
    }
}