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

Normalize enum variant names #10

Merged
merged 3 commits into from
Jul 2, 2020
Merged

Normalize enum variant names #10

merged 3 commits into from
Jul 2, 2020

Conversation

rkr35
Copy link
Owner

@rkr35 rkr35 commented Jul 2, 2020

Closes #9 .

Strips prefixes from enum variants and normalizes the case to PascalCase.

Enum variants that become invalid Rust identifiers after stripping will retain their prefixes, but the case of the variant still gets normalized to PascalCase.

Examples of generated enums

All the variants no longer have a common prefix:

#[repr(u8)]
pub enum ECsgOper {
    Active,
    Add,
    Subtract,
    Intersect,
    Deintersect,
    Max,
}

A variant named Self after stripping still retains its prefix:

#[repr(u8)]
pub enum EBehaviorContext {
    BcontextSelf, // Compiler error if variant was simply called `Self`
    MyInstigator,
    OtherEventParticipant,
    EventData,
    UseContextObject,
    Max,
}

A variant that begins with a number after stripping still retains its prefix:

#[repr(u8)]
pub enum ESliderType {
    St1d, // This and the following variant would not compile if they were called "1d" and "2d" respectively.
    St2d,
    Max,
}

A variant that becomes an empty string after stripping still retains its prefix:

#[repr(u8)]
pub enum EBehaviorCounterID {
    Bcounter, // The prefix is "Bcounter", but removing that prefix leaves an empty string.
    Bcounter1, // Incidentally, another example of retaining the prefix because the stripped version begins with a number.
    Max,
}

rkr35 added 3 commits July 2, 2020 18:06
The outer `if let Some(...) = common_prefix` check already handles
variants.len() == 0, and the stripped check takes care of preventing
the use of a stripped, empty string as a variant name.
@rkr35 rkr35 merged commit ababfa2 into master Jul 2, 2020
@rkr35 rkr35 deleted the normalize_enum_variant_names branch July 2, 2020 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strip common prefix from enum variants
1 participant