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
use crate::*;

/// Traverse a value's fields and variants.
///
/// Each method of the `Visit` trait is a hook that enables the implementor to
/// observe value fields. By default, most methods are implemented as a no-op.
/// The `visit_primitive_slice` default implementation will iterate the slice,
/// calling `visit_value` with each item.
///
/// To recurse, the implementor must implement methods to visit the arguments.
///
/// # Examples
///
/// Recursively printing a Rust value.
///
/// ```
/// use valuable::{NamedValues, Valuable, Value, Visit};
///
/// struct Print(String);
///
/// impl Print {
///     fn indent(&self) -> Print {
///        Print(format!("{}    ", self.0))
///     }
/// }
///
/// impl Visit for Print {
///     fn visit_value(&mut self, value: Value<'_>) {
///         match value {
///             Value::Structable(v) => {
///                 let def = v.definition();
///                 // Print the struct name
///                 println!("{}{}:", self.0, def.name());
///
///                 // Visit fields
///                 let mut visit = self.indent();
///                 v.visit(&mut visit);
///             }
///             Value::Enumerable(v) => {
///                 let def = v.definition();
///                 let variant = v.variant();
///                 // Print the enum name
///                 println!("{}{}::{}:", self.0, def.name(), variant.name());
///
///                 // Visit fields
///                 let mut visit = self.indent();
///                 v.visit(&mut visit);
///             }
///             Value::Listable(v) => {
///                 println!("{}", self.0);
///
///                 // Visit fields
///                 let mut visit = self.indent();
///                 v.visit(&mut visit);
///             }
///             Value::Mappable(v) => {
///                 println!("{}", self.0);
///
///                 // Visit fields
///                 let mut visit = self.indent();
///                 v.visit(&mut visit);
///             }
///             // Primitive or unknown type, just render Debug
///             v => println!("{:?}", v),
///         }
///     }
///
///     fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
///         for (field, value) in named_values {
///             print!("{}- {}: ", self.0, field.name());
///             value.visit(self);
///         }
///     }
///
///     fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
///         for value in values {
///             print!("{}- ", self.0);
///             value.visit(self);
///         }
///     }
///
///     fn visit_entry(&mut self, key: Value<'_>, value: Value<'_>) {
///         print!("{}- {:?}: ", self.0, key);
///         value.visit(self);
///     }
/// }
///
/// #[derive(Valuable)]
/// struct Person {
///     name: String,
///     age: u32,
///     addresses: Vec<Address>,
/// }
///
/// #[derive(Valuable)]
/// struct Address {
///     street: String,
///     city: String,
///     zip: String,
/// }
///
/// let person = Person {
///     name: "Angela Ashton".to_string(),
///     age: 31,
///     addresses: vec![
///         Address {
///             street: "123 1st Ave".to_string(),
///             city: "Townsville".to_string(),
///             zip: "12345".to_string(),
///         },
///         Address {
///             street: "555 Main St.".to_string(),
///             city: "New Old Town".to_string(),
///             zip: "55555".to_string(),
///         },
///     ],
/// };
///
/// let mut print = Print("".to_string());
/// valuable::visit(&person, &mut print);
/// ```
pub trait Visit {
    /// Visit a single value.
    ///
    /// The `visit_value` method is called once when visiting single primitive
    /// values. When visiting `Listable` types, the `visit_value` method is
    /// called once per item in the listable type.
    ///
    /// Note, in the case of Listable types containing primitive types,
    /// `visit_primitive_slice` can be implemented instead for less overhead.
    ///
    /// # Examples
    ///
    /// Visiting a single value.
    ///
    /// ```
    /// use valuable::{Valuable, Visit, Value};
    ///
    /// struct Print;
    ///
    /// impl Visit for Print {
    ///     fn visit_value(&mut self, value: Value<'_>) {
    ///         println!("{:?}", value);
    ///     }
    /// }
    ///
    /// let my_val = 123;
    /// my_val.visit(&mut Print);
    /// ```
    ///
    /// Visiting multiple values in a list.
    ///
    /// ```
    /// use valuable::{Valuable, Value, Visit};
    ///
    /// struct PrintList { comma: bool };
    ///
    /// impl Visit for PrintList {
    ///     fn visit_value(&mut self, value: Value<'_>) {
    ///         match value {
    ///             Value::Listable(v) => v.visit(self),
    ///             value => {
    ///                 if self.comma {
    ///                     println!(", {:?}", value);
    ///                 } else {
    ///                     print!("{:?}", value);
    ///                     self.comma = true;
    ///                 }
    ///             }
    ///         }
    ///     }
    /// }
    ///
    /// let my_list = vec![1, 2, 3, 4, 5];
    /// valuable::visit(&my_list, &mut PrintList { comma: false });
    /// ```
    fn visit_value(&mut self, value: Value<'_>);

    /// Visit a struct or enum's named fields.
    ///
    /// When the struct/enum is statically defined, all fields are known ahead
    /// of time and `visit_named_fields` is called once with all field values.
    /// When the struct/enum is dynamic, then the `visit_named_fields` method
    /// may be called multiple times.
    ///
    /// See [`Structable`] and [`Enumerable`] for static vs. dynamic details.
    ///
    /// # Examples
    ///
    /// Visiting all fields in a struct.
    ///
    /// ```
    /// use valuable::{NamedValues, Valuable, Value, Visit};
    ///
    /// #[derive(Valuable)]
    /// struct MyStruct {
    ///     hello: String,
    ///     world: u32,
    /// }
    ///
    /// struct Print;
    ///
    /// impl Visit for Print {
    ///     fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
    ///         for (field, value) in named_values {
    ///             println!("{:?}: {:?}", field, value);
    ///         }
    ///     }
    ///
    ///     fn visit_value(&mut self, value: Value<'_>) {
    ///         match value {
    ///             Value::Structable(v) => v.visit(self),
    ///             _ => {} // do nothing for other types
    ///         }
    ///     }
    /// }
    ///
    /// let my_struct = MyStruct {
    ///     hello: "Hello world".to_string(),
    ///     world: 42,
    /// };
    ///
    /// valuable::visit(&my_struct, &mut Print);
    /// ```
    fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
        let _ = named_values;
    }

