typex 0.1.1

Runtime reflection: inspection, traversal, equality and mutation
Documentation
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
use crate::{Object, ObjectRef, ObjectRefMut};
// Keep public API names in scope for short intra-doc links.
#[allow(unused_imports)]
use crate::PatchOperation;
use alloc::collections::{BTreeMap, LinkedList, VecDeque};
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use core::marker::PhantomData;

/// Borrowed path segment for nested reflective traversal.
///
/// A slice of segments can traverse named fields, indexed items and keyed
/// entries in sequence. The borrowed names remain valid for the path's
/// lifetime.
///
/// # Example
///
/// ```
/// use std::collections::BTreeMap;
/// use typex::{Meta, ObjectRef, PathSegment};
///
/// #[derive(Meta)]
/// struct Document {
///   sections: Vec<BTreeMap<&'static str, u8>>,
/// }
///
/// let document = Document {
///   sections: vec![BTreeMap::from([("title", 42)])],
/// };
///
/// let path = [
///   PathSegment::Field("sections"),
///   PathSegment::Item(0),
///   PathSegment::Key("title"),
/// ];
///
/// let title = ObjectRef::new(&document).field_path(&path).unwrap();
/// assert_eq!(title.to_ref::<u8>(), Some(&42));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PathSegment<'a> {
  /// Traverses a named field.
  Field(&'a str),
  /// Traverses a keyed entry.
  Key(&'a str),
  /// Selects the active enum variant with this name and keeps the enum as the
  /// current value. Traversal fails when another variant is active.
  Variant(&'a str),
  /// Traverses an indexed item.
  Item(usize),
}

/// Owned path used by staged operations and rollback records.
///
/// Stores every segment in one string buffer. Converting a slice reserves
/// space for all its segments before encoding them. [`iter`](Self::iter) yields
/// borrowed [`PathSegment`] values.
///
/// # Example
///
/// ```
/// # use typex::{OwnedPath, PathSegment};
/// let path = OwnedPath::from([PathSegment::Field("items"), PathSegment::Item(2)].as_slice());
///
/// assert_eq!(
///   path.iter().collect::<Vec<_>>(),
///   [PathSegment::Field("items"), PathSegment::Item(2)]
/// );
/// ```
// Each segment is a tag (see `FIELD_TAG` and the constants after it) followed by
// a number. For fields, keys and variants, the number is the name's length in bytes and the
// name follows. For items, the number is the index. Numbers are stored in 6-bit
// groups, least significant first, one group per byte. Bit 6 marks that another
// group follows and bit 7 stays clear, so every byte is ASCII and the buffer
// remains a valid `String` whose names can be borrowed as `&str` without
// validation.
#[derive(Clone, Default, PartialEq, Eq, Hash)]
pub struct OwnedPath {
  encoded: String,
}

impl OwnedPath {
  /// Creates an empty path.
  #[inline]
  pub fn new() -> Self {
    Self::default()
  }

  /// Returns whether the path has no segments.
  #[inline]
  pub fn is_empty(&self) -> bool {
    self.encoded.is_empty()
  }

  /// Returns the number of segments.
  #[inline]
  pub fn len(&self) -> usize {
    self.iter().count()
  }

  /// Appends a segment.
  #[inline]
  pub fn push(&mut self, segment: PathSegment<'_>) {
    let (tag, number, name) = match segment {
      PathSegment::Field(name) => (FIELD_TAG, name.len(), name),
      PathSegment::Key(key) => (KEY_TAG, key.len(), key),
      PathSegment::Variant(name) => (VARIANT_TAG, name.len(), name),
      PathSegment::Item(index) => (ITEM_TAG, index, ""),
    };
    self.encoded.push(tag);
    push_number(&mut self.encoded, number);
    self.encoded.push_str(name);
  }

  /// Returns an iterator over the segments in order.
  #[inline]
  pub fn iter(&self) -> OwnedPathIter<'_> {
    OwnedPathIter {
      rest: &self.encoded,
    }
  }
}

fn encoded_len(segment: &PathSegment<'_>) -> usize {
  let (number, name_len) = match segment {
    PathSegment::Field(name) | PathSegment::Variant(name) | PathSegment::Key(name) => {
      (name.len(), name.len())
    }
    PathSegment::Item(index) => (*index, 0),
  };
  1 + number_len(number) + name_len
}

// `Item` and `Variant` have the highest tags, so decoding separates them from
// the more common fields and keys with one comparison.
const FIELD_TAG: char = '0';
const KEY_TAG: char = '1';
const VARIANT_TAG: char = '2';
const ITEM_TAG: char = '3';

const NUMBER_BITS: u32 = 6;
const NUMBER_MASK: u8 = 0x3F;
const NUMBER_CONTINUE: u8 = 0x40;

fn number_len(mut number: usize) -> usize {
  let mut len = 1;
  while number > usize::from(NUMBER_MASK) {
    number >>= NUMBER_BITS;
    len += 1;
  }
  len
}

fn push_number(encoded: &mut String, mut number: usize) {
  loop {
    let group = number as u8 & NUMBER_MASK;
    number >>= NUMBER_BITS;
    if number == 0 {
      encoded.push(char::from(group));
      return;
    }
    encoded.push(char::from(group | NUMBER_CONTINUE));
  }
}

impl fmt::Debug for OwnedPath {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_list().entries(self.iter()).finish()
  }
}

