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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use crate::{encode, Bytes, Error, Param, ParamType, Result, Token};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Constructor {
pub inputs: Vec<Param>,
}
impl Constructor {
fn param_types(&self) -> Vec<ParamType> {
self.inputs.iter().map(|p| p.kind.clone()).collect()
}
pub fn encode_input(&self, code: Bytes, tokens: &[Token]) -> Result<Bytes> {
let params = self.param_types();
if Token::types_check(tokens, ¶ms) {
Ok(code.into_iter().chain(encode(tokens)).collect())
} else {
Err(Error::InvalidData)
}
}
}