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
pub mod myers;

use crate::{r#type::Field, r#type::Type};

use self::myers::Change;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FieldEditKind {
    ChangedTyped,
    RenamedField,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FieldDiff {
    Insert {
        index: usize,
        new_type: Type,
    },
    Edit {
        old_type: Type,
        new_type: Type,
        old_index: Option<usize>,
        new_index: usize,
        kind: FieldEditKind,
    },
    Move {
        ty: Type,
        old_index: usize,
        new_index: usize,
    },
    Delete {
        index: usize,
    },
}

/// The difference between an old and new ordered set of structs.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StructDiff {
    /// The struct was newly inserted
    Insert { index: usize, ty: Type },
    /// An existing struct was modified
    Edit {
        diff: Vec<FieldDiff>,
        old_index: usize,
        new_index: usize,
        old_ty: Type,
        new_ty: Type,
    },
    /// An existing struct was moved to another position
    Move {
        old_index: usize,
        new_index: usize,
        old_ty: Type,
        new_ty: Type,
    },
    /// An existing struct was deleted
    Delete { index: usize, ty: Type },
}

impl Ord for StructDiff {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        fn get_index(diff: &StructDiff) -> usize {
            match diff {
                StructDiff::Insert { index, .. }
                | StructDiff::Edit {
                    old_index: index, ..
                }
                | StructDiff::Move {
                    old_index: index, ..
                }
                | StructDiff::Delete { index, .. } => *index,
            }
        }

        get_index(self).cmp(&get_index(other))
    }
}

impl PartialOrd for StructDiff {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// Given an `old` and a `new` ordered set of types, computes the difference based on ordering and equality of struct types.
/// Thus, a diff can consist of inserted, deleted, moved, and edited (i.e. fields of) struct types.
pub fn compute_struct_diff(old: &[Type], new: &[Type]) -> Vec<StructDiff> {
    let diff = myers::compute_diff(old, new);
    let (deletions, insertions) = myers::split_diff(&diff);

    let deleted_structs = deletions
        .into_iter()
        .filter(|Change { element, .. }| element.is_struct())
        .collect();

    let inserted_structs = insertions
        .into_iter()
        .filter(|Change { element, .. }| element.is_struct())
        .collect();

    let mut mapping: Vec<StructDiff> = Vec::with_capacity(diff.len());
    append_struct_mapping(deleted_structs, inserted_structs, &mut mapping);

    mapping.shrink_to_fit();
    // Sort to guarantee order of execution when deleting and/or inserting
    mapping.sort();
    mapping
}

/// A helper struct to check equality between fields.
#[derive(Clone, Eq, PartialEq)]
struct UniqueFieldInfo<'a> {
    name: &'a str,
    ty: Type,
}

impl<'a> From<Field<'a>> for UniqueFieldInfo<'a> {
    fn from(other: Field<'a>) -> Self {
        Self {
            name: other.name(),
            ty: other.ty(),
        }
    }
}

