rbx_tree/instance.rs
1use std::collections::HashMap;
2
3use serde_derive::{Serialize, Deserialize};
4
5use crate::{
6 id::RbxId,
7 value::RbxValue,
8};
9
10/// The properties associated with a Roblox Instance that might not exist yet.
11///
12/// To construct a real instance with an ID and children, insert an
13/// `RbxInstanceProperties` object into an existing [`RbxTree`] with
14/// [`RbxTree::insert_instance`] or by creating a new tree with it as the root
15/// using [`RbxTree::new`].
16///
17/// [`RbxTree`]: struct.RbxTree.html
18/// [`RbxTree::insert_instance`]: struct.RbxTree.html#method.insert_instance
19/// [`RbxTree::new`]: struct.RbxTree.html#method.new
20#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
21#[serde(rename_all = "PascalCase")]
22pub struct RbxInstanceProperties {
23 /// Maps to the `Name` property on Instance.
24 pub name: String,
25
26 /// Maps to the `ClassName` property on Instance.
27 pub class_name: String,
28
29 /// Contains all other properties of the Instance.
30 pub properties: HashMap<String, RbxValue>,
31}
32
33/// Represents an instance that is rooted in an [`RbxTree`]. These are always
34/// returned from an existing [`RbxTree`] with a method like
35/// [`RbxTree::get_instance`].
36///
37/// `RbxInstance` derefs to `RbxInstanceProperties` to make accessing properties
38/// easier.
39///
40/// [`RbxTree`]: struct.RbxTree.html
41/// [`RbxTree::get_instance`]: struct.RbxTree.html#method.get_instance
42#[derive(Debug, Serialize, Deserialize, PartialEq)]
43#[serde(rename_all = "PascalCase")]
44pub struct RbxInstance {
45 #[serde(flatten)]
46 properties: RbxInstanceProperties,
47
48 /// The unique ID of the instance
49 id: RbxId,
50
51 /// All of the children of this instance. Order is relevant to preserve!
52 pub(crate) children: Vec<RbxId>,
53
54 /// The parent of the instance, if there is one.
55 pub(crate) parent: Option<RbxId>,
56}
57
58impl RbxInstance {
59 pub(crate) fn new(properties: RbxInstanceProperties) -> RbxInstance {
60 RbxInstance {
61 properties,
62 id: RbxId::new(),
63 parent: None,
64 children: Vec::new(),
65 }
66 }
67
68 /// Returns the unique ID associated with this instance.
69 pub fn get_id(&self) -> RbxId {
70 self.id
71 }
72
73 /// Returns the ID of the parent of this instance, if it has a parent.
74 pub fn get_parent_id(&self) -> Option<RbxId> {
75 self.parent
76 }
77
78 /// Returns a list of the IDs of the children of this instance.
79 pub fn get_children_ids(&self) -> &[RbxId] {
80 &self.children
81 }
82}
83
84impl Clone for RbxInstance {
85 fn clone(&self) -> Self {
86 unimplemented!()
87 }
88}
89
90impl std::ops::Deref for RbxInstance {
91 type Target = RbxInstanceProperties;
92
93 fn deref(&self) -> &Self::Target {
94 &self.properties
95 }
96}
97
98impl std::ops::DerefMut for RbxInstance {
99 fn deref_mut(&mut self) -> &mut Self::Target {
100 &mut self.properties
101 }
102}