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
use crate::patch::{OwnedPatchOperation, kind_of, move_index_out_of_bounds};
use crate::path::OwnedPath;
use crate::reflect::with_sequence_mut;
use crate::{
  MetaMut, MoveItemError, Object, ObjectRef, PatchOperation, PatchOperationKind, PathSegment,
  ReflectMut, TypeInfo, ValueKind, select_variant,
};
// Keep public API names in scope for short intra-doc links.
#[allow(unused_imports)]
use crate::{Meta, ObjectRefMut};
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::iter::FromIterator;
use core::{fmt, mem};

/// Holds inverse operations used by [`undo`](Self::undo) to revert a committed
/// [`MutationBatch`].
///
/// [`MutationBatch::commit`] returns a [`MutationRollback`] on success. Each
/// applied operation adds one inverse. A `set` inverse holds the previous
/// value. A `push_item` inverse records the sequence length before appending
/// as the inserted item's index, then removes the item at that index on undo.
///
/// Use [`previous_value`](Self::previous_value) to inspect values overwritten
/// or removed by the batch. Use [`undo`](Self::undo) to revert the batch, or
/// drop the rollback to retain the changes.
///
/// Each inverse targets the path and index recorded at commit. If the target
/// changes between commit and [`undo`](Self::undo), an inverse may no longer
/// resolve, in which case [`undo`](Self::undo) returns a [`RollbackError`], or
/// may restore a value at a different position.
///
/// # Example
///
/// ```
/// # use typex::{MutationBatch, PatchOperation, PathSegment};
/// let mut items = vec![23_u8, 42];
/// let rollback = MutationBatch::from([
///   PatchOperation::set([PathSegment::Item(0)], 7_u8),
///   PatchOperation::push_item([], 9_u8),
/// ])
/// .commit(&mut items)
/// .unwrap();
/// assert_eq!(items, vec![7, 42, 9]);
///
/// let previous = rollback.previous_value(0).unwrap();
/// assert_eq!(previous.to_ref::<u8>(), Some(&23));
/// assert!(rollback.previous_value(1).is_none());
///
/// rollback.undo(&mut items).unwrap();
/// assert_eq!(items, vec![23, 42]);
/// ```
pub struct MutationRollback {
  inverses: Vec<OwnedPatchOperation>,
}

impl fmt::Debug for MutationRollback {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("MutationRollback")
      .field("inverse_count", &self.inverses.len())
      .finish()
  }
}

impl MutationRollback {
  /// Returns the value that the operation at `index` in the batch replaced or
  /// removed.
  ///
  /// Insertions, appends, moves and out-of-range indices return `None`.
  pub fn previous_value(&self, index: usize) -> Option<ObjectRef<'_>> {
    match self.inverses.get(index)? {
      OwnedPatchOperation::Set { value, .. }
      | OwnedPatchOperation::InsertKey { value, .. }
      | OwnedPatchOperation::InsertItem { value, .. }
      | OwnedPatchOperation::PushItem { value, .. } => Some(value.as_object_ref()),
      OwnedPatchOperation::RemoveKey { .. }
      | OwnedPatchOperation::RemoveItem { .. }
      | OwnedPatchOperation::MoveItem { .. } => None,
    }
  }

  /// Reverts the batch by applying each inverse operation in reverse order.
  ///
  /// Stops at the first inverse that fails and returns a [`RollbackError`].
  /// Operations with an index above [`RollbackError::inverse_index`] stay
  /// reverted, while the operation at that index and those below it stay
  /// applied.
  pub fn undo(mut self, target: &mut dyn MetaMut) -> Result<(), RollbackError> {
    while let Some(inverse) = self.inverses.pop() {
      let inverse_index = self.inverses.len();
      apply_staged_operation(target, inverse).map_err(|error| RollbackError {
        inverse_index,
        error,
      })?;
    }
    Ok(())
  }
}

/// Failure returned by [`MutationRollback::undo`].
#[derive(Debug)]
pub struct RollbackError {
  /// Zero-based position in the batch of the operation whose inverse failed.
  pub inverse_index: usize,
  /// Failure reported by the inverse operation.
  pub error: CommitOperationError,
}

