1use crate::ProgressEntry;
2use bytes::Bytes;
3use core::fmt::Debug;
4
5pub trait Pusher: Send + 'static {
6 type Error: Send + Unpin + 'static;
7 fn push(&mut self, range: &ProgressEntry, content: Bytes) -> Result<(), (Self::Error, Bytes)>;
8 fn flush(&mut self) -> Result<(), Self::Error> {
9 Ok(())
10 }
11}
12
13pub trait AnyError: Debug + Send + Unpin + 'static {}
14impl<T: Debug + Send + Unpin + 'static> AnyError for T {}
15
16pub struct BoxPusher {
17 pub pusher: Box<dyn Pusher<Error = Box<dyn AnyError>>>,
18}
19impl Pusher for BoxPusher {
20 type Error = Box<dyn AnyError>;
21 fn push(&mut self, range: &ProgressEntry, content: Bytes) -> Result<(), (Self::Error, Bytes)> {
22 self.pusher.push(range, content)
23 }
24 fn flush(&mut self) -> Result<(), Self::Error> {
25 self.pusher.flush()
26 }
27}
28
29struct PusherAdapter<P: Pusher> {
30 inner: P,
31}
32impl<P: Pusher> Pusher for PusherAdapter<P>
33where
34 P::Error: Debug,
35{
36 type Error = Box<dyn AnyError>;
37 fn push(&mut self, range: &ProgressEntry, content: Bytes) -> Result<(), (Self::Error, Bytes)> {
38 self.inner
39 .push(range, content)
40 .map_err(|(e, b)| (Box::new(e) as Box<dyn AnyError>, b))
41 }
42 fn flush(&mut self) -> Result<(), Self::Error> {
43 self.inner
44 .flush()
45 .map_err(|e| Box::new(e) as Box<dyn AnyError>)
46 }
47}
48
49impl BoxPusher {
50 pub fn new<P: Pusher>(pusher: P) -> Self
51 where
52 P::Error: Debug,
53 {
54 Self {
55 pusher: Box::new(PusherAdapter { inner: pusher }),
56 }
57 }
58}