1use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp};
20
21#[derive(Debug, PartialEq, Clone)]
23pub enum ActionType {
24	Create,
26	Call,
28	CallCode,
30	DelegateCall,
32	StaticCall,
34	Create2
36}
37
38impl Encodable for ActionType {
39	fn rlp_append(&self, s: &mut RlpStream) {
40		let v = match *self {
41			ActionType::Create => 0u32,
42			ActionType::Call => 1,
43			ActionType::CallCode => 2,
44			ActionType::DelegateCall => 3,
45			ActionType::StaticCall => 4,
46			ActionType::Create2 => 5,
47		};
48		Encodable::rlp_append(&v, s);
49	}
50}
51
52impl Decodable for ActionType {
53	fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
54		rlp.as_val().and_then(|v| Ok(match v {
55			0u32 => ActionType::Create,
56			1 => ActionType::Call,
57			2 => ActionType::CallCode,
58			3 => ActionType::DelegateCall,
59			4 => ActionType::StaticCall,
60			5 => ActionType::Create2,
61			_ => return Err(DecoderError::Custom("Invalid value of ActionType item")),
62		}))
63	}
64}
65
66#[cfg(test)]
67mod tests {
68	use rlp::*;
69	use super::ActionType;
70
71	#[test]
72	fn encode_call_type() {
73		let ct = ActionType::Call;
74
75		let mut s = RlpStream::new_list(2);
76		s.append(&ct);
77		assert!(!s.is_finished(), "List shouldn't finished yet");
78		s.append(&ct);
79		assert!(s.is_finished(), "List should be finished now");
80		s.out();
81	}
82
83	#[test]
84	fn should_encode_and_decode_call_type() {
85		let original = ActionType::Call;
86		let encoded = encode(&original);
87		let decoded = decode(&encoded).expect("failure decoding ActionType");
88		assert_eq!(original, decoded);
89	}
90}