/// An operation failure reported by [`MutationBatch::commit`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommitOperationError {
  /// No value exists at the requested path.
  PathNotFound {
    /// Path that could not be resolved.
    path: OwnedPath,
    /// Operation that was requested at the missing path.
    operation: PatchOperationKind,
  },
  /// The patch shape cannot be applied to the target shape.
  ShapeMismatch {
    /// Path of the incompatible value.
    path: OwnedPath,
    /// Shape required by the patch.
    expected: ValueKind,
    /// Shape found on the target.
    actual: ValueKind,
  },
  /// The patch value and target value have different concrete types.
  TypeMismatch {
    /// Path of the incompatible value.
    path: OwnedPath,
    /// Concrete type required by the patch.
    expected: TypeInfo,
    /// Concrete type found on the target.
    actual: TypeInfo,
  },
  /// An index lies outside the target sequence.
  IndexOutOfBounds {
    /// Path to the sequence.
    path: OwnedPath,
    /// Operation that received the index.
    operation: PatchOperationKind,
    /// Index that lies outside the sequence.
    index: usize,
    /// Length of the sequence.
    len: usize,
  },
  /// A move failed and could not restore the item at its source index. The
  /// sequence no longer contains the item.
  ItemLost {
    /// Path to the sequence.
    path: OwnedPath,
    /// Source index of the lost item.
    index: usize,
  },
  /// The target does not support the requested operation.
  Unsupported {
    /// Path at which the operation was requested.
    path: OwnedPath,
    /// Operation that was not supported.
    operation: PatchOperationKind,
  },
}

impl fmt::Display for CommitOperationError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::PathNotFound { operation, .. } => write!(
        f,
        "Staged mutation operation {operation} targets a path that does not exist"
      ),
      Self::ShapeMismatch {
        expected, actual, ..
      } => {
        write!(f, "Staged mutation expected {expected:?}, found {actual:?}")
      }
      Self::TypeMismatch {
        expected, actual, ..
      } => write!(
        f,
        "Staged mutation expected {}, found {}",
        expected.name(),
        actual.name()
      ),
      Self::IndexOutOfBounds {
        operation,
        index,
        len,
        ..
      } => write!(
        f,
        "Staged mutation operation {operation} received index {index} for a sequence of length {len}"
      ),
      Self::ItemLost { index, .. } => write!(
        f,
        "Staged mutation could not restore the item moved from index {index}"
      ),
      Self::Unsupported { operation, .. } => {
        write!(f, "Staged mutation operation {operation} is unsupported")
      }
    }
  }
}

impl CommitOperationError {
  /// Returns the path at which the operation failed.
  pub fn path(&self) -> &OwnedPath {
    match self {
      Self::PathNotFound { path, .. }
      | Self::ShapeMismatch { path, .. }
      | Self::TypeMismatch { path, .. }
      | Self::IndexOutOfBounds { path, .. }
      | Self::ItemLost { path, .. }
      | Self::Unsupported { path, .. } => path,
    }
  }
}

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

/// Failure returned by a staged commit.
#[derive(Debug)]
pub enum CommitError {
  /// An operation failed and all earlier successful operations were rolled
  /// back. The target retains its state from before the commit unless `error`
  /// is [`CommitOperationError::ItemLost`].
  OperationFailed {
    /// Zero-based index of the failed operation.
    index: usize,
    /// Failure reported while validating or applying the operation.
    error: CommitOperationError,
  },
  /// Rollback failed after an operation failure. This can occur when a custom
  /// [`MetaMut`] implementation cannot apply an inverse. Rollback stops at the
  /// failing inverse, so the target may be partially restored.
  RollbackFailed {
    /// The operation failure that triggered rollback.
    failed_operation: Box<CommitError>,
    /// Zero-based index of the successful operation whose rollback failed.
    operation_index: usize,
    /// Failure reported by the inverse operation.
    error: CommitOperationError,
  },
}

impl fmt::Display for CommitError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::OperationFailed { index, error, .. } => {
        write!(f, "Staged mutation operation {index} failed: {error}")
      }
      Self::RollbackFailed {
        operation_index,
        error,
        ..
      } => write!(
        f,
        "Staged mutation rollback for operation {operation_index} failed: {error}"
      ),
    }
  }
}

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

