Skip to content

Commit

Permalink
change 'collapse_type_name' to retain enum types (#9587)
Browse files Browse the repository at this point in the history
# Objective

Fixes #9509 

## Solution
We use the assumption, that enum types are uppercase in contrast to
module names.
[`collapse_type_name`](crates/bevy_util/src/short_names) is now
retaining the second last segment, if it starts with a uppercase
character.

---------

Co-authored-by: Emi <emanuel.boehm@gmail.com>
Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 26, 2023
1 parent 90b3ac7 commit a6991c3
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crates/bevy_utils/src/short_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@ pub fn get_short_name(full_name: &str) -> String {

#[inline(always)]
fn collapse_type_name(string: &str) -> &str {
string.split("::").last().unwrap()
// Enums types are retained.
// As heuristic, we assume the enum type to be uppercase.
let mut segments = string.rsplit("::");
let (last, second_last): (&str, Option<&str>) = (segments.next().unwrap(), segments.next());
let Some(second_last) = second_last else {
return last;
};

if second_last.starts_with(char::is_uppercase) {
let index = string.len() - last.len() - second_last.len() - 2;
&string[index..]
} else {
last
}
}

#[cfg(test)]
Expand Down Expand Up @@ -102,6 +115,19 @@ mod name_formatting_tests {
assert_eq!(get_short_name("a<B, C>"), "a<B, C>".to_string());
}

#[test]
fn enums() {
assert_eq!(get_short_name("Option::None"), "Option::None".to_string());
assert_eq!(
get_short_name("Option::Some(2)"),
"Option::Some(2)".to_string()
);
assert_eq!(
get_short_name("bevy_render::RenderSet::Prepare"),
"RenderSet::Prepare".to_string()
);
}

#[test]
fn generics() {
assert_eq!(
Expand Down

0 comments on commit a6991c3

Please sign in to comment.