Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rbx_reflection: Superclass Iterator v2 #453

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions rbx_reflection/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ pub struct ReflectionDatabase<'a> {

pub struct SuperClassIter<'a> {
database: &'a ReflectionDatabase<'a>,
descriptor: Option<&'a ClassDescriptor<'a>>,
}
impl<'a> SuperClassIter<'a> {
fn next_descriptor(&self) -> Option<&'a ClassDescriptor<'a>> {
let superclass = self.descriptor?.superclass.as_ref()?;
self.database.classes.get(superclass)
}
class_name: &'a str,
}
impl<'a> Iterator for SuperClassIter<'a> {
type Item = &'a ClassDescriptor<'a>;
fn next(&mut self) -> Option<Self::Item> {
let next_descriptor = self.next_descriptor();
std::mem::replace(&mut self.descriptor, next_descriptor)
let next_descriptor = self.database.classes.get(self.class_name)?;
// abuse empty string to create a non-class
self.class_name = next_descriptor.superclass.as_deref().unwrap_or("");
Some(next_descriptor)
}
}

Expand Down Expand Up @@ -79,13 +75,20 @@ impl<'a> ReflectionDatabase<'a> {

/// Returns an iterator of superclasses for the provided ClassDescriptor. This
/// iterator will start with the provided class and end with `Instance`.
pub fn superclasses_iter(&'a self, descriptor: &'a ClassDescriptor<'a>) -> SuperClassIter {
pub fn superclasses_iter(&'a self, class_name: &'a str) -> SuperClassIter {
SuperClassIter {
database: self,
descriptor: Some(descriptor),
class_name,
}
}

/// This mimics the behavior of the Roblox Lua Instance:IsA(ClassName) method.
/// Returns whether superclass_descriptor is a superclass of descriptor.
pub fn class_is_a(&'a self, class_name: &'a str, superclass_name: &'a str) -> bool {
self.superclasses_iter(class_name)
.any(|class_descriptor| class_descriptor.name == superclass_name)
}

/// Finds the default value of a property given its name and a class that
/// contains or inherits the property. Returns `Some(&Variant)` if a default
/// value exists, None otherwise.
Expand Down
14 changes: 11 additions & 3 deletions rbx_reflection_database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ mod test {
#[test]
fn superclasses_iter_test() {
let database = get();
let part_class_descriptor = database.classes.get("Part");
let mut iter = database.superclasses_iter(part_class_descriptor.unwrap());
let mut iter = database.superclasses_iter("Part");
fn class_descriptor_eq(lhs: Option<&ClassDescriptor>, rhs: Option<&ClassDescriptor>) {
let eq = match (lhs, rhs) {
(Some(lhs), Some(rhs)) => lhs.name == rhs.name,
Expand All @@ -36,11 +35,20 @@ mod test {
};
assert!(eq, "{:?} != {:?}", lhs, rhs);
}
class_descriptor_eq(iter.next(), part_class_descriptor);
class_descriptor_eq(iter.next(), database.classes.get("Part"));
class_descriptor_eq(iter.next(), database.classes.get("FormFactorPart"));
class_descriptor_eq(iter.next(), database.classes.get("BasePart"));
class_descriptor_eq(iter.next(), database.classes.get("PVInstance"));
class_descriptor_eq(iter.next(), database.classes.get("Instance"));
class_descriptor_eq(iter.next(), None);
}

#[test]
fn class_is_a_test() {
let database = get();
assert!(database.class_is_a("Part", "Instance"));
assert!(!database.class_is_a("Instance", "Part"));
assert!(!database.class_is_a("Invalid", "Part"));
assert!(!database.class_is_a("Part", "Invalid"));
}
}