/// Ordered list of patch operations that can be applied to a mutable reflective
/// value.
///
/// Build a batch with [`MutationBatch::new`] and [`MutationBatch::push`],
/// collect it from [`PatchOperation`] values or convert an array with
/// [`MutationBatch::from`]. The batch owns its operations, so it can outlive
/// the values they borrowed.
///
/// Committing with [`MutationBatch::commit`] applies the operations in order.
/// If one fails, the batch attempts to revert the earlier ones before returning
/// the error. On success, it returns a [`MutationRollback`] that undoes the
/// whole batch.
///
/// By contrast, [`ObjectRefMut::apply`] stops at the first failure and keeps
/// the changes made before it. It records no undo information, so it is
/// cheaper when a partial update is acceptable.
///
/// # Examples
///
/// Commit a batch and undo it later:
///
/// ```
/// # use typex::{MutationBatch, PatchOperation, PathSegment};
/// let mut values = vec![1_u8, 2];
/// let rollback = MutationBatch::from([
///   PatchOperation::set([PathSegment::Item(0)], 10_u8),
///   PatchOperation::push_item([], 3_u8),
/// ])
/// .commit(&mut values)
/// .unwrap();
/// assert_eq!(values, [10, 2, 3]);
///
/// rollback.undo(&mut values).unwrap();
/// assert_eq!(values, [1, 2]);
/// ```
///
/// A failed operation attempts to revert the operations before it:
///
/// ```
/// # use typex::{MutationBatch, PatchOperation, PathSegment};
/// let mut values = vec![1_u8, 2];
/// let result = MutationBatch::from([
///   PatchOperation::set([PathSegment::Item(0)], 10_u8),
///   PatchOperation::remove_item([], 5),
/// ])
/// .commit(&mut values);
///
/// assert!(result.is_err());
/// assert_eq!(values, [1, 2]);
/// ```
#[derive(Debug, Default)]
pub struct MutationBatch {
  operations: Vec<OwnedPatchOperation>,
}

impl<'a> FromIterator<PatchOperation<'a>> for MutationBatch {
  fn from_iter<I: IntoIterator<Item = PatchOperation<'a>>>(operations: I) -> Self {
    let mut batch = Self::new();
    batch.extend(operations);
    batch
  }
}

impl<'a, const N: usize> From<[PatchOperation<'a>; N]> for MutationBatch {
  fn from(operations: [PatchOperation<'a>; N]) -> Self {
    operations.into_iter().collect()
  }
}

impl MutationBatch {
  /// Creates an empty mutation batch.
  pub fn new() -> Self {
    Self::default()
  }

  /// Returns whether no operations are staged.
  pub fn is_empty(&self) -> bool {
    self.operations.is_empty()
  }

  /// Returns the number of staged operations.
  pub fn len(&self) -> usize {
    self.operations.len()
  }

  /// Appends an operation to the batch.
  pub fn push(&mut self, operation: impl Into<OwnedPatchOperation>) -> &mut Self {
    self.operations.push(operation.into());
    self
  }

  /// Appends patch operations in iteration order.
  pub fn extend<'a, I>(&mut self, operations: I) -> &mut Self
  where
    I: IntoIterator<Item = PatchOperation<'a>>,
  {
    self
      .operations
      .extend(operations.into_iter().map(OwnedPatchOperation::from));
    self
  }

