From 14415444261761121e855aa13d5280e533a5567b Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 22 Mar 2023 11:29:11 +0100 Subject: [PATCH] Make DataModel class name a shared constant for safety --- packages/lib-roblox/src/document/mod.rs | 18 +++++++++--------- packages/lib-roblox/src/instance/data_model.rs | 6 ++++-- packages/lib-roblox/src/instance/mod.rs | 6 +++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/lib-roblox/src/document/mod.rs b/packages/lib-roblox/src/document/mod.rs index 81e7248..31857a2 100644 --- a/packages/lib-roblox/src/document/mod.rs +++ b/packages/lib-roblox/src/document/mod.rs @@ -12,7 +12,7 @@ pub use error::*; pub use format::*; pub use kind::*; -use crate::instance::Instance; +use crate::instance::{data_model, Instance}; pub type DocumentResult = Result; @@ -196,7 +196,7 @@ impl Document { let data_model_ref = self .dom - .insert(dom_root, DomInstanceBuilder::new("DataModel")); + .insert(dom_root, DomInstanceBuilder::new(data_model::CLASS_NAME)); let data_model_child_refs = self.dom.root().children().to_vec(); for child_ref in data_model_child_refs { @@ -233,14 +233,14 @@ impl Document { Will error if the instance is not a DataModel. */ - pub fn from_data_model_instance(instance: Instance) -> DocumentResult { - if instance.get_class_name() != "DataModel" { + pub fn from_data_model_instance(i: Instance) -> DocumentResult { + if i.get_class_name() != data_model::CLASS_NAME { return Err(DocumentError::FromDataModelInvalidArgs); } let mut dom = WeakDom::new(DomInstanceBuilder::new("ROOT")); - for data_model_child in instance.get_children() { + for data_model_child in i.get_children() { data_model_child.into_external_dom(&mut dom); } @@ -256,16 +256,16 @@ impl Document { Will error if any of the instances is a DataModel. */ - pub fn from_instance_array(instances: Vec) -> DocumentResult { - for instance in &instances { - if instance.get_class_name() == "DataModel" { + pub fn from_instance_array(v: Vec) -> DocumentResult { + for i in &v { + if i.get_class_name() == data_model::CLASS_NAME { return Err(DocumentError::FromInstanceArrayInvalidArgs); } } let mut dom = WeakDom::new(DomInstanceBuilder::new("ROOT")); - for instance in instances { + for instance in v { instance.into_external_dom(&mut dom); } diff --git a/packages/lib-roblox/src/instance/data_model.rs b/packages/lib-roblox/src/instance/data_model.rs index 8d00422..e057349 100644 --- a/packages/lib-roblox/src/instance/data_model.rs +++ b/packages/lib-roblox/src/instance/data_model.rs @@ -4,16 +4,18 @@ use crate::shared::classes::add_class_restricted_method; use super::Instance; +pub const CLASS_NAME: &str = "DataModel"; + pub fn add_methods<'lua, M: LuaUserDataMethods<'lua, Instance>>(methods: &mut M) { add_class_restricted_method( methods, - "DataModel", + CLASS_NAME, "GetService", |_, _, _service_name: String| Ok(()), ); add_class_restricted_method( methods, - "DataModel", + CLASS_NAME, "FindService", |_, _, _service_name: String| Ok(()), ); diff --git a/packages/lib-roblox/src/instance/mod.rs b/packages/lib-roblox/src/instance/mod.rs index 6c48318..2908cfc 100644 --- a/packages/lib-roblox/src/instance/mod.rs +++ b/packages/lib-roblox/src/instance/mod.rs @@ -15,7 +15,7 @@ use crate::{ shared::instance::{class_exists, class_is_a, find_property_info}, }; -mod data_model; +pub(crate) mod data_model; lazy_static::lazy_static! { static ref INTERNAL_DOM: RwLock = @@ -468,7 +468,7 @@ impl Instance { let mut instance_ref = self.dom_ref; while let Some(instance) = dom.get_by_ref(instance_ref) { - if instance_ref != dom_root && instance.class != "DataModel" { + if instance_ref != dom_root && instance.class != data_model::CLASS_NAME { instance_ref = instance.parent(); parts.push(instance.name.clone()); } else { @@ -702,7 +702,7 @@ impl LuaUserData for Instance { return Ok(()); } "Parent" => { - if this.get_class_name() == "DataModel" { + if this.get_class_name() == data_model::CLASS_NAME { return Err(LuaError::RuntimeError(format!( "Failed to set property '{}' - DataModel can not be reparented", prop_name