impl<'a> From<&[PathSegment<'a>]> for OwnedPath {
  fn from(segments: &[PathSegment<'a>]) -> Self {
    let capacity = segments.iter().map(encoded_len).sum();
    let mut path = Self {
      encoded: String::with_capacity(capacity),
    };
    for &segment in segments {
      path.push(segment);
    }
    path
  }
}

impl<'a> FromIterator<PathSegment<'a>> for OwnedPath {
  fn from_iter<I: IntoIterator<Item = PathSegment<'a>>>(segments: I) -> Self {
    let mut path = Self::new();
    path.extend(segments);
    path
  }
}

impl<'a> Extend<PathSegment<'a>> for OwnedPath {
  fn extend<I: IntoIterator<Item = PathSegment<'a>>>(&mut self, segments: I) {
    for segment in segments {
      self.push(segment);
    }
  }
}

impl<'a> IntoIterator for &'a OwnedPath {
  type Item = PathSegment<'a>;
  type IntoIter = OwnedPathIter<'a>;

  fn into_iter(self) -> OwnedPathIter<'a> {
    self.iter()
  }
}

/// Iterator over the segments of an [`OwnedPath`].
#[derive(Clone, Debug)]
pub struct OwnedPathIter<'a> {
  rest: &'a str,
}

/// Splits a variant segment whose name starts at `start` and has `len` bytes
/// from `rest`, returning the segment and the encoding that follows it.
// Kept out of line so that the decoding of other segments stays small. Taking
// and returning `rest` by value keeps the iterator state in registers.
#[cold]
#[inline(never)]
fn split_variant(rest: &str, start: usize, len: usize) -> (PathSegment<'_>, &str) {
  let end = start + len;
  (PathSegment::Variant(&rest[start..end]), &rest[end..])
}

impl<'a> Iterator for OwnedPathIter<'a> {
  type Item = PathSegment<'a>;

  // Without forced inlining, mutation batches call this out of line and return
  // each segment through memory.
  #[inline(always)]
  fn next(&mut self) -> Option<PathSegment<'a>> {
    let bytes = self.rest.as_bytes();
    let tag = *bytes.first()?;
    let mut number = 0_usize;
    let mut shift = 0;
    let mut position = 1;
    loop {
      let byte = bytes[position];
      position += 1;
      number |= usize::from(byte & NUMBER_MASK) << shift;
      if byte & NUMBER_CONTINUE == 0 {
        break;
      }
      shift += NUMBER_BITS;
    }
    let start = position;
    // `Item` and `Variant` have the two highest tags, so one comparison
    // separates them from fields and keys, which decode without a further
    // tag comparison than the one that selects between them.
    if tag >= VARIANT_TAG as u8 {
      if tag == ITEM_TAG as u8 {
        self.rest = &self.rest[start..];
        return Some(PathSegment::Item(number));
      }
      let (segment, rest) = split_variant(self.rest, start, number);
      self.rest = rest;
      return Some(segment);
    }
    let end = start + number;
    let name = &self.rest[start..end];
    self.rest = &self.rest[end..];
    Some(if tag == FIELD_TAG as u8 {
      PathSegment::Field(name)
    } else {
      PathSegment::Key(name)
    })
  }
}