  /// Applies the staged operations to `target` in order and returns a
  /// [`MutationRollback`] that undoes them.
  ///
  /// If an operation fails, reverts the earlier operations and returns
  /// [`CommitError::OperationFailed`]. If reverting fails, returns
  /// [`CommitError::RollbackFailed`] and leaves `target` partially restored.
  #[allow(clippy::result_large_err)]
  pub fn commit(self, target: &mut dyn MetaMut) -> Result<MutationRollback, CommitError> {
    // Each inverse replaces its operation, so the rollback reuses the batch
    // allocation.
    let mut inverses = self.operations;

    for index in 0..inverses.len() {
      let placeholder = OwnedPatchOperation::MoveItem {
        path: OwnedPath::new(),
        from: 0,
        to: 0,
      };
      let operation = mem::replace(&mut inverses[index], placeholder);
      match apply_staged_operation(target, operation) {
        Ok(inverse) => inverses[index] = inverse,
        Err(error) => {
          inverses.truncate(index);
          let failed_operation = CommitError::OperationFailed { index, error };
          let rollback = MutationRollback { inverses };
          return Err(match rollback.undo(target) {
            Ok(()) => failed_operation,
            Err(RollbackError {
              inverse_index,
              error,
            }) => CommitError::RollbackFailed {
              failed_operation: Box::new(failed_operation),
              operation_index: inverse_index,
              error,
            },
          });
        }
      }
    }

    Ok(MutationRollback { inverses })
  }
}

fn resolve<'a>(
  target: &'a mut dyn MetaMut,
  path: &OwnedPath,
  operation: PatchOperationKind,
) -> Result<&'a mut dyn MetaMut, CommitOperationError> {
  resolve_path(target, path).ok_or_else(|| CommitOperationError::PathNotFound {
    path: path.clone(),
    operation,
  })
}

fn resolve_path<'a>(target: &'a mut dyn MetaMut, path: &OwnedPath) -> Option<&'a mut dyn MetaMut> {
  let mut current = target;
  for segment in path {
    current = match segment {
      PathSegment::Field(name) => current.field_mut_dyn(name)?.inner,
      PathSegment::Variant(name) => select_variant(current, name)?,
      PathSegment::Item(index) => current.item_mut_dyn(index)?.inner,
      PathSegment::Key(key) => current.key_mut_dyn(key)?.inner,
    };
  }
  Some(current)
}

/// Builds the error for a target whose [`MetaMut::reflect_mut`] does not
/// expose the `expected` shape. Returns
/// [`CommitOperationError::Unsupported`] when `actual` equals `expected` and
/// [`CommitOperationError::ShapeMismatch`] otherwise.
fn build_container_error(
  actual: ValueKind,
  path: &OwnedPath,
  operation: PatchOperationKind,
  expected: ValueKind,
) -> CommitOperationError {
  if actual == expected {
    unsupported_error(path, operation)
  } else {
    CommitOperationError::ShapeMismatch {
      path: path.clone(),
      expected,
      actual,
    }
  }
}

fn unsupported_error(path: &OwnedPath, operation: PatchOperationKind) -> CommitOperationError {
  CommitOperationError::Unsupported {
    path: path.clone(),
    operation,
  }
}

fn replacement_error(
  path: &OwnedPath,
  operation: PatchOperationKind,
  value: &Object,
  actual: TypeInfo,
) -> CommitOperationError {
  let expected = value.type_info();
  if expected != actual {
    CommitOperationError::TypeMismatch {
      path: path.clone(),
      expected,
      actual,
    }
  } else {
    unsupported_error(path, operation)
  }
}