/// Given a set of indices for `deletions` from the `old` slice of types and a set of indices
/// for `insertions` into the `new` slice of types, appends the corresponding `Diff` mapping
/// for all
fn append_struct_mapping(
    deletions: Vec<Change<Type>>,
    insertions: Vec<Change<Type>>,
    mapping: &mut Vec<StructDiff>,
) {
    let deletions: Vec<_> = deletions
        .iter()
        .enumerate()
        .map(|(deletion_index, Change { index, element })| {
            let fields = element
                .as_struct()
                .map(|s| s.fields().iter().map(UniqueFieldInfo::from).collect())
                .unwrap_or_else(Vec::new);

            (deletion_index, *index, element.clone(), fields)
        })
        .collect();

    let insertions: Vec<_> = insertions
        .iter()
        .enumerate()
        .map(|(insertion_index, Change { index, element })| {
            let fields = element
                .as_struct()
                .map(|s| s.fields().iter().map(UniqueFieldInfo::from).collect())
                .unwrap_or_else(Vec::new);

            (insertion_index, *index, element.clone(), fields)
        })
        .collect();

    struct LengthDescription<'f> {
        deletion_idx: usize,
        insertion_idx: usize,
        old_index: usize,
        new_index: usize,
        old_ty: Type,
        new_ty: Type,
        old_fields: &'f Vec<UniqueFieldInfo<'f>>,
        new_fields: &'f Vec<UniqueFieldInfo<'f>>,
        length: usize,
    }

    // For all (insertion, deletion) pairs, calculate their `myers::diff_length`
    let mut myers_lengths: Vec<_> = insertions
        .iter()
        .flat_map(|(insertion_idx, new_idx, new_ty, new_fields)| {
            deletions
                .iter()
                .filter_map(|(deletion_idx, old_idx, old_ty, old_fields)| {
                    let length = myers::diff_length(old_fields, new_fields);

                    // Given N old fields and M new fields, the smallest set capable of
                    // completely changing a struct is N + M.
                    // E.g.
                    // old: [("a", Foo)]
                    // new: [("b", Bar), "c", Baz]
                    // `old` can be converted to `new` in 3 steps: 1 deletion + 2 insertions
                    // let min = new_fields.len() + old_fields.len();

                    // If the type's name is equal
                    if old_ty.name() == new_ty.name() || length == 0 {
                        // TODO: Potentially we want to retain an X% for types with equal names,
                        // whilst allowing types with different names to be modified for up to Y%.
                        Some(LengthDescription {
                            deletion_idx: *deletion_idx,
                            insertion_idx: *insertion_idx,
                            old_index: *old_idx,
                            new_index: *new_idx,
                            old_ty: old_ty.clone(),
                            new_ty: new_ty.clone(),
                            old_fields,
                            new_fields,
                            length,
                        })
                    } else {
                        // Indicate that the respective two fields are too different.
                        None
                    }
                })
                .collect::<Vec<LengthDescription>>()
        })
        .collect();

    // Sort in ascending order of their `myers::diff_length`.
    myers_lengths.sort_by(
        |LengthDescription { length: lhs, .. }, LengthDescription { length: rhs, .. }| lhs.cmp(rhs),
    );

    let mut used_deletions = vec![false; deletions.len()];
    let mut used_insertions = vec![false; insertions.len()];
    for LengthDescription {
        deletion_idx,
        insertion_idx,
        old_index,
        new_index,
        old_ty,
        new_ty,
        length,
        old_fields,
        new_fields,
    } in myers_lengths
    {
        // Skip marked fields
        if used_deletions[deletion_idx] || used_insertions[insertion_idx] {
            continue;
        }

        used_deletions[deletion_idx] = true;
        used_insertions[insertion_idx] = true;

        // If there is no difference between the old and new fields
        mapping.push(if length == 0 {
            // Move the struct
            StructDiff::Move {
                old_index,
                new_index,
                old_ty,
                new_ty,
            }
        } else {
            // ASSUMPTION: Don't use recursion, because all types are individually checked for
            // differences.
            // TODO: Support value struct vs heap struct?
            let diff = field_diff(old_fields, new_fields);

            // Edit the struct, potentially moving it in the process.
            StructDiff::Edit {
                diff,
                old_index,
                new_index,
                old_ty,
                new_ty,
            }
        });
    }

    // Any remaining unused deletions must have been deleted.
    used_deletions
        .into_iter()
        .zip(deletions.into_iter())
        .for_each(|(used, (_, old_index, ty, _))| {
            if !used {
                mapping.push(StructDiff::Delete {
                    index: old_index,
                    ty,
                });
            }
        });

    // Any remaining unused insertions must have been inserted.
    used_insertions
        .into_iter()
        .zip(insertions.into_iter())
        .for_each(|(used, (_, new_index, ty, _))| {
            if !used {
                mapping.push(StructDiff::Insert {
                    index: new_index,
                    ty,
                });
            }
        });
}

