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
//! Home of [`ArgsParser`], a trait used for parsing contract arguments from n-ary tuples.

// Can be removed once https://github.com/rust-lang/rustfmt/issues/3362 is resolved.
#[rustfmt::skip]
use alloc::vec;
use alloc::vec::Vec;

use casperlabs_types::{bytesrepr::ToBytes, CLTyped, CLValue, CLValueError};

/// Types which implement [`ArgsParser`] can be parsed into an ABI-compliant byte representation
/// suitable for passing as arguments to a contract.
///
/// It is primarily implemented for n-ary tuples of values which themselves implement [`ToBytes`]
/// and [`CLTyped`].
pub trait ArgsParser {
    /// Parses the arguments to a `Vec` of [`CLValue`]s.
    fn parse(self) -> Result<Vec<CLValue>, CLValueError>;
}

impl ArgsParser for () {
    fn parse(self) -> Result<Vec<CLValue>, CLValueError> {
        Ok(Vec::new())
    }
}

macro_rules! impl_argsparser_tuple {
    ( $($name:ident)+) => (
        impl<$($name: CLTyped + ToBytes),*> ArgsParser for ($($name,)*) {
            #[allow(non_snake_case)]
            fn parse(self) -> Result<Vec<CLValue>, CLValueError> {
                let ($($name,)+) = self;
                Ok(vec![$(CLValue::from_t($name)?,)+])
            }
        }
    );
}

impl_argsparser_tuple! { T1 }
impl_argsparser_tuple! { T1 T2 }
impl_argsparser_tuple! { T1 T2 T3 }
impl_argsparser_tuple! { T1 T2 T3 T4 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 }
impl_argsparser_tuple! { T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 }