instance_snapshot.rs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //! Defines the structure of an instance snapshot.
  2. use std::{borrow::Cow, collections::HashMap};
  3. use rbx_dom_weak::{RbxId, RbxTree, RbxValue};
  4. use super::InstanceMetadata;
  5. /// A lightweight description of what an instance should look like. Attempts to
  6. /// be somewhat memory efficient by borrowing from its source data, indicated by
  7. /// the lifetime parameter `'source`.
  8. ///
  9. // Possible future improvements:
  10. // - Use refcounted/interned strings
  11. // - Replace use of RbxValue with a sum of RbxValue + borrowed value
  12. #[derive(Debug, Clone, PartialEq)]
  13. pub struct InstanceSnapshot<'source> {
  14. /// A temporary ID applied to the snapshot that's used for Ref properties.
  15. pub snapshot_id: Option<RbxId>,
  16. /// Rojo-specific metadata associated with the instance.
  17. pub metadata: InstanceMetadata,
  18. /// Correpsonds to the Name property of the instance.
  19. pub name: Cow<'source, str>,
  20. /// Corresponds to the ClassName property of the instance.
  21. pub class_name: Cow<'source, str>,
  22. /// All other properties of the instance, weakly-typed.
  23. pub properties: HashMap<String, RbxValue>,
  24. /// The children of the instance represented as more snapshots.
  25. ///
  26. /// Order is relevant for Roblox instances!
  27. pub children: Vec<InstanceSnapshot<'source>>,
  28. }
  29. impl<'source> InstanceSnapshot<'source> {
  30. pub fn get_owned(&'source self) -> InstanceSnapshot<'static> {
  31. let children: Vec<InstanceSnapshot<'static>> = self
  32. .children
  33. .iter()
  34. .map(InstanceSnapshot::get_owned)
  35. .collect();
  36. InstanceSnapshot {
  37. snapshot_id: None,
  38. metadata: self.metadata.clone(),
  39. name: Cow::Owned(self.name.clone().into_owned()),
  40. class_name: Cow::Owned(self.class_name.clone().into_owned()),
  41. properties: self.properties.clone(),
  42. children,
  43. }
  44. }
  45. pub fn from_tree(tree: &RbxTree, id: RbxId) -> InstanceSnapshot<'static> {
  46. let instance = tree
  47. .get_instance(id)
  48. .expect("instance did not exist in tree");
  49. let children = instance
  50. .get_children_ids()
  51. .iter()
  52. .cloned()
  53. .map(|id| InstanceSnapshot::from_tree(tree, id))
  54. .collect();
  55. InstanceSnapshot {
  56. snapshot_id: Some(id),
  57. metadata: InstanceMetadata::default(),
  58. name: Cow::Owned(instance.name.clone()),
  59. class_name: Cow::Owned(instance.class_name.clone()),
  60. properties: instance.properties.clone(),
  61. children,
  62. }
  63. }
  64. }
  65. impl<'source> Default for InstanceSnapshot<'source> {
  66. fn default() -> InstanceSnapshot<'source> {
  67. InstanceSnapshot {
  68. snapshot_id: None,
  69. metadata: InstanceMetadata::default(),
  70. name: Cow::Borrowed("DEFAULT"),
  71. class_name: Cow::Borrowed("DEFAULT"),
  72. properties: HashMap::new(),
  73. children: Vec::new(),
  74. }
  75. }
  76. }