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
//! Build InitResponse and HandleResponse using expressions.

// note: see issue #35203 <https://github.com/rust-lang/rust/issues/35203>

#![allow(patterns_in_fns_without_body)]

use crate::prelude::*;

pub trait ResponseBuilder: Sized {
    fn msg (mut self, msg: CosmosMsg) -> StdResult<Self>;
    fn msg_to (mut self, contract: ContractLink<HumanAddr>, msg: &impl serde::Serialize)
        -> StdResult<Self>;
    fn log (mut self, key: &str, value: &str) -> StdResult<Self>;
    fn data <T: serde::Serialize> (mut self, data: &T) -> StdResult<Self>;
    fn merge (mut self, mut other: Self) -> StdResult<Self>;
}

impl ResponseBuilder for InitResponse {
    fn msg (mut self, msg: CosmosMsg) -> StdResult<Self> {
        self.messages.push(msg);
        Ok(self)
    }
    fn msg_to (
        mut self, contract: ContractLink<HumanAddr>, msg: &impl serde::Serialize
    ) -> StdResult<Self> {
        let msg = to_cosmos_msg(contract.address, contract.code_hash, msg)?;
        self.messages.push(msg);
        Ok(self)
    }
    fn log (mut self, key: &str, value: &str) -> StdResult<Self> {
        self.log.push(log(key, value));
        Ok(self)
    }
    fn data <T: serde::Serialize> (self, _: &T) -> StdResult<Self> {
        unimplemented!(); // InitResponse does not have data field
    }
    fn merge (mut self, mut other: Self) -> StdResult<Self> {
        self.messages.append(&mut other.messages);
        self.log.append(&mut other.log);
        Ok(self)
    }
}

impl ResponseBuilder for HandleResponse {
    fn msg (mut self, msg: CosmosMsg) -> StdResult<Self> {
        self.messages.push(msg);
        Ok(self)
    }
    fn msg_to (
        mut self, contract: ContractLink<HumanAddr>, msg: &impl serde::Serialize
    ) -> StdResult<Self> {
        let msg = to_cosmos_msg(contract.address, contract.code_hash, msg)?;
        self.messages.push(msg);
        Ok(self)
    }
    fn log (mut self, key: &str, value: &str) -> StdResult<Self> {
        self.log.push(log(key, value));
        Ok(self)
    }
    fn data <T: serde::Serialize> (mut self, data: &T) -> StdResult<Self> {
        self.data = Some(to_binary(data)?);
        Ok(self)
    }
    fn merge (mut self, mut other: Self) -> StdResult<Self> {
        self.messages.append(&mut other.messages);
        self.log.append(&mut other.log);
        Ok(self)
    }
}