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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Provides a helper macro for extracting arguments from Ketos values.

/// Parses a set of arguments from a `&[Value]`.
///
/// ```ignore
/// // All arguments are required
/// let (name, number) = ketos_args!(args, (&str, i32));
///
/// // `number` is optional; its type will be `Option<i32>`
/// let (name, number) = ketos_args!(args, (&str [ i32 ]));
///
/// // All arguments are optional; their types will be `Option<T>`
/// let (name, number) = ketos_args!(args, [ &str, i32 ]);
/// ```
#[macro_export]
macro_rules! ketos_args {
    // All required arguments
    ( $args:expr , ( $( $ty:ty ),* ) ) => { {
        let args: &[$crate::value::Value] = $args;
        let n_args = args.len();
        let expected = 0 $( + { stringify!($ty); 1 } )*;

        if n_args != expected {
            return Err(From::from($crate::exec::ExecError::ArityError{
                name: None,
                expected: $crate::function::Arity::Exact(expected as u32),
                found: n_args as u32,
            }));
        }

        let mut _iter = args.iter();

        ( $( {
            let arg = _iter.next().unwrap();
            try!(<$ty as $crate::value::FromValueRef>::from_value_ref(arg))
        } , )* )
    } };
    // Optional arguments
    ( $args:expr , [ $( $ty:ty ),* ] ) => { {
        let args: &[$crate::value::Value] = $args;
        let n_args = args.len();
        let max = 0 $( + { stringify!($ty); 1 })*;

        if n_args > max {
            return Err(From::from($crate::exec::ExecError::ArityError{
                name: None,
                expected: $crate::function::Arity::Range(0, max as u32),
                found: n_args as u32,
            }));
        }

        let mut _iter = args.iter();

        ( $( {
            match _iter.next() {
                Some(arg) => Some(try!(<$ty as
                    $crate::value::FromValueRef>::from_value_ref(arg))),
                None => None
            }
        } , )* )
    } };
    // Some required arguments; some optional
    ( $args:expr , ( $( $r_ty:ty ),* [ $( $o_ty:ty ),* ] ) ) => { {
        let args: &[$crate::value::Value] = $args;
        let n_args = args.len();
        let min = 0 $( + { stringify!($r_ty); 1 })*;
        let optional = 0 $( + { stringify!($o_ty); 1 })*;
        let max = min + optional;

        if n_args < min || n_args > max {
            return Err(From::from($crate::exec::ExecError::ArityError{
                name: None,
                expected: $crate::function::Arity::Range(
                    min as u32, max as u32),
                found: n_args as u32,
            }));
        }

        let mut _iter = args.iter();

        ( $( {
            let arg = _iter.next().unwrap();
            try!(<$r_ty as $crate::value::FromValueRef>::from_value_ref(arg))
        } , )*
        $( {
            match _iter.next() {
                Some(arg) => Some(try!(<$o_ty as
                    $crate::value::FromValueRef>::from_value_ref(arg))),
                None => None
            }
        } , )* )
    } };
}

#[cfg(test)]
mod test {
    use error::Error;

    #[test]
    fn test_args() {
        let args = [1.into(), "sup".into(), (2, 'a').into()];

        let f = || -> Result<(), Error> {
            let (a, b, (c, d)) = ketos_args!(&args, (i32, &str, (i32, char)));

            assert_eq!(a, 1);
            assert_eq!(b, "sup");
            assert_eq!(c, 2);
            assert_eq!(d, 'a');

            Ok(())
        };

        f().unwrap();
    }

    #[test]
    fn test_empty_args() {
        let args = [];

        let f = || -> Result<(), Error> {
            let () = ketos_args!(&args, ());
            ketos_args!(&args, ());

            Ok(())
        };

        f().unwrap();
    }

    #[test]
    fn test_one_arg() {
        let args = [1.into()];

        let f = || -> Result<(), Error> {
            let (a,) = ketos_args!(&args, (i32));
            assert_eq!(a, 1);

            let (a,) = ketos_args!(&args, [ i32 ]);
            assert_eq!(a, Some(1));

            Ok(())
        };

        f().unwrap();
    }

    #[test]
    fn test_optional_args() {
        let args = [1.into(), 2.into(), 3.into()];

        let f = || -> Result<(), Error> {
            let (a, b, c, d) = ketos_args!(&args, (i32, i32 [ i32, i32 ]));

            assert_eq!(a, 1);
            assert_eq!(b, 2);
            assert_eq!(c, Some(3));
            assert_eq!(d, None);

            let (a, b, c, d) = ketos_args!(&args, [ i32, i32, i32, i32 ]);

            assert_eq!(a, Some(1));
            assert_eq!(b, Some(2));
            assert_eq!(c, Some(3));
            assert_eq!(d, None);

            Ok(())
        };

        f().unwrap();
    }
}