/// Applies `operation` to `target` and returns the operation that reverts it.
fn apply_staged_operation(
  target: &mut dyn MetaMut,
  operation: OwnedPatchOperation,
) -> Result<OwnedPatchOperation, CommitOperationError> {
  match operation {
    OwnedPatchOperation::Set { path, value } => {
      let operation_kind = PatchOperationKind::Set;
      let child = resolve(target, &path, operation_kind)?;
      match child.replace_dyn(value) {
        Ok(previous) => Ok(OwnedPatchOperation::Set {
          path,
          value: previous,
        }),
        Err(value) => Err(replacement_error(
          &path,
          operation_kind,
          &value,
          child.type_info(),
        )),
      }
    }
    OwnedPatchOperation::InsertKey { path, key, value } => {
      let operation_kind = PatchOperationKind::InsertKey;
      let target = resolve(target, &path, operation_kind)?;
      let ReflectMut::Map(map) = target.reflect_mut() else {
        return Err(build_container_error(
          kind_of(target),
          &path,
          operation_kind,
          ValueKind::Map,
        ));
      };
      let Some(child) = map.key_mut(&key) else {
        map
          .insert_key(&key, value)
          .map_err(|_| unsupported_error(&path, operation_kind))?;
        return Ok(OwnedPatchOperation::RemoveKey { path, key });
      };
      match child.inner.replace_dyn(value) {
        Ok(previous) => Ok(OwnedPatchOperation::InsertKey {
          path,
          key,
          value: previous,
        }),
        Err(value) => Err(replacement_error(
          &path,
          operation_kind,
          &value,
          child.type_info(),
        )),
      }
    }
    OwnedPatchOperation::RemoveKey { path, key } => {
      let operation_kind = PatchOperationKind::RemoveKey;
      let target = resolve(target, &path, operation_kind)?;
      let ReflectMut::Map(map) = target.reflect_mut() else {
        return Err(build_container_error(
          kind_of(target),
          &path,
          operation_kind,
          ValueKind::Map,
        ));
      };
      match map.remove_key(&key) {
        Some(previous) => Ok(OwnedPatchOperation::InsertKey {
          path,
          key,
          value: previous,
        }),
        None => Err(CommitOperationError::PathNotFound {
          path,
          operation: operation_kind,
        }),
      }
    }
    OwnedPatchOperation::InsertItem { path, index, value } => {
      let operation_kind = PatchOperationKind::InsertItem;
      let target = resolve(target, &path, operation_kind)?;
      with_sequence_mut!(
        target.reflect_mut(),
        |sequence| {
          sequence
            .insert_item(index, value)
            .map_err(|_| unsupported_error(&path, operation_kind))?;
          Ok(OwnedPatchOperation::RemoveItem { path, index })
        },
        {
          Err(build_container_error(
            kind_of(target),
            &path,
            operation_kind,
            ValueKind::Sequence,
          ))
        }
      )
    }
    OwnedPatchOperation::PushItem { path, value } => {
      let operation_kind = PatchOperationKind::PushItem;
      let target = resolve(target, &path, operation_kind)?;
      with_sequence_mut!(
        target.reflect_mut(),
        |sequence| {
          let index = sequence.len();
          sequence
            .push_item(value)
            .map_err(|_| unsupported_error(&path, operation_kind))?;
          Ok(OwnedPatchOperation::RemoveItem { path, index })
        },
        {
          Err(build_container_error(
            kind_of(target),
            &path,
            operation_kind,
            ValueKind::Sequence,
          ))
        }
      )
    }
    OwnedPatchOperation::RemoveItem { path, index } => {
      let operation_kind = PatchOperationKind::RemoveItem;
      let target = resolve(target, &path, operation_kind)?;
      with_sequence_mut!(
        target.reflect_mut(),
        |sequence| {
          match sequence.remove_item(index) {
            Some(previous) => Ok(OwnedPatchOperation::InsertItem {
              path,
              index,
              value: previous,
            }),
            None => Err(unsupported_error(&path, operation_kind)),
          }
        },
        {
          Err(build_container_error(
            kind_of(target),
            &path,
            operation_kind,
            ValueKind::Sequence,
          ))
        }
      )
    }
    OwnedPatchOperation::MoveItem { path, from, to } => {
      let operation_kind = PatchOperationKind::MoveItem;
      let target = resolve(target, &path, operation_kind)?;
      with_sequence_mut!(
        target.reflect_mut(),
        |sequence| {
          let len = sequence.len();
          if let Some(index) = move_index_out_of_bounds(from, to, len) {
            return Err(CommitOperationError::IndexOutOfBounds {
              path,
              operation: operation_kind,
              index,
              len,
            });
          }
          sequence.move_item(from, to).map_err(|error| match error {
            MoveItemError::RestoreFailed => CommitOperationError::ItemLost {
              path: path.clone(),
              index: from,
            },
            _ => unsupported_error(&path, operation_kind),
          })?;
          let (from, to) = if from < to {
            (to - 1, from)
          } else {
            (to, from + 1)
          };
          Ok(OwnedPatchOperation::MoveItem { path, from, to })
        },
        {
          Err(build_container_error(
            kind_of(target),
            &path,
            operation_kind,
            ValueKind::Sequence,
          ))
        }
      )
    }
  }
}