    /// Visit a struct or enum's unnamed fields.
    ///
    /// When the struct/enum is statically defined, all fields are known ahead
    /// of time and `visit_unnamed_fields` is called once with all field values.
    /// When the struct/enum is dynamic, then the `visit_unnamed_fields` method
    /// may be called multiple times.
    ///
    /// See [`Structable`] and [`Enumerable`] for static vs. dynamic details.
    ///
    /// # Examples
    ///
    /// Visiting all fields in a struct.
    ///
    /// ```
    /// use valuable::{Valuable, Value, Visit};
    ///
    /// #[derive(Valuable)]
    /// struct MyStruct(String, u32);
    ///
    /// struct Print;
    ///
    /// impl Visit for Print {
    ///     fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
    ///         for value in values {
    ///             println!("{:?}", value);
    ///         }
    ///     }
    ///
    ///     fn visit_value(&mut self, value: Value<'_>) {
    ///         match value {
    ///             Value::Structable(v) => v.visit(self),
    ///             _ => {} // do nothing for other types
    ///         }
    ///     }
    /// }
    ///
    /// let my_struct = MyStruct("Hello world".to_string(), 42);
    ///
    /// valuable::visit(&my_struct, &mut Print);
    /// ```
    fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
        let _ = values;
    }

    /// Visit a primitive slice.
    ///
    /// This method exists as an optimization when visiting [`Listable`] types.
    /// By default, `Listable` types are visited by passing each item to
    /// `visit_value`. However, if the listable stores a **primitive** type
    /// within contiguous memory, then `visit_primitive_slice` is called
    /// instead.
    ///
    /// When implementing `visit_primitive_slice`, be aware that the method may
    /// be called multiple times for a single `Listable` type.
    ///
    /// # Examples
    ///
    /// A vec calls `visit_primitive_slice` one time, but a `VecDeque` will call
    /// `visit_primitive_slice` twice.
    ///
    /// ```
    /// use valuable::{Valuable, Value, Visit, Slice};
    /// use std::collections::VecDeque;
    ///
    /// struct Count(u32);
    ///
    /// impl Visit for Count {
    ///     fn visit_primitive_slice(&mut self, slice: Slice<'_>) {
    ///         self.0 += 1;
    ///     }
    ///
    ///     fn visit_value(&mut self, value: Value<'_>) {
    ///         match value {
    ///             Value::Listable(v) => v.visit(self),
    ///             _ => {} // do nothing for other types
    ///         }
    ///     }
    /// }
    ///
    /// let vec = vec![1, 2, 3, 4, 5];
    ///
    /// let mut count = Count(0);
    /// valuable::visit(&vec, &mut count);
    /// assert_eq!(1, count.0);
    ///
    /// let mut vec_deque = VecDeque::from(vec);
    ///
    /// let mut count = Count(0);
    /// valuable::visit(&vec_deque, &mut count);
    ///
    /// assert_eq!(2, count.0);
    /// ```
    fn visit_primitive_slice(&mut self, slice: Slice<'_>) {
        for value in slice {
            self.visit_value(value);
        }
    }

    /// Visit a `Mappable`'s entries.
    ///
    /// The `visit_entry` method is called once for each entry contained by a
    /// `Mappable.`
    ///
    /// # Examples
    ///
    /// Visit a map's entries
    ///
    /// ```
    /// use valuable::{Valuable, Value, Visit};
    /// use std::collections::HashMap;
    ///
    /// let mut map = HashMap::new();
    /// map.insert("hello", 123);
    /// map.insert("world", 456);
    ///
    /// struct Print;
    ///
    /// impl Visit for Print {
    ///     fn visit_entry(&mut self, key: Value<'_>, value: Value<'_>) {
    ///         println!("{:?} => {:?}", key, value);
    ///     }
    ///
    ///     fn visit_value(&mut self, value: Value<'_>) {
    ///         match value {
    ///             Value::Mappable(v) => v.visit(self),
    ///             _ => {} // do nothing for other types
    ///         }
    ///     }
    /// }
    ///
    /// valuable::visit(&map, &mut Print);
    /// ```
    fn visit_entry(&mut self, key: Value<'_>, value: Value<'_>) {
        let _ = (key, value);
    }
}

