ethabi_decode/
param.rs

1// Copyright 2015-2020 Parity Technologies
2// Copyright 2020 Snowfork
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0 <LICENSE or
7// http://www.apache.org/licenses/LICENSE-2.0>. This file may not be 
8// copied, modified, or distributed except according to those terms.
9
10use crate::std::{Box, Vec};
11
12
13/// Event param specification.
14#[derive(Debug, Clone, PartialEq)]
15pub struct Param {
16	/// Param type.
17	pub kind: ParamKind,
18	/// Indexed flag. If true, param is used to build block bloom.
19	pub indexed: bool,
20}
21
22/// Function and event param types.
23#[derive(Debug, Clone, PartialEq)]
24pub enum ParamKind {
25	/// Address.
26	Address,
27	/// Bytes.
28	Bytes,
29	/// Signed integer.
30	Int(usize),
31	/// Unsigned integer.
32	Uint(usize),
33	/// Boolean.
34	Bool,
35	/// String.
36	String,
37	/// Array of unknown size.
38	Array(Box<ParamKind>),
39	/// Vector of bytes with fixed size.
40	FixedBytes(usize),
41	/// Array with fixed size.
42	FixedArray(Box<ParamKind>, usize),
43	/// Tuple containing different types
44	Tuple(Vec<Box<ParamKind>>),
45}
46
47impl ParamKind {
48	/// returns whether a zero length byte slice (`0x`) is
49	/// a valid encoded form of this param type
50	pub fn is_empty_bytes_valid_encoding(&self) -> bool {
51		match self {
52			ParamKind::FixedBytes(len) => *len == 0,
53			ParamKind::FixedArray(_, len) => *len == 0,
54			_ => false,
55		}
56	}
57
58	/// returns whether a ParamKind is dynamic
59	/// used to decide how the ParamKind should be encoded
60	pub fn is_dynamic(&self) -> bool {
61		match self {
62			ParamKind::Bytes | ParamKind::String | ParamKind::Array(_) => true,
63			ParamKind::FixedArray(elem_type, _) => elem_type.is_dynamic(),
64			ParamKind::Tuple(params) => params.iter().any(|param| param.is_dynamic()),
65			_ => false,
66		}
67	}
68}
69
70#[cfg(test)]
71mod tests {
72	use crate::ParamKind;
73
74	#[test]
75	fn test_is_dynamic() {
76		assert_eq!(ParamKind::Address.is_dynamic(), false);
77		assert_eq!(ParamKind::Bytes.is_dynamic(), true);
78		assert_eq!(ParamKind::FixedBytes(32).is_dynamic(), false);
79		assert_eq!(ParamKind::Uint(256).is_dynamic(), false);
80		assert_eq!(ParamKind::Int(64).is_dynamic(), false);
81		assert_eq!(ParamKind::Bool.is_dynamic(), false);
82		assert_eq!(ParamKind::String.is_dynamic(), true);
83		assert_eq!(ParamKind::Array(Box::new(ParamKind::Bool)).is_dynamic(), true);
84		assert_eq!(ParamKind::FixedArray(Box::new(ParamKind::Uint(256)), 2).is_dynamic(), false);
85		assert_eq!(ParamKind::FixedArray(Box::new(ParamKind::String), 2).is_dynamic(), true);
86		assert_eq!(ParamKind::FixedArray(Box::new(ParamKind::Array(Box::new(ParamKind::Bool))), 2).is_dynamic(), true);
87	}
88}