ibc_relayer_types/
handler.rs

1use crate::events::IbcEvent;
2
3use std::marker::PhantomData;
4
5pub type HandlerResult<T, E> = Result<HandlerOutput<T>, E>;
6
7#[derive(Clone, Debug)]
8pub struct HandlerOutput<T, Event = IbcEvent> {
9    pub result: T,
10    pub log: Vec<String>,
11    pub events: Vec<Event>,
12}
13
14impl<T, E> HandlerOutput<T, E> {
15    pub fn builder() -> HandlerOutputBuilder<T, E> {
16        HandlerOutputBuilder::new()
17    }
18}
19
20#[derive(Clone, Debug, Default)]
21pub struct HandlerOutputBuilder<T, E = IbcEvent> {
22    log: Vec<String>,
23    events: Vec<E>,
24    marker: PhantomData<T>,
25}
26
27impl<T, E> HandlerOutputBuilder<T, E> {
28    pub fn new() -> Self {
29        Self {
30            log: Vec::new(),
31            events: Vec::new(),
32            marker: PhantomData,
33        }
34    }
35
36    pub fn with_log(mut self, log: impl Into<Vec<String>>) -> Self {
37        self.log.append(&mut log.into());
38        self
39    }
40
41    pub fn log(&mut self, log: impl Into<String>) {
42        self.log.push(log.into());
43    }
44
45    pub fn with_events(mut self, mut events: Vec<E>) -> Self {
46        self.events.append(&mut events);
47        self
48    }
49
50    pub fn emit(&mut self, event: E) {
51        self.events.push(event);
52    }
53
54    pub fn with_result(self, result: T) -> HandlerOutput<T, E> {
55        HandlerOutput {
56            result,
57            log: self.log,
58            events: self.events,
59        }
60    }
61
62    pub fn merge<Event: Into<E>>(&mut self, other: HandlerOutputBuilder<(), Event>) {
63        let HandlerOutputBuilder {
64            mut log, events, ..
65        } = other;
66        self.log.append(&mut log);
67        self.events
68            .append(&mut events.into_iter().map(Into::into).collect());
69    }
70
71    pub fn merge_output<Event: Into<E>>(&mut self, other: HandlerOutput<(), Event>) {
72        let HandlerOutput {
73            mut log, events, ..
74        } = other;
75        self.log.append(&mut log);
76        self.events
77            .append(&mut events.into_iter().map(Into::into).collect());
78    }
79}