/// Typed path from a `Root` value to a nested `Value`.
///
/// Start a path with [`TypedField::path`] on a `FIELD_*` constant that
/// `#[derive(Meta)]` generates, then extend it with [`then`](Self::then) for
/// fields, [`item`](Self::item) for sequence items and [`key`](Self::key) for
/// map entries. Lookups return `&Value` or `&mut Value` without a downcast.
///
/// [`ObjectRef::field_path`], [`ObjectRefMut::field_path_mut`] and
/// [`ObjectRefMut::set_field_path`] accept typed paths. Typed paths iterate as
/// [`PathSegment`] values, so they can be passed to [`PatchOperation::set`].
/// The lifetime `'k` bounds the keys that [`key`](Self::key) borrows. Paths
/// without key steps are `TypedPath<'static, Root, Value>`.
///
/// The compiler checks `Value` at each step but not `Root`. A lookup fails at
/// runtime when:
///
/// - the path is applied to a type other than `Root`
/// - an enum holds a variant other than the one the path names
/// - an index or key is absent
/// - a hand-written [`TypedField::new`] constant names a missing field or
///   declares the wrong type
///
/// # Example
///
/// ```
/// # use typex::{Meta, MetaMut, ObjectRef, ObjectRefMut};
/// #[derive(Meta, MetaMut)]
/// struct Scope {
///   name: &'static str,
/// }
///
/// #[derive(Meta, MetaMut)]
/// struct User {
///   id: u16,
///   scopes: Vec<Scope>,
/// }
///
/// let mut user = User { id: 42, scopes: vec![Scope { name: "read" }] };
///
/// // `TypedPath<'static, User, &'static str>`
/// let name = User::FIELD_SCOPES.item(0).then(Scope::FIELD_NAME);
/// assert_eq!(ObjectRef::new(&user).field_path(name), Some(&"read"));
///
/// *ObjectRefMut::new(&mut user)
///   .field_path_mut(User::FIELD_ID.path())
///   .unwrap() = 7;
/// assert_eq!(user.id, 7);
/// ```
///
/// Map entries with `String` or `&'static str` keys use [`key`](Self::key):
///
/// ```
/// # use std::collections::BTreeMap;
/// # use typex::{Meta, ObjectRef};
/// #[derive(Meta)]
/// struct Config {
///   limits: BTreeMap<String, u32>,
/// }
///
/// let config = Config {
///   limits: BTreeMap::from([(String::from("requests"), 100)]),
/// };
///
/// let key = String::from("requests");
/// assert_eq!(
///   ObjectRef::new(&config).field_path(Config::FIELD_LIMITS.key(&key)),
///   Some(&100)
/// );
/// ```
pub struct TypedPath<'k, Root, Value> {
  segments: Vec<PathSegment<'k>>,
  marker: PhantomData<fn() -> (Root, Value)>,
}

/// A single typed field segment that can be composed into a [`TypedPath`].
///
/// The parent and value type parameters let `then` and `item` carry the
/// expected types through a path without requiring a second downcast at the
/// call site.
///
/// The derive macro generates associated `FIELD_*` constants for named struct
/// fields and enum variants, including constants for enum variant fields and
/// tuple items.
///
/// # Example
///
/// ```
/// # use typex::{Meta, ObjectRef};
/// #[derive(Meta)]
/// struct Address {
///   city: &'static str,
/// }
///
/// #[derive(Meta)]
/// struct User {
///   addresses: Vec<Address>,
/// }
///
/// let user = User {
///   addresses: vec![Address { city: "London" }],
/// };
/// let city = User::FIELD_ADDRESSES.item(0).then(Address::FIELD_CITY);
///
/// assert_eq!(ObjectRef::new(&user).field_path(city), Some(&"London"));
/// ```
#[derive(Clone, Copy)]
pub struct TypedField<Parent, Value> {
  name: &'static str,
  segments: Option<&'static [PathSegment<'static>]>,
  marker: PhantomData<fn() -> (Parent, Value)>,
}

/// Associates a sequential value with the type of each indexed item.
///
/// This is implemented for the built-in sequence collections that expose
/// uniform indexed items. The trait only carries type information; traversal
/// can still fail when an index is absent or the collection cannot provide a
/// mutable item.
pub trait TypedSequence {
  /// Type of each indexed item in the sequence.
  type Item;
}

impl<Item> TypedSequence for Vec<Item> {
  type Item = Item;
}