/// Given an `old` and a `new` set of fields, calculates the difference.
fn field_diff<'a, 'b>(old: &[UniqueFieldInfo<'a>], new: &[UniqueFieldInfo<'b>]) -> Vec<FieldDiff> {
    let diff = myers::compute_diff(old, new);
    let (deletions, insertions) = myers::split_diff(&diff);
    let mut insertions: Vec<Option<Change<UniqueFieldInfo>>> =
        insertions.into_iter().map(Some).collect();

    let mut mapping = Vec::with_capacity(diff.len());
    // For all deletions,
    #[allow(clippy::manual_flatten)]
    'outer: for Change {
        index: old_index,
        element: old_field,
    } in deletions
    {
        // is there an insertion with the same field name and type `T`?
        for insertion in insertions.iter_mut() {
            if let Some(Change {
                index: new_index,
                element: new_field,
            }) = insertion
            {
                if old_field == *new_field {
                    // If so, move it.
                    mapping.push(FieldDiff::Move {
                        ty: old_field.ty,
                        old_index,
                        new_index: *new_index,
                    });
                    *insertion = None;
                    continue 'outer;
                }
            }
        }
        // Else, is there an insertion with the same field name but different type `T`?
        for insertion in insertions.iter_mut() {
            if let Some(Change {
                index: new_index,
                element: new_field,
            }) = insertion
            {
                if old_field.name == new_field.name {
                    // If so,
                    mapping.push({
                        // convert the type in-place.
                        FieldDiff::Edit {
                            old_type: old_field.ty,
                            new_type: new_field.ty.clone(),
                            old_index: if old_index != *new_index {
                                Some(old_index)
                            } else {
                                None
                            },
                            new_index: *new_index,
                            kind: FieldEditKind::ChangedTyped,
                        }
                    });
                    *insertion = None;
                    continue 'outer;
                }
            }
        }
        // Else, is there an insertion with a different name but same type?
        // As there can be multiple fields with the same type, we want to find the closest one.
        let mut closest = None;
        for (insert_index, insertion) in insertions.iter_mut().enumerate() {
            if let Some(Change {
                index: new_index,
                element: new_field,
            }) = insertion
            {
                if old_field.ty == new_field.ty {
                    let diff = old_index.max(*new_index) - old_index.min(*new_index);
                    // If so, select the closest candidate.
                    if let Some((closest_insert_index, closest_index, closest_ty, closest_diff)) =
                        &mut closest
                    {
                        if diff < *closest_diff {
                            *closest_insert_index = insert_index;
                            *closest_index = *new_index;
                            *closest_ty = new_field.ty.clone();
                            *closest_diff = diff;
                        }
                    } else {
                        closest = Some((insert_index, *new_index, new_field.ty.clone(), diff));
                    }

                    // Terminate early if we managed to find the optimal solution (i.e. the field's
                    // position did not change).
                    if diff == 0 {
                        break;
                    }
                }
            }
        }
        // If there is one, use the closest match
        if let Some((closest_insert_index, closest_index, closest_type, _)) = closest {
            // Remove the insertion
            insertions
                .get_mut(closest_insert_index)
                .expect("Closest index must be within insertions")
                .take();

            mapping.push({
                // rename the field in-place.
                FieldDiff::Edit {
                    old_type: old_field.ty.clone(),
                    new_type: closest_type,
                    old_index: if old_index != closest_index {
                        Some(old_index)
                    } else {
                        None
                    },
                    new_index: closest_index,
                    kind: FieldEditKind::RenamedField,
                }
            });
            continue 'outer;
        }
        // If not, delete the field.
        mapping.push(FieldDiff::Delete { index: old_index })
    }

    // If an insertion did not have a matching deletion, then insert it.
    for Change {
        index,
        element: new_field,
    } in insertions.into_iter().flatten()
    {
        mapping.push(FieldDiff::Insert {
            index,
            new_type: new_field.ty,
        });
    }

    mapping.shrink_to_fit();
    mapping
}