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
use std::collections::HashMap;

use serde_derive::{Deserialize, Serialize};

use crate::{
    id::RbxId,
    instance::{RbxInstance, RbxInstanceProperties},
};

/// Represents a tree containing Roblox instances.
///
/// Instances are described by [RbxInstance](struct.RbxInstance.html) objects
/// and have an ID, children, and a parent.
///
/// When constructing instances, you'll want to create
/// [RbxInstanceProperties](struct.RbxInstanceProperties.html) objects and
/// insert them into the tree.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RbxTree {
    instances: HashMap<RbxId, RbxInstance>,
    root_id: RbxId,
}

impl RbxTree {
    /// Construct a new `RbxTree` with its root instance constructed using the
    /// given properties.
    pub fn new(root_properties: RbxInstanceProperties) -> RbxTree {
        let rooted_root = RbxInstance::new(root_properties);
        let root_id = rooted_root.get_id();

        let mut instances = HashMap::new();
        instances.insert(root_id, rooted_root);

        RbxTree { instances, root_id }
    }

    /// Returns the ID of the root instance in the tree, which can be used
    /// alongside `get_instance` and friends.
    pub fn get_root_id(&self) -> RbxId {
        self.root_id
    }

    /// Returns an iterator over all IDs in the tree.
    pub fn iter_all_ids(&self) -> impl Iterator<Item = RbxId> + '_ {
        self.instances.keys().cloned()
    }

    /// Returns the instance with the given ID if it's contained in this tree.
    pub fn get_instance(&self, id: RbxId) -> Option<&RbxInstance> {
        self.instances.get(&id)
    }

    /// Returns mutable access to the instance with the given ID if it's
    /// contained in this tree.
    pub fn get_instance_mut(&mut self, id: RbxId) -> Option<&mut RbxInstance> {
        self.instances.get_mut(&id)
    }

    /// Move the instance with the given ID from this tree to a new tree,
    /// underneath the given parent instance ID.
    ///
    /// ## Panics
    /// Panics if the instance `source_id` doesn't exist in the source tree or
    /// if the instance `dest_parent_id` doesn't exist in the destination tree.
    pub fn move_instance(
        &mut self,
        source_id: RbxId,
        dest_tree: &mut RbxTree,
        dest_parent_id: RbxId,
    ) {
        self.orphan_instance(source_id);

        // Remove the instance we're trying to move and manually rewrite its
        // parent.
        let mut root_instance = self
            .instances
            .remove(&source_id)
            .expect("Cannot move an instance that does not exist in the tree");
        root_instance.parent = Some(dest_parent_id);

        let mut to_visit = root_instance.children.clone();

        dest_tree.insert_internal_and_unorphan(root_instance);

        // We can move children in whatever order since we aren't touching their
        // children tables
        while let Some(id) = to_visit.pop() {
            let instance = self.instances.remove(&id).unwrap();
            to_visit.extend_from_slice(&instance.children);

            dest_tree.instances.insert(instance.get_id(), instance);
        }
    }

    /// Move the instance with the ID `id` so that its new parent is
    /// `dest_parent_id`.
    ///
    /// ## Panics
    /// Panics if `id` or `dest_parent_id` do not refer to instances that exist
    /// in the tree.
    ///
    /// Panics if this operation would cause the tree to become cyclical and
    /// invalid.
    pub fn set_parent(&mut self, id: RbxId, dest_parent_id: RbxId) {
        for instance in self.descendants(id) {
            if instance.get_id() == dest_parent_id {
                panic!("set_parent cannot create circular references");
            }
        }

        self.orphan_instance(id);
        self.unorphan_instance(id, dest_parent_id);
    }

    /// Inserts a new instance with the given properties into the tree, putting it
    /// under the instance with the given ID.
    ///
    /// ## Panics
    /// Panics if the given ID does not refer to an instance in this tree.
    pub fn insert_instance(
        &mut self,
        properties: RbxInstanceProperties,
        parent_id: RbxId,
    ) -> RbxId {
        let mut tree_instance = RbxInstance::new(properties);
        tree_instance.parent = Some(parent_id);

        let id = tree_instance.get_id();

        self.insert_internal_and_unorphan(tree_instance);

        id
    }

    /// Given an ID, remove the instance from the tree with that ID, along with
    /// all of its descendants.
    pub fn remove_instance(&mut self, root_id: RbxId) -> Option<RbxTree> {
        if self.root_id == root_id {
            panic!("Cannot remove root ID from tree!");
        }

        self.orphan_instance(root_id);

        let mut ids_to_visit = vec![root_id];
        let mut new_tree_instances = HashMap::new();

        while let Some(id) = ids_to_visit.pop() {
            match self.instances.get(&id) {
                Some(instance) => ids_to_visit.extend_from_slice(&instance.children),
                None => continue,
            }

            let instance = self.instances.remove(&id).unwrap();
            new_tree_instances.insert(id, instance);
        }

        Some(RbxTree {
            instances: new_tree_instances,
            root_id,
        })
    }

    /// Returns an iterator over all of the descendants of the given instance by
    /// ID.
    ///
    /// ## Panics
    /// Panics if the given ID is not present in the tree.
    pub fn descendants(&self, id: RbxId) -> Descendants<'_> {
        let instance = self
            .get_instance(id)
            .expect("Cannot enumerate descendants of an instance not in the tree");

        Descendants {
            tree: self,
            ids_to_visit: instance.get_children_ids().to_vec(),
        }
    }

    /// Unlinks the parent->child link for the given ID, effectively making it
    /// an orphan in the tree.
    ///
    /// The instance will still refer to its parent by ID, so any method calling
    /// orphan_instance will need to make additional changes to preserve
    /// RbxTree's invariants.
    ///
    /// # Panics
    /// Panics if the given instance does not exist, does not have a parent, or
    /// if any RbxTree variants were violated.
    fn orphan_instance(&mut self, orphan_id: RbxId) {
        let parent_id = self
            .instances
            .get(&orphan_id)
            .expect("Cannot orphan an instance that does not exist in the tree")
            .get_parent_id()
            .expect("Cannot orphan an instance without a parent, like the root instance");

        let parent = self
            .get_instance_mut(parent_id)
            .expect("Instance referred to an ID that does not exist");

        parent.children.retain(|&id| id != orphan_id);
    }

    /// Inserts a fully-constructed instance into this tree's instance table and
    /// links it to the parent given by its parent ID field.
    ///
    /// # Panics
    /// Panics if the instance has a None parent or if the parent it refers to
    /// does not exist in this tree.
    fn insert_internal_and_unorphan(&mut self, instance: RbxInstance) {
        let id = instance.get_id();
        let parent_id = instance
            .parent
            .expect("Cannot use insert_internal_and_unorphan on instances with no parent");

        self.instances.insert(instance.get_id(), instance);
        self.unorphan_instance(id, parent_id);
    }

    fn unorphan_instance(&mut self, id: RbxId, parent_id: RbxId) {
        {
            let instance = self
                .instances
                .get_mut(&id)
                .expect("Cannot unorphan and instance not in this tree");

            instance.parent = Some(parent_id);
        }

        let parent = self
            .instances
            .get_mut(&parent_id)
            .expect("Cannot unorphan into an instance not in this tree");

        parent.children.push(id);
    }
}

/// An iterator over all descendants of an instance in an [`RbxTree`]. Returned
/// by [`RbxTree::descendants`].
///
/// [`RbxTree`]: struct.RbxTree.html
/// [`RbxTree::descendants`]: struct.RbxTree.html#method.descendants
pub struct Descendants<'a> {
    tree: &'a RbxTree,
    ids_to_visit: Vec<RbxId>,
}

impl<'a> Iterator for Descendants<'a> {
    type Item = &'a RbxInstance;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(id) = self.ids_to_visit.pop() {
            if let Some(instance) = self.tree.get_instance(id) {
                for child_id in &instance.children {
                    self.ids_to_visit.push(*child_id);
                }

                return Some(instance);
            }
        }

        None
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use std::collections::HashSet;

    #[test]
    fn descendants() {
        let mut tree = RbxTree::new(RbxInstanceProperties {
            name: "Place 1".to_owned(),
            class_name: "DataModel".to_owned(),
            properties: HashMap::new(),
        });

        let root_id = tree.get_root_id();

        let a_id = tree.insert_instance(
            RbxInstanceProperties {
                name: "A".to_owned(),
                class_name: "Folder".to_owned(),
                properties: HashMap::new(),
            },
            root_id,
        );

        let b_id = tree.insert_instance(
            RbxInstanceProperties {
                name: "B".to_owned(),
                class_name: "Folder".to_owned(),
                properties: HashMap::new(),
            },
            root_id,
        );

        let c_id = tree.insert_instance(
            RbxInstanceProperties {
                name: "C".to_owned(),
                class_name: "Folder".to_owned(),
                properties: HashMap::new(),
            },
            b_id,
        );

        let mut seen_ids = HashSet::new();

        for instance in tree.descendants(root_id) {
            assert!(seen_ids.insert(instance.get_id()));
        }

        assert_eq!(seen_ids.len(), 3);
        assert!(seen_ids.contains(&a_id));
        assert!(seen_ids.contains(&b_id));
        assert!(seen_ids.contains(&c_id));
    }

    #[test]
    fn move_instances() {
        let mut source_tree = RbxTree::new(RbxInstanceProperties {
            name: "Place 1".to_owned(),
            class_name: "DataModel".to_owned(),
            properties: HashMap::new(),
        });

        let source_root_id = source_tree.get_root_id();

        let a_id = source_tree.insert_instance(
            RbxInstanceProperties {
                name: "A".to_owned(),
                class_name: "Folder".to_owned(),
                properties: HashMap::new(),
            },
            source_root_id,
        );

        let b_id = source_tree.insert_instance(
            RbxInstanceProperties {
                name: "B".to_owned(),
                class_name: "Folder".to_owned(),
                properties: HashMap::new(),
            },
            a_id,
        );

        let c_id = source_tree.insert_instance(
            RbxInstanceProperties {
                name: "C".to_owned(),
                class_name: "Folder".to_owned(),
                properties: HashMap::new(),
            },
            a_id,
        );

        let mut dest_tree = RbxTree::new(RbxInstanceProperties {
            name: "Place 2".to_owned(),
            class_name: "DataModel".to_owned(),
            properties: HashMap::new(),
        });

        let dest_root_id = dest_tree.get_root_id();

        source_tree.move_instance(a_id, &mut dest_tree, dest_root_id);

        assert!(source_tree.get_instance(a_id).is_none());
        assert!(source_tree.get_instance(b_id).is_none());
        assert!(source_tree.get_instance(c_id).is_none());
        assert_eq!(
            source_tree
                .get_instance(source_root_id)
                .unwrap()
                .get_children_ids()
                .len(),
            0
        );

        assert!(dest_tree.get_instance(a_id).is_some());
        assert!(dest_tree.get_instance(b_id).is_some());
        assert!(dest_tree.get_instance(c_id).is_some());
        assert_eq!(
            dest_tree
                .get_instance(dest_root_id)
                .unwrap()
                .get_children_ids()
                .len(),
            1
        );
        assert_eq!(
            dest_tree.get_instance(a_id).unwrap().get_children_ids(),
            &[b_id, c_id]
        );
    }

    #[test]
    fn set_parent() {
        let mut tree = RbxTree::new(RbxInstanceProperties {
            name: "Place 1".to_owned(),
            class_name: "DataModel".to_owned(),
            properties: HashMap::new(),
        });

        let root_id = tree.get_root_id();

        let a_id = tree.insert_instance(
            RbxInstanceProperties {
                name: "A".to_owned(),
                class_name: "A".to_owned(),
                properties: HashMap::new(),
            },
            root_id,
        );

        let b_id = tree.insert_instance(
            RbxInstanceProperties {
                name: "B".to_owned(),
                class_name: "B".to_owned(),
                properties: HashMap::new(),
            },
            root_id,
        );

        tree.set_parent(a_id, b_id);

        let a = tree.get_instance(a_id).unwrap();
        assert_eq!(a.get_parent_id(), Some(b_id));
    }
}