technocreatives / dbc-codegen

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

Handle lead underscore in message name #40

Closed andresv closed 3 years ago

andresv commented 3 years ago

At the moment this

BO_ 1066 _4WD13: 6 _4WD
 SG_ _4WD_CURRENT : 0|8@1+ (0.390625,0.0) [-50.0|50.0] "A"  TCU
 SG_ _4WD_POSITION : 8|16@1+ (0.015625,0.0) [-180.0|180.0] "Deg"  TCU
 SG_ _4WD_CLU_THERM_STR : 24|8@1+ (1.0,0.0) [0.0|100.0] "%"  TCU
 SG_ _4WD_STATUS : 32|8@1+ (1.0,0.0) [0.0|15.0] ""  ESC,TCU

... is generated as

/// _4WD13
///
/// - ID: 1066 (0x42a)
/// - Size: 6 bytes
/// - Transmitter: _4WD
#[derive(Clone, Copy)]
pub struct 4wd13 {
    raw: [u8; 6],
}

impl 4wd13 {
    pub const MESSAGE_ID: u32 = 1066;

    pub const 4WD_CURRENT_MIN: f32 = -50_f32;
    pub const 4WD_CURRENT_MAX: f32 = 50_f32;
    pub const 4WD_POSITION_MIN: f32 = -180_f32;
    pub const 4WD_POSITION_MAX: f32 = 180_f32;
    pub const 4WD_CLU_THERM_STR_MIN: u8 = 0_u8;
    pub const 4WD_CLU_THERM_STR_MAX: u8 = 100_u8;
    pub const 4WD_STATUS_MIN: u8 = 0_u8;
    pub const 4WD_STATUS_MAX: u8 = 15_u8;

    /// Construct new _4WD13 from values
    pub fn new(x4wd_current: f32, x4wd_position: f32, x4wd_clu_therm_str: u8, x4wd_status: u8) -> Result<Self, CanError> {
        let mut res = Self { raw: [0u8; 6] };
        res.set_x4wd_current(x4wd_current)?;
        res.set_x4wd_position(x4wd_position)?;
        res.set_x4wd_clu_therm_str(x4wd_clu_therm_str)?;
        res.set_x4wd_status(x4wd_status)?;
        Ok(res)
    }

It seems methods are prefixed with X, but struct itself is not and therefore it is invalid rust code. I am going to create a PR that turns this into X4wd13.