sheave_core/handlers/
map_err.rs

1use std::{
2    io::{
3        Error as IOError,
4        Result as IOResult
5    },
6    pin::Pin,
7    task::{
8        Context as FutureContext,
9        Poll
10    }
11};
12use pin_project_lite::pin_project;
13use futures::ready;
14use super::{
15    AsyncHandler,
16    RtmpContext
17};
18
19pub trait ErrorHandler {
20    fn poll_handle_error(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext, error: IOError) -> Poll<IOResult<()>>;
21}
22
23pin_project! {
24    #[doc(hidden)]
25    #[derive(Debug)]
26    pub struct MapErr<H, E> {
27        #[pin] body: H,
28        #[pin] error_handler: E
29    }
30}
31
32#[doc(hidden)]
33impl<H, E> AsyncHandler for MapErr<H, E>
34where
35    H: AsyncHandler + Unpin,
36    E: ErrorHandler + Unpin
37{
38    fn poll_handle(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext) -> Poll<IOResult<()>> {
39        let this = self.project();
40        if let Err(e) = ready!(this.body.poll_handle(cx, rtmp_context)) {
41            this.error_handler.poll_handle_error(cx, rtmp_context, e)
42        } else {
43            Poll::Ready(Ok(()))
44        }
45    }
46}
47
48pub fn map_err<H, E>(body: H, error_handler: E) -> MapErr<H, E>
49where
50    H: AsyncHandler + Unpin,
51    E: ErrorHandler + Unpin
52{
53    MapErr { body, error_handler }
54}