macro_rules! deref {
    (
        $(
            $(#[$attrs:meta])*
            $ty:ty,
        )*
    ) => {
        $(
            $(#[$attrs])*
            impl<T: ?Sized + Visit> Visit for $ty {
                fn visit_value(&mut self, value: Value<'_>) {
                    T::visit_value(&mut **self, value)
                }

                fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
                    T::visit_named_fields(&mut **self, named_values)
                }

                fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
                    T::visit_unnamed_fields(&mut **self, values)
                }

                fn visit_primitive_slice(&mut self, slice: Slice<'_>) {
                    T::visit_primitive_slice(&mut **self, slice)
                }

                fn visit_entry(&mut self, key: Value<'_>, value: Value<'_>) {
                    T::visit_entry(&mut **self, key, value)
                }
            }
        )*
    };
}

deref! {
    &mut T,
    #[cfg(feature = "alloc")]
    alloc::boxed::Box<T>,
}

/// Inspects a value by calling the relevant [`Visit`] methods with `value`'s
/// data.
///
/// This method calls [`Visit::visit_value()`] with the provided [`Valuable`]
/// instance. See [`Visit`] documentation for more details.
///
/// # Examples
///
/// Extract a single field from a struct. Note: if the same field is repeatedly
/// extracted from a struct, it is preferable to obtain the associated
/// [`NamedField`] once and use it repeatedly.
///
/// ```
/// use valuable::{NamedValues, Valuable, Value, Visit};
///
/// #[derive(Valuable)]
/// struct MyStruct {
///     foo: usize,
///     bar: usize,
/// }
///
/// struct GetFoo(usize);
///
/// impl Visit for GetFoo {
///     fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
///         if let Some(foo) = named_values.get_by_name("foo") {
///             if let Some(val) = foo.as_usize() {
///                 self.0 = val;
///             }
///         }
///     }
///
///     fn visit_value(&mut self, value: Value<'_>) {
///         if let Value::Structable(v) = value {
///             v.visit(self);
///         }
///     }
/// }
///
/// let my_struct = MyStruct {
///     foo: 123,
///     bar: 456,
/// };
///
/// let mut get_foo = GetFoo(0);
/// valuable::visit(&my_struct, &mut get_foo);
///
/// assert_eq!(123, get_foo.0);
/// ```
///
/// [`Visit`]: Visit [`NamedField`]: crate::NamedField
pub fn visit(value: &impl Valuable, visit: &mut dyn Visit) {
    visit.visit_value(value.as_value());
}