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

change 'collapse_type_name' to retain enum types #9587

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Changes from 1 commit
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
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(|c: char| c.is_uppercase()) {
EmiOnGit marked this conversation as resolved.
Show resolved Hide resolved
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