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
//! Module containing crate macros.

/// Given an int, creates and returns a `BigInt`.
#[macro_export]
macro_rules! int {
    ($int:expr) => {{
        use num_bigint::BigInt;

        let _b: BigInt = $int.into();
        _b
    }};
}

/// Given two ints, creates and returns a `BigRational`.
#[macro_export]
macro_rules! frac {
    ($int1:expr, $int2:expr) => {{
        ::num_rational::BigRational::new($int1.into(), $int2.into())
    }};
}

/// Given a list of elements, converts each element to a `Value` and returns an `Arr` containing a
/// vector of the values. For a non-panicking version, see `try_arr!`.
///
/// # Panics
/// Panics if the types don't check out.
#[macro_export]
macro_rules! arr {
    [] => {
        $crate::arr::Arr::from_vec(vec![]).unwrap()
    };
    [ $( $elem:expr ),+ , ] => {
        // Rule with trailing comma.
        try_arr![ $( $elem ),+ ].unwrap()
    };
    [ $( $elem:expr ),+ ] => {
        try_arr![ $( $elem ),+ ].unwrap()
    };
}

/// Given a list of elements, converts each element to a `Value` and returns an `Arr` containing a
/// vector of the values. Returns an `OverResult` instead of panicking on error. To create an empty
/// `Arr`, use `arr!` as it will never fail.
#[macro_export]
macro_rules! try_arr {
    [ $( $elem:expr ),+ , ] => {
        // Rule with trailing comma.
        try_arr![ $( $elem ),+ ]
    };
    [ $( $elem:expr ),+ ] => {
        {
            $crate::arr::Arr::from_vec(vec![ $( $elem.into() ),+ ])
        }
    };
}

/// Given a list of elements, converts each element to `Value`s and returns a `Tup` containing a
/// vector of the values.
#[macro_export]
macro_rules! tup {
    ( $( $elem:expr ),* , ) => {
        tup!( $( $elem ),* )
    };
    ( $( $elem:expr ),* ) => {
        {
            $crate::tup::Tup::from_vec(vec![ $( $elem.into() ),+ ])
        }
    };
}

/// Given a list of field/value pairs, returns an `Obj` containing each pair.
/// For a non-panicking version, see `try_obj!`.
///
/// # Panics
/// Panics if a field name is invalid.
#[macro_export]
macro_rules! obj {
    {} => {
        $crate::obj::Obj::from_map_unchecked(::std::collections::HashMap::new())
    };
    { $( $field:expr => $inner:expr ),+ , } => {
        // Rule with trailing comma.
        try_obj!{ $( $field => $inner ),+ }.unwrap()
    };
    { $( $field:expr => $inner:expr ),+ } => {
        try_obj!{ $( $field => $inner ),+ }.unwrap()
    };
}

/// Given a list of field to `Value` pairs, returns an `Obj` with the fields and values.
/// Returns an `OverResult` instead of panicking on error. To create an empty `Obj`, use `obj!` as
/// it will never fail.
#[macro_export]
macro_rules! try_obj {
    { $( $field:expr => $inner:expr ),+ , } => {
        // Rule with trailing comma.
        try_obj!{ $( $field => $inner ),* };
    };
    { $( $field:expr => $inner:expr ),+ } => {
        #[allow(clippy::useless_let_if_seq)]
        {
            use $crate::obj::Obj;

            let mut _map = ::std::collections::HashMap::new();
            let mut _parent: Option<$crate::value::Value> = None;

            $(
                if $field == "^" {
                    _parent = Some($inner.into());
                } else {
                    _map.insert($field.into(), $inner.into());
                }
            )*

            match _parent {
                Some(parent) => match parent.get_obj() {
                    Ok(parent) => Obj::from_map_with_parent(_map, parent),
                    e @ Err(_) => e,
                }
                None => Obj::from_map(_map),
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use crate::obj::Obj;
    use crate::types::Type::*;
    use crate::value::Value;
    use crate::OverError;

    #[test]
    fn arr_basic() {
        assert_eq!(
            arr![Value::Int(1.into()), Value::Int(2.into())],
            try_arr![1, 2].unwrap()
        );

        assert_ne!(
            arr![-1, 2],
            try_arr![Value::Int(1.into()), Value::Int(2.into())].unwrap()
        );
    }

    #[test]
    fn try_arr_mismatch() {
        assert_eq!(
            try_arr![arr![1, 1], arr!['c']],
            Err(OverError::ArrTypeMismatch(
                Arr(Box::new(Int)),
                Arr(Box::new(Char)),
            ))
        );
        assert_eq!(try_arr![1, 'c'], Err(OverError::ArrTypeMismatch(Int, Char)));
    }

    #[test]
    fn obj_basic() {
        let obj = Obj::from_map_unchecked(map! {"a".into() => 1.into(),
        "b".into() => arr![1, 2].into()});

        assert_eq!(
            obj,
            obj! {
                "a" => 1,
                "b" => arr![1, 2]
            }
        );
    }
}