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
use crate::{
    dep_encode_from_no_err, dep_encode_num_mimic, top_encode_from_no_err, DecodeError,
    DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode,
    NestedEncodeNoErr, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeNoErr,
    TopEncodeOutput, TypeInfo,
};

impl TopEncodeNoErr for bool {
    fn top_encode_no_err<O: TopEncodeOutput>(&self, output: O) {
        // only using signed because this one is implemented in Arwen, unsigned is not
        // TODO: change to set_u64
        output.set_i64(if *self { 1i64 } else { 0i64 });
    }
}

top_encode_from_no_err! {bool, TypeInfo::Bool}

impl TopDecode for bool {
    const TYPE_INFO: TypeInfo = TypeInfo::Bool;

    fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
    where
        I: TopDecodeInput,
        H: DecodeErrorHandler,
    {
        match input.into_u64(h)? {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(h.handle_error(DecodeError::INPUT_OUT_OF_RANGE)),
        }
    }
}

dep_encode_num_mimic! {bool, u8, TypeInfo::Bool}

impl NestedDecode for bool {
    const TYPE_INFO: TypeInfo = TypeInfo::Bool;

    fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
    where
        I: NestedDecodeInput,
        H: DecodeErrorHandler,
    {
        match input.read_byte(h)? {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(h.handle_error(DecodeError::INVALID_VALUE)),
        }
    }
}

#[cfg(test)]
pub mod tests {
    use crate::test_util::{check_dep_encode_decode, check_top_encode_decode};

    #[test]
    fn test_top() {
        check_top_encode_decode(true, &[1]);
        check_top_encode_decode(false, &[]);
    }
    #[test]
    fn test_dep() {
        check_dep_encode_decode(true, &[1]);
        check_dep_encode_decode(false, &[0]);
    }
}