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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2015-2017 Intecture Developers. See the COPYRIGHT file at the
// top-level directory of this distribution and at
// https://intecture.io/COPYRIGHT.
//
// Licensed under the Mozilla Public License 2.0 <LICENSE or
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
// modified, or distributed except according to those terms.

macro_rules! want_macro {
    ($t:expr, $n:ident, $isf:ident, $asf:ident) => {
        /// Helper that returns an `Option<Value>`.
        /// You can optionally pass a JSON pointer to retrieve a
        /// nested key.
        #[macro_export]
        macro_rules! $n {
            ($v:expr) => (if $v.$isf() {
                Some($v.$asf().unwrap())
            } else {
                None
            });

            ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
                $n!(v)
            } else {
                None
            });
        }
    }
}

want_macro!("Null", wantnull, is_null, as_null);
want_macro!("Bool", wantbool, is_boolean, as_bool);
want_macro!("I64", wanti64, is_i64, as_i64);
want_macro!("U64", wantu64, is_u64, as_u64);
want_macro!("F64", wantf64, is_f64, as_f64);
want_macro!("String", wantstr, is_string, as_str);
want_macro!("Array", wantarray, is_array, as_array);
want_macro!("Object", wantobj, is_object, as_object);

// XXX We can't auto-generate macros due to the eager expansion of
// `$crate`, which causes any external libs to look for ::error::...
// instead of inapi::error::...
//
// macro_rules! need_macro {
//     ($t:expr, $n:ident, $isf:ident, $asf:ident) => {
//         /// Helper that returns a Result<Value::$t>.
//         /// You can optionally pass a JSON pointer to retrieve a
//         /// nested key.
//         #[macro_export]
//         macro_rules! $n {
//             ($v:expr) => (if $v.$isf() {
//                 Ok($v.$asf().unwrap())
//             } else {
//                 Err($crate::Error::Generic(format!("Value is not $t")))
//             });
//
//             ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
//                 $n!(v)
//             } else {
//                 Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
//             });
//         }
//     }
// }
//
// need_macro!("Null", neednull, is_null, as_null);
// need_macro!("Bool", needbool, is_boolean, as_bool);
// need_macro!("I64", needi64, is_i64, as_i64);
// need_macro!("U64", needu64, is_u64, as_u64);
// need_macro!("F64", needf64, is_f64, as_f64);
// need_macro!("String", needstr, is_string, as_str);
// need_macro!("Array", needarray, is_array, as_array);
// need_macro!("Object", needobj, is_object, as_object);

/// Helper that returns a `Result<Value::Null>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! neednull {
    ($v:expr) => (if $v.is_null() {
        Ok($v.as_null().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not null")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        neednull!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::Bool>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needbool {
    ($v:expr) => (if $v.is_boolean() {
        Ok($v.as_bool().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not boolean")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needbool!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::I64>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needi64 {
    ($v:expr) => (if $v.is_i64() {
        Ok($v.as_i64().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not i64")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needi64!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::U64>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needu64 {
    ($v:expr) => (if $v.is_u64() {
        Ok($v.as_u64().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not u64")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needu64!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::F64>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needf64 {
    ($v:expr) => (if $v.is_f64() {
        Ok($v.as_f64().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not f64")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needf64!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::String>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needstr {
    ($v:expr) => (if $v.is_string() {
        Ok($v.as_str().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not string")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needstr!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::Array>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needarray {
    ($v:expr) => (if $v.is_array() {
        Ok($v.as_array().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not array")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needarray!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}

/// Helper that returns a `Result<Value::Object>`.
/// You can optionally pass a JSON pointer to retrieve a nested key.
#[macro_export]
macro_rules! needobj {
    ($v:expr) => (if $v.is_object() {
        Ok($v.as_object().unwrap())
    } else {
        Err($crate::Error::Generic(format!("Value is not object")))
    });

    ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
        needobj!(v)
    } else {
        Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
    });
}