Skip to content

Commit a6991c3

Browse files
EmiOnGitEminicopap
authored
change 'collapse_type_name' to retain enum types (#9587)
# 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 <[email protected]> Co-authored-by: Nicola Papale <[email protected]>
1 parent 90b3ac7 commit a6991c3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

crates/bevy_utils/src/short_names.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,20 @@ pub fn get_short_name(full_name: &str) -> String {
5959

6060
#[inline(always)]
6161
fn collapse_type_name(string: &str) -> &str {
62-
string.split("::").last().unwrap()
62+
// Enums types are retained.
63+
// As heuristic, we assume the enum type to be uppercase.
64+
let mut segments = string.rsplit("::");
65+
let (last, second_last): (&str, Option<&str>) = (segments.next().unwrap(), segments.next());
66+
let Some(second_last) = second_last else {
67+
return last;
68+
};
69+
70+
if second_last.starts_with(char::is_uppercase) {
71+
let index = string.len() - last.len() - second_last.len() - 2;
72+
&string[index..]
73+
} else {
74+
last
75+
}
6376
}
6477

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

118+
#[test]
119+
fn enums() {
120+
assert_eq!(get_short_name("Option::None"), "Option::None".to_string());
121+
assert_eq!(
122+
get_short_name("Option::Some(2)"),
123+
"Option::Some(2)".to_string()
124+
);
125+
assert_eq!(
126+
get_short_name("bevy_render::RenderSet::Prepare"),
127+
"RenderSet::Prepare".to_string()
128+
);
129+
}
130+
105131
#[test]
106132
fn generics() {
107133
assert_eq!(

0 commit comments

Comments
 (0)