impl<Item, const LENGTH: usize> TypedSequence for [Item; LENGTH] {
  type Item = Item;
}

impl<Item> TypedSequence for VecDeque<Item> {
  type Item = Item;
}

impl<Item> TypedSequence for LinkedList<Item> {
  type Item = Item;
}

/// Associates a map with the type of its values.
///
/// Implemented for `BTreeMap` and `HashMap` with `String` or `&'static str`
/// keys, which are the key types that [`PathSegment::Key`] can look up. The
/// trait only carries type information; traversal fails when the key is absent.
pub trait TypedMap {
  /// Type of each value in the map.
  type Value;
}

impl<Value> TypedMap for BTreeMap<String, Value> {
  type Value = Value;
}

impl<Value> TypedMap for BTreeMap<&'static str, Value> {
  type Value = Value;
}

#[cfg(feature = "std")]
impl<Value, State> TypedMap for std::collections::HashMap<String, Value, State> {
  type Value = Value;
}

#[cfg(feature = "std")]
impl<Value, State> TypedMap for std::collections::HashMap<&'static str, Value, State> {
  type Value = Value;
}

impl<Parent, Value> TypedField<Parent, Value> {
  /// Creates a typed field segment.
  pub const fn new(name: &'static str) -> Self {
    TypedField {
      name,
      segments: None,
      marker: PhantomData,
    }
  }

  /// Creates a typed field from a static reflective path.
  #[doc(hidden)]
  pub const fn from_segments(segments: &'static [PathSegment<'static>]) -> Self {
    TypedField {
      name: "",
      segments: Some(segments),
      marker: PhantomData,
    }
  }

  /// Starts a path at this field and appends another typed field.
  pub fn then<Next>(self, next: TypedField<Value, Next>) -> TypedPath<'static, Parent, Next> {
    TypedPath::root().then(self).then(next)
  }

  /// Converts this field into a one-segment typed path.
  pub fn path(self) -> TypedPath<'static, Parent, Value> {
    TypedPath::root().then(self)
  }
}

impl<Parent, Sequence> TypedField<Parent, Sequence>
where
  Sequence: TypedSequence,
{
  /// Starts an indexed path into this sequential field.
  pub fn item(self, index: usize) -> TypedPath<'static, Parent, Sequence::Item> {
    self.path().item(index)
  }
}

impl<Parent, Map> TypedField<Parent, Map>
where
  Map: TypedMap,
{
  /// Starts a keyed path into this map field. The path borrows `key`.
  pub fn key(self, key: &str) -> TypedPath<'_, Parent, Map::Value> {
    self.path().key(key)
  }
}

impl<'k, Root, Value> TypedPath<'k, Root, Value> {
  /// Appends a field of the current `Value`. The path's `Value` becomes the
  /// field's type.
  pub fn then<Next>(mut self, field: TypedField<Value, Next>) -> TypedPath<'k, Root, Next> {
    if let Some(segments) = field.segments {
      self.segments.extend_from_slice(segments);
    } else {
      self.segments.push(PathSegment::Field(field.name));
    }
    TypedPath {
      segments: self.segments,
      marker: PhantomData,
    }
  }

  /// Returns the reflective representation of this path for APIs that accept
  /// raw [`PathSegment`] values.
  pub fn segments(&self) -> &[PathSegment<'k>] {
    &self.segments
  }
}

impl<'k, Root, Sequence> TypedPath<'k, Root, Sequence>
where
  Sequence: TypedSequence,
{
  /// Appends an index into the current sequence. The path's `Value` becomes
  /// the sequence's item type.
  pub fn item(mut self, index: usize) -> TypedPath<'k, Root, Sequence::Item> {
    self.segments.push(PathSegment::Item(index));
    TypedPath {
      segments: self.segments,
      marker: PhantomData,
    }
  }
}

impl<'k, Root, Map> TypedPath<'k, Root, Map>
where
  Map: TypedMap,
{
  /// Appends a key into the current map. The path borrows `key` and its
  /// `Value` becomes the map's value type.
  pub fn key(mut self, key: &'k str) -> TypedPath<'k, Root, Map::Value> {
    self.segments.push(PathSegment::Key(key));
    TypedPath {
      segments: self.segments,
      marker: PhantomData,
    }
  }
}

impl<Root> TypedPath<'_, Root, Root> {
  /// Creates an empty path rooted at `Root`.
  pub fn root() -> Self {
    TypedPath {
      segments: Vec::new(),
      marker: PhantomData,
    }
  }
}

impl<'k, Root, Value> IntoIterator for TypedPath<'k, Root, Value> {
  type Item = PathSegment<'k>;
  type IntoIter = alloc::vec::IntoIter<Self::Item>;

  fn into_iter(self) -> Self::IntoIter {
    self.segments.into_iter()
  }
}

impl<'a, 'k, Root, Value> IntoIterator for &'a TypedPath<'k, Root, Value> {
  type Item = PathSegment<'k>;
  type IntoIter = core::iter::Copied<core::slice::Iter<'a, Self::Item>>;

  fn into_iter(self) -> Self::IntoIter {
    self.segments.iter().copied()
  }
}

/// Path accepted by `field_path`. Its kind determines the return type.
///
/// This trait is not normally used directly. Pass one of the following to
/// [`ObjectRef::field_path`] or to `field_path` on another wrapper such as
/// [`Object`]:
///
/// | Path | Returns |
/// |---|---|
/// | `&[PathSegment]` or `&[PathSegment; N]` | `Option<ObjectRef>`, a reflective view of the target |
/// | [`TypedPath<'k, Root, Value>`](TypedPath) | `Option<&Value>`, a reference to the target itself |
///
/// Raw segments suit paths that are only known at runtime. A [`TypedPath`]
/// needs no downcast. Both return `None` when a segment cannot be found. A
/// typed path also returns `None` when the target is not a `Value`, such as
/// when the path is applied to a type other than `Root`.
///
/// # Example
///
/// ```
/// # use typex::{Meta, ObjectRef, PathSegment};
/// #[derive(Meta)]
/// struct User {
///   id: u16,
///   tags: Vec<&'static str>,
/// }
///
/// let user = User { id: 42, tags: vec!["admin"] };
///
/// // A raw path returns an `ObjectRef` that still needs a downcast
/// let root = ObjectRef::new(&user);
/// let tag = root
///   .field_path(&[PathSegment::Field("tags"), PathSegment::Item(0)])
///   .unwrap();
/// assert_eq!(tag.to_ref::<&'static str>(), Some(&"admin"));
///
/// // A typed path returns `&u16` directly
/// assert_eq!(root.field_path(User::FIELD_ID.path()), Some(&42));
/// ```
pub trait FieldPathQuery<'a> {
  /// Value that the path resolves to.
  type Output;

  #[doc(hidden)]
  fn resolve(self, root: ObjectRef<'a>) -> Option<Self::Output>;
}

impl<'a, 'b, 'c> FieldPathQuery<'a> for &'b [PathSegment<'c>] {
  type Output = ObjectRef<'a>;

  #[inline]
  fn resolve(self, root: ObjectRef<'a>) -> Option<Self::Output> {
    let mut current = root;

    for segment in self {
      current = match segment {
        PathSegment::Field(name) => current.field(name)?,
        PathSegment::Variant(name) => current.variant(name)?,
        PathSegment::Item(index) => current.item(*index)?,
        PathSegment::Key(key) => current.key(key)?,
      };
    }

    Some(current)
  }
}

// Array literals (`&[PathSegment::Field("x")]`) don't unsize-coerce to `&[_]`
// at a generic call site the way they do for a concretely-typed parameter,
// so this mirrors the slice impl to keep that call shape working.
impl<'a, 'b, 'c, const N: usize> FieldPathQuery<'a> for &'b [PathSegment<'c>; N] {
  type Output = ObjectRef<'a>;

  fn resolve(self, root: ObjectRef<'a>) -> Option<Self::Output> {
    FieldPathQuery::resolve(self.as_slice(), root)
  }
}

impl<'a, Root, Value: 'static> FieldPathQuery<'a> for TypedPath<'_, Root, Value> {
  type Output = &'a Value;

  fn resolve(self, root: ObjectRef<'a>) -> Option<Self::Output> {
    root.field_path(self.segments())?.to_ref::<Value>()
  }
}

/// Failure returned by reflective path traversal or mutable operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReflectiveError {
  /// A field, item or key was not available at some path segment.
  PathNotFound,
  /// A supplied value or requested concrete type did not match the target.
  MutationTypeMismatch,
}

impl fmt::Display for ReflectiveError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::PathNotFound => write!(f, "Reflective path not found"),
      Self::MutationTypeMismatch => {
        write!(f, "The value has an incompatible concrete type")
      }
    }
  }
}

#[cfg(feature = "std")]
impl std::error::Error for ReflectiveError {}

/// Path accepted by `field_path_mut` and [`ObjectRefMut::set_field_path`]:
/// the mutable counterpart of [`FieldPathQuery`].
///
/// | Path | Returns | Assigned value |
/// |---|---|---|
/// | `&[PathSegment]` or `&[PathSegment; N]` | `Result<ObjectRefMut, ReflectiveError>` | [`Object`] |
/// | [`TypedPath<'k, Root, Value>`](TypedPath) | `Result<&mut Value, ReflectiveError>` | `Value` |
///
/// A lookup fails with [`ReflectiveError::PathNotFound`] when a segment is
/// missing. A typed path also fails with
/// [`ReflectiveError::MutationTypeMismatch`] when the target is not a `Value`.
///
/// # Example
///
/// ```
/// # use typex::{Meta, MetaMut, ObjectRefMut, PathSegment};
/// #[derive(Meta, MetaMut)]
/// struct User {
///   id: u16,
///   tags: Vec<&'static str>,
/// }
///
/// let mut user = User { id: 42, tags: vec!["admin"] };
///
/// // A raw path returns an `ObjectRefMut` that still needs a downcast
/// let mut root = ObjectRefMut::new(&mut user);
/// let tag = root
///   .field_path_mut(&[PathSegment::Field("tags"), PathSegment::Item(0)])
///   .unwrap();
/// *tag.to_mut::<&'static str>().unwrap() = "owner";
///
/// // A typed path returns `&mut u16` directly
/// *root.field_path_mut(User::FIELD_ID.path()).unwrap() = 7;
/// assert_eq!(user.tags, ["owner"]);
/// assert_eq!(user.id, 7);
/// ```
pub trait FieldPathQueryMut<'r> {
  /// Value that the path resolves to.
  type Output;

  /// Value that [`ObjectRefMut::set_field_path`] writes to the path's target.
  type Value;

  #[doc(hidden)]
  fn resolve(self, root: ObjectRefMut<'r>) -> Result<Self::Output, ReflectiveError>;

  #[doc(hidden)]
  fn assign(self, root: ObjectRefMut<'r>, value: Self::Value) -> Result<(), ReflectiveError>;
}

impl<'r, 'b, 'c> FieldPathQueryMut<'r> for &'b [PathSegment<'c>] {
  type Output = ObjectRefMut<'r>;
  type Value = Object;

  #[inline]
  fn resolve(self, root: ObjectRefMut<'r>) -> Result<Self::Output, ReflectiveError> {
    ObjectRefMut::path_from(root.inner, self).ok_or(ReflectiveError::PathNotFound)
  }

  #[inline]
  fn assign(self, root: ObjectRefMut<'r>, value: Object) -> Result<(), ReflectiveError> {
    FieldPathQueryMut::resolve(self, root)?.set(value)
  }
}

// See the array impl of `FieldPathQuery` above for why this is needed
// alongside the slice impl.
impl<'r, 'b, 'c, const N: usize> FieldPathQueryMut<'r> for &'b [PathSegment<'c>; N] {
  type Output = ObjectRefMut<'r>;
  type Value = Object;

  fn resolve(self, root: ObjectRefMut<'r>) -> Result<Self::Output, ReflectiveError> {
    FieldPathQueryMut::resolve(self.as_slice(), root)
  }

  fn assign(self, root: ObjectRefMut<'r>, value: Object) -> Result<(), ReflectiveError> {
    FieldPathQueryMut::assign(self.as_slice(), root, value)
  }
}

impl<'r, Root, Value: 'static> FieldPathQueryMut<'r> for TypedPath<'_, Root, Value> {
  type Output = &'r mut Value;
  type Value = Value;

  fn resolve(self, root: ObjectRefMut<'r>) -> Result<Self::Output, ReflectiveError> {
    ObjectRefMut::path_from(root.inner, self.segments())
      .ok_or(ReflectiveError::PathNotFound)?
      .to_mut::<Value>()
  }

  fn assign(self, root: ObjectRefMut<'r>, value: Value) -> Result<(), ReflectiveError> {
    *FieldPathQueryMut::resolve(self, root)? = value;
    Ok(())
  }
}