technocreatives / dbc-codegen

Generate Rust structs for messages from a dbc (CAN bus definition) file.
Apache License 2.0
48 stars 31 forks source link

None unique field enums are not renamed #52

Open wallem89 opened 1 year ago

wallem89 commented 1 year ago

I have a DBC file which has non unique field names in an enumerated signal. Example:

/// Defined values for Command
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub enum CommandCommand{
    NotAvailable,
    Error,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Reserved,
    Release,
    Close,
    Open,
    Init,
    _Other(u8),
}

I know this is a bit strange but all none used enums are set to reserved. I could delete them so that _Other is handling those but renaming would be done as for the enums starting with a non-alphabetic character .

From DBC:

VAL_ 2566903515 Command 15 "Not Available" 14 "Error" 13 "Reserved" 12 "Reserved" 11 "Reserved" 10 "Reserved" 9 "Reserved" 8 "Reserved" 7 "Reserved" 6 "Reserved" 5 "Reserved" 4 "Reserved" 3 "Release" 2 "Close" 1 "Open" 0 "Init" ;

For now my workaround is to rename the Reserved enums in the DBC-file:

VAL_ 2566903515 Command 15 "Not Available" 14 "Error" 13 "Reserved_13" 12 "Reserved_12" 11 "Reserved_11" 10 "Reserved_10" 9 "Reserved_9" 8 "Reserved_8" 7 "Reserved_7" 6 "Reserved_6" 5 "Reserved_5" 4 "Reserved_4" 3 "Release" 2 "Close" 1 "Open" 0 "Init" ;

Which will give me unique enums:

/// Defined values for Command
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub enum CommandCommand{
    NotAvailable,
    Error,
    Reserved13,
    Reserved12,
    Reserved11,
    Reserved10,
    Reserved9,
    Reserved8,
    Reserved7,
    Reserved6,
    Reserved5,
    Reserved4,
    Release,
    Close,
    Open,
    Init,
    _Other(u8),
}

I could try to implement this myself and send in a PR but depends if you thinks this makes sense.

JoNil commented 6 months ago

I have a pr for that in the can-dbc crate that would fix it when parsing: https://github.com/marcelbuesing/can-dbc/pull/15

killercup commented 6 months ago

Just saw @marcelbuesing wrote that this would be better done here. If you want to port the PR over here, I'd be happy to accept it :)