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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/// Returns a reference to the typed [`Annotated`] value at a given path.
///
/// The return type depends on the path expression used. By default, this macro will resolve to an
/// `Option<&Annotated<T>>`, where the option is `Some` if the path exists. Note that if the
/// annotated value at the specificed path is empty, this returns `Some` with an empty annotated.
///
/// When used with an exclamation mark after the path, this macro unwraps to an `&Annotated<T>`.
///
/// [`Annotated`]: crate::Annotated
///
/// # Syntax
///
/// A path starts with the name of a variable holding an [`Annotated`]. Access to children of this
/// type depend on the value type:
///  - To access a struct field, use a period followed by the field's name, for instance (`.field`).
///  - To access an array element, use the element's numeric index in brackets, for instance `[0]`.
///  - To access an object's element, use the element's quoted string key in brackets, for instance
///    `["key_name"]`.
///
/// Paths can be chained, so a valid path expression is `data.values["key"].field`.
///
/// To unwrap the annotated field at the destination, append an exclamation mark at the end of the
/// path, for instance: `data.field!`.
///
/// # Panics
///
/// Panics when unwrap (`!`) is used and there is an empty field along the path. Since `get_path!`
/// always returns the final `Annotated`, the final field can be empty without panicking.
///
/// # Example
///
/// ```
/// use relay_protocol::{get_path, Annotated, Object};
///
/// struct Inner {
///     value: Annotated<u64>,
/// }
///
/// struct Outer {
///     inners: Annotated<Object<Inner>>,
/// }
///
/// let outer = Annotated::new(Outer {
///     inners: Annotated::new(Object::from([(
///         "key".to_string(),
///         Annotated::new(Inner {
///             value: Annotated::new(1),
///         }),
///     )])),
/// });
///
/// assert_eq!(get_path!(outer.inners["key"].value), Some(&Annotated::new(1)));
/// assert_eq!(get_path!(outer.inners["key"].value!), &Annotated::new(1));
/// ```
#[macro_export]
macro_rules! get_path {
    (@access $root:ident,) => {};
    (@access $root:ident, !) => {
        let $root = $root.unwrap();
    };
    (@access $root:ident, . $field:ident $( $tail:tt )*) => {
        let $root = $root.and_then(|a| a.value()).map(|v| &v.$field);
        get_path!(@access $root, $($tail)*);
    };
    (@access $root:ident, [ $index:literal ] $( $tail:tt )*) => {
        let $root = $root.and_then(|a| a.value()).and_then(|v| v.get($index));
        get_path!(@access $root, $($tail)*);
    };
    ($root:ident $( $tail:tt )*) => {{
        let $root = Some(&$root);
        $crate::get_path!(@access $root, $($tail)*);
        $root
    }};
}

/// Returns a reference to the typed value at a given path in an [`Annotated`].
///
/// The return type depends on the path expression used. By default, this macro will resolve to an
/// `Option<&T>`, where the option is `Some` if the path exists and the value is present.
///
/// When used with an exclamation mark after the path, this macro unwraps to an `&T`.
///
/// [`Annotated`]: crate::Annotated
///
/// # Syntax
///
/// A path starts with the name of a variable holding an [`Annotated`]. Access to children of this
/// type depend on the value type:
///  - To access a struct field, use a period followed by the field's name, for instance (`.field`).
///  - To access an array element, use the element's numeric index in brackets, for instance `[0]`.
///  - To access an object's element, use the element's quoted string key in brackets, for instance
///    `["key_name"]`.
///
/// Paths can be chained, so a valid path expression is `data.values["key"].field`.
///
/// To unwrap the value at the destination, append an exclamation mark at the end of the path, for
/// instance: `data.field!`.
///
/// # Panics
///
/// Panics when unwrap (`!`) is used and there is an empty field along the path.
///
/// # Example
///
/// ```
/// use relay_protocol::{get_value, Annotated, Object};
///
/// struct Inner {
///     value: Annotated<u64>,
/// }
///
/// struct Outer {
///     inners: Annotated<Object<Inner>>,
/// }
///
/// let outer = Annotated::new(Outer {
///     inners: Annotated::new(Object::from([(
///         "key".to_string(),
///         Annotated::new(Inner {
///             value: Annotated::new(1),
///         }),
///     )])),
/// });
///
/// assert_eq!(get_value!(outer.inners["key"].value), Some(&1));
/// assert_eq!(get_value!(outer.inners["key"].value!), &1);
/// ```
#[macro_export]
macro_rules! get_value {
    (@access $root:ident,) => {};
    (@access $root:ident, !) => {
        let $root = $root.unwrap();
    };
    (@access $root:ident, . $field:ident $( $tail:tt )*) => {
        let $root = $root.and_then(|v| v.$field.value());
        get_value!(@access $root, $($tail)*);
    };
    (@access $root:ident, [ $index:literal ] $( $tail:tt )*) => {
        let $root = $root.and_then(|v| v.get($index)).and_then(|a| a.value());
        get_value!(@access $root, $($tail)*);
    };
    ($root:ident $( $tail:tt )*) => {{
        let $root = $root.value();
        $crate::get_value!(@access $root, $($tail)*);
        $root
    }};
}

/// Derives the [`FromValue`], [`IntoValue`], and [`Empty`] traits using the string representation.
///
/// Requires that this type implements `FromStr` and `Display`. Implements the following traits:
///
///  - [`FromValue`]: Deserializes a string, then uses [`FromStr`](std::str::FromStr) to convert
///        into the type.
///  - [`IntoValue`]: Serializes into a string using the [`Display`](std::fmt::Display) trait.
///  - [`Empty`]: This type is never empty.
///
/// [`FromValue`]: crate::FromValue
/// [`IntoValue`]: crate::IntoValue
/// [`Empty`]: crate::Empty
#[macro_export]
macro_rules! derive_string_meta_structure {
    ($type:ident, $expectation:expr) => {
        impl $crate::FromValue for $type {
            fn from_value(value: Annotated<Value>) -> Annotated<Self> {
                match value {
                    Annotated(Some(Value::String(value)), mut meta) => match value.parse() {
                        Ok(value) => Annotated(Some(value), meta),
                        Err(err) => {
                            meta.add_error($crate::Error::invalid(err));
                            meta.set_original_value(Some(value));
                            Annotated(None, meta)
                        }
                    },
                    Annotated(None, meta) => Annotated(None, meta),
                    Annotated(Some(value), mut meta) => {
                        meta.add_error($crate::Error::expected($expectation));
                        meta.set_original_value(Some(value));
                        Annotated(None, meta)
                    }
                }
            }
        }

        impl $crate::IntoValue for $type {
            fn into_value(self) -> Value {
                Value::String(self.to_string())
            }

            fn serialize_payload<S>(
                &self,
                s: S,
                _behavior: $crate::SkipSerialization,
            ) -> Result<S::Ok, S::Error>
            where
                Self: Sized,
                S: serde::ser::Serializer,
            {
                s.collect_str(self)
            }
        }

        impl $crate::Empty for $type {
            fn is_empty(&self) -> bool {
                false
            }
        }
    };
}

pub use derive_string_meta_structure;

/// Asserts the snapshot of an annotated structure using `insta`.
#[cfg(feature = "test")]
#[macro_export]
macro_rules! assert_annotated_snapshot {
    ($value:expr, @$snapshot:literal) => {
        ::insta::assert_snapshot!(
            $value.to_json_pretty().unwrap(),
            stringify!($value),
            @$snapshot
        )
    };
    ($value:expr, $debug_expr:expr, @$snapshot:literal) => {
        ::insta::assert_snapshot!(
            $value.to_json_pretty().unwrap(),
            $debug_expr,
            @$snapshot
        )
    };
    ($name:expr, $value:expr) => {
        ::insta::assert_snapshot!(
            $name,
            $value.to_json_pretty().unwrap(),
            stringify!($value)
        )
    };
    ($name:expr, $value:expr, $debug_expr:expr) => {
        ::insta::assert_snapshot!(
            $name,
            $value.to_json_pretty().unwrap(),
            $debug_expr
        )
    };
    ($value:expr) => {
        ::insta::assert_snapshot!(
            None::<String>,
            $value.to_json_pretty().unwrap(),
            stringify!($value)
        )
    };
}

#[cfg(test)]
mod tests {
    use similar_asserts::assert_eq;

    use crate::{Annotated, Array, Object};

    #[derive(Clone, Debug, PartialEq)]
    struct Inner {
        value: Annotated<u64>,
    }

    #[derive(Clone, Debug, PartialEq)]
    struct Outer {
        inner: Annotated<Inner>,
    }

    #[test]
    fn get_path() {
        let value = Annotated::new(1);
        let inner = Annotated::new(Inner {
            value: value.clone(),
        });
        let outer = Annotated::new(Outer {
            inner: inner.clone(),
        });

        // Optional
        assert_eq!(get_path!(outer), Some(&outer));
        assert_eq!(get_path!(outer.inner), Some(&inner));
        assert_eq!(get_path!(outer.inner.value), Some(&value));

        // Unwrap
        assert_eq!(get_path!(outer!), &outer);
        assert_eq!(get_path!(outer.inner!), &inner);
        assert_eq!(get_path!(outer.inner.value!), &value);
    }

    #[test]
    fn get_path_empty() {
        let empty = Annotated::<Outer>::empty();
        let outer_empty = Annotated::new(Outer {
            inner: Annotated::empty(),
        });
        let outer = Annotated::new(Outer {
            inner: Annotated::new(Inner {
                value: Annotated::empty(),
            }),
        });

        // Empty leaf
        assert_eq!(get_path!(empty), Some(&Annotated::empty()));
        assert_eq!(get_path!(outer_empty.inner), Some(&Annotated::empty()));
        assert_eq!(get_path!(outer.inner.value), Some(&Annotated::empty()));

        // Empty in the path
        assert_eq!(get_path!(empty.inner), None);
        assert_eq!(get_path!(empty.inner.value), None);
        assert_eq!(get_path!(outer_empty.inner.value), None);

        // Empty unwraps
        assert_eq!(get_path!(empty!), &Annotated::empty());
        assert_eq!(get_path!(outer_empty.inner!), &Annotated::empty());
        assert_eq!(get_path!(outer.inner.value!), &Annotated::empty());
    }

    #[test]
    fn get_path_array() {
        let array = Annotated::new(Array::from([Annotated::new(0), Annotated::new(1)]));

        // Indexes
        assert_eq!(get_path!(array[0]), Some(&Annotated::new(0)));
        assert_eq!(get_path!(array[1]), Some(&Annotated::new(1)));
        // Out of bounds
        assert_eq!(get_path!(array[2]), None);
        // Unwrap
        assert_eq!(get_path!(array[0]!), &Annotated::new(0));
    }

    #[test]
    fn get_path_object() {
        let object = Annotated::new(Object::from([("key".to_string(), Annotated::new(1))]));

        // Exists
        assert_eq!(get_path!(object["key"]), Some(&Annotated::new(1)));
        // Unwrap
        assert_eq!(get_path!(object["key"]!), &Annotated::new(1));
        // Does not exist
        assert_eq!(get_path!(object["other"]), None);
    }

    #[test]
    fn get_path_combined() {
        struct Inner {
            value: Annotated<u64>,
        }

        struct Outer {
            inners: Annotated<Object<Inner>>,
        }

        let outer = Annotated::new(Outer {
            inners: Annotated::new(Object::from([(
                "key".to_string(),
                Annotated::new(Inner {
                    value: Annotated::new(1),
                }),
            )])),
        });

        assert_eq!(
            get_path!(outer.inners["key"].value),
            Some(&Annotated::new(1))
        );
        assert_eq!(get_path!(outer.inners["key"].value!), &Annotated::new(1));
    }

    #[test]
    fn get_value() {
        let inner = Inner {
            value: Annotated::new(1),
        };
        let outer = Outer {
            inner: Annotated::new(inner.clone()),
        };
        let annotated = Annotated::new(outer.clone());

        // Optional
        assert_eq!(get_value!(annotated), Some(&outer));
        assert_eq!(get_value!(annotated.inner), Some(&inner));
        assert_eq!(get_value!(annotated.inner.value), Some(&1));

        // Unwrap
        assert_eq!(get_value!(annotated!), &outer);
        assert_eq!(get_value!(annotated.inner!), &inner);
        assert_eq!(get_value!(annotated.inner.value!), &1);
    }

    #[test]
    fn get_value_empty() {
        let empty = Annotated::<Outer>::empty();
        let outer_empty = Annotated::new(Outer {
            inner: Annotated::empty(),
        });
        let outer = Annotated::new(Outer {
            inner: Annotated::new(Inner {
                value: Annotated::empty(),
            }),
        });

        // Empty leaf
        assert_eq!(get_value!(empty), None);
        assert_eq!(get_value!(outer_empty.inner), None);
        assert_eq!(get_value!(outer.inner.value), None);

        // Empty in the path
        assert_eq!(get_value!(empty.inner), None);
        assert_eq!(get_value!(empty.inner.value), None);
        assert_eq!(get_value!(outer_empty.inner.value), None);
    }

    #[test]
    fn get_value_array() {
        let array = Annotated::new(Array::from([Annotated::new(0), Annotated::new(1)]));

        // Existing indexes
        assert_eq!(get_value!(array[0]), Some(&0));
        assert_eq!(get_value!(array[1]), Some(&1));
        // Out of bounds
        assert_eq!(get_value!(array[2]), None);
        // Unwrap
        assert_eq!(get_value!(array[0]!), &0);
    }

    #[test]
    fn get_value_object() {
        let object = Annotated::new(Object::from([("key".to_string(), Annotated::new(1))]));

        // Exists
        assert_eq!(get_value!(object["key"]), Some(&1));
        // Unwrap
        assert_eq!(get_value!(object["key"]!), &1);
        // Does not exist
        assert_eq!(get_value!(object["other"]), None);
    }

    #[test]
    fn get_value_combined() {
        struct Inner {
            value: Annotated<u64>,
        }

        struct Outer {
            inners: Annotated<Object<Inner>>,
        }

        let outer = Annotated::new(Outer {
            inners: Annotated::new(Object::from([(
                "key".to_string(),
                Annotated::new(Inner {
                    value: Annotated::new(1),
                }),
            )])),
        });

        assert_eq!(get_value!(outer.inners["key"].value), Some(&1));
        assert_eq!(get_value!(outer.inners["key"].value!), &1);
    }
}