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
/// Create a json-like 'object':
/// A map of string keys to json values
///
/// `unreact::Object` is a type alias for `serde_json::Map<String, serde_json::Value>`
///
/// Similar to `serde_json::json!` macro, but must be an object
///
/// # Examples
///
/// ```
/// # use unreact::object;
/// let my_key = "Hello!";
/// object! {
///     foo: 123,
///     bar: vec![4, 5, 6],
///     // Use variable with same name as key
///     my_key,
///     // Nested objects must also use `object!` macro
///     nested: object! {
///         key: "value"
///     }
/// };
/// ```
///
/// The above code is equivalent to this json:
///
/// ```json
/// {
///     "foo": 123,
///     "bar": [4, 5, 6],
///     "my_key": "Hello!",
///     "nested": {
///         "key": "value"
///     }
/// }
/// ```
#[macro_export]
macro_rules! object {
    // Empty object
    {} => { $crate::Object::new() };

    // Object
    {
        $( $key: ident $(: $value: expr)? ),* $(,)?
    } => {{
        let mut hm = $crate::Object::new();
        $(
            object!(@entry hm, $key $(: $value)?);
        )*
        hm
    }};

    // Key, no value
    (@entry $hm: expr,
        $key: ident
    ) => {
        $hm.insert(String::from(stringify!($key)), $crate::json!($key));
    };

    // Key and value
    (@entry $hm: expr,
        $key: ident : $value: expr
    ) => {
        $hm.insert(String::from(stringify!($key)), $crate::json!($value));
    };
}

/// Private macro
///
/// Try to unwrap a `Result`, returns value in `Ok` variant
///
/// If result is `Err`, then run code block
/// Similar to a `let else` statement, but captures the value inside the `Err` variant
macro_rules! try_unwrap {
    (
        $option: expr,
        else Err($err: ident) => $block: block
        $(,)?
    ) => {
        match $option {
            Ok(value) => value,
            Err($err) => $block,
        }
    };

    (
        $option: expr,
        else Err($err: ident) => $stmt: stmt
        $(,)?
    ) => {
        match $option {
            Ok(value) => value,
            // Brackets cannot be removed
            #[rustfmt::skip] Err($err) => { $stmt },
        }
    };
}

/// Private macro
///
/// Shorthand for `Err(crate::Error...)`
macro_rules! fail {
    ( $kind: ident ) => {
        Err($crate::Error::$kind)
    };
    ( $kind: ident, $( $arg: expr ),* ) => {
        Err($crate::Error::$kind( $( $arg ),* ))
    };
}

/// Private macro
///
/// Shorthand for `Err(crate::Error::IoFail(crate::IoError...))`
macro_rules! io_fail {
    ( $kind: ident ) => {
        Err($crate::Error::IoFail($crate::IoError::$kind))
    };
    ( $kind: ident, $( $arg: expr ),* ) => {
        Err($crate::Error::IoFail($crate::IoError::$kind( $( $arg ),* )))
    };
}

#[cfg(test)]
mod tests {
    use crate::{Object, Value};

    #[test]
    fn object_macro_works() {
        let my_key = "hello!";

        let mut obj = Object::new();
        obj.insert("abc".to_string(), Value::from(123));
        obj.insert("array".to_string(), Value::from(vec![4, 5, 6]));
        obj.insert("my_key".to_string(), Value::from(my_key));

        assert_eq!(
            object! {
                abc: 123,
                array: Value::from(vec![4, 5, 6]),
                my_key,
            },
            obj
        );

        let mut obj = Object::new();
        obj.insert("abc".to_string(), Value::from("abcdef"));

        let mut obj2 = Object::new();
        obj2.insert("ghi".to_string(), Value::from(456));

        obj.insert("def".to_string(), Value::Object(obj2));

        assert_eq!(
            object! {
                abc: Value::from("abcdef"),
                def: Value::from(object!{
                    ghi: 456,
                }),
            },
            obj
        );
    }

    #[test]
    fn try_unwrap_works() {
        let result: Result<i32, &str> = Ok(123);
        let value = try_unwrap!( result,
            else Err(_err) => {
                panic!("Should not be Err")
            }
        );
        assert_eq!(value, 123);

        let result: Result<i32, &str> = Err("oh no!");
        let value = try_unwrap!( result,
            else Err(_err) => {
                456
            }
        );
        assert_eq!(value, 456);

        let result: Result<i32, &str> = Err("oh no!");
        let _value = try_unwrap!( result,
            else Err(_err) => {
                return;
            }
        );
        panic!("Should not have been Ok");
    }
}