Make DataModel class name a shared constant for safety

This commit is contained in:
Filip Tibell 2023-03-22 11:29:11 +01:00
parent b0d401421e
commit 1441544426
No known key found for this signature in database
3 changed files with 16 additions and 14 deletions

View file

@ -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<T> = Result<T, DocumentError>;
@ -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<Self> {
if instance.get_class_name() != "DataModel" {
pub fn from_data_model_instance(i: Instance) -> DocumentResult<Self> {
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<Instance>) -> DocumentResult<Self> {
for instance in &instances {
if instance.get_class_name() == "DataModel" {
pub fn from_instance_array(v: Vec<Instance>) -> DocumentResult<Self> {
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);
}

View file

@ -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(()),
);

View file

@ -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<WeakDom> =
@ -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