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
use crate::events::IbcEvent;
use crate::prelude::*;
use core::marker::PhantomData;

pub type HandlerResult<T, E> = Result<HandlerOutput<T>, E>;

#[derive(Clone, Debug)]
pub struct HandlerOutput<T, Event = IbcEvent> {
    pub result: T,
    pub log: Vec<String>,
    pub events: Vec<Event>,
}

impl<T> HandlerOutput<T> {
    pub fn builder() -> HandlerOutputBuilder<T> {
        HandlerOutputBuilder::new()
    }
}

#[derive(Clone, Debug, Default)]
pub struct HandlerOutputBuilder<T> {
    log: Vec<String>,
    events: Vec<IbcEvent>,
    marker: PhantomData<T>,
}

impl<T> HandlerOutputBuilder<T> {
    pub fn new() -> Self {
        Self {
            log: Vec::new(),
            events: Vec::new(),
            marker: PhantomData,
        }
    }

    pub fn with_log(mut self, log: impl Into<Vec<String>>) -> Self {
        self.log.append(&mut log.into());
        self
    }

    pub fn log(&mut self, log: impl Into<String>) {
        self.log.push(log.into());
    }

    pub fn with_events(mut self, mut events: Vec<IbcEvent>) -> Self {
        self.events.append(&mut events);
        self
    }

    pub fn emit(&mut self, event: IbcEvent) {
        self.events.push(event);
    }

    pub fn with_result(self, result: T) -> HandlerOutput<T> {
        HandlerOutput {
            result,
            log: self.log,
            events: self.events,
        }
    }

    pub fn merge(&mut self, other: HandlerOutput<()>) {
        let HandlerOutput {
            mut log,
            mut events,
            ..
        } = other;
        self.log.append(&mut log);
        self.events.append(&mut events);
    }
}