diff --git a/src/lib.rs b/src/lib.rs index c6fbee1..f01d207 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,6 +85,10 @@ pub struct Config<'a> { /// Optional: Allow dead code in the generated module. Default: `false`. #[builder(default)] pub allow_dead_code: bool, + + /// Optional: Padding value used; set all unused bits to 1 if true else 0. Default: `false`. + #[builder(default)] + pub padding_value: bool, } /// Configuration for including features in the codegenerator. @@ -345,8 +349,9 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D let mut w = PadAdapter::wrap(&mut w); writeln!( &mut w, - "let {}res = Self {{ raw: [0u8; {}] }};", + "let {}res = Self {{ raw: [{}; {}] }};", if msg.signals().is_empty() { "" } else { "mut " }, + if config.padding_value { "0xFF" } else { "0x00" }, msg.message_size() )?; for signal in msg.signals().iter() { diff --git a/testing/can-messages/src/messages.rs b/testing/can-messages/src/messages.rs index 6ee9d84..797388c 100644 --- a/testing/can-messages/src/messages.rs +++ b/testing/can-messages/src/messages.rs @@ -108,7 +108,7 @@ impl Foo { /// Construct new Foo from values pub fn new(voltage: f32, current: f32) -> Result { - let mut res = Self { raw: [0u8; 4] }; + let mut res = Self { raw: [0x00; 4] }; res.set_voltage(voltage)?; res.set_current(current)?; Ok(res) @@ -306,7 +306,7 @@ impl Bar { /// Construct new Bar from values pub fn new(one: u8, two: f32, three: u8, four: u8, xtype: bool) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_one(one)?; res.set_two(two)?; res.set_three(three)?; @@ -357,7 +357,7 @@ impl Bar { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: Bar::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -454,7 +454,7 @@ impl Bar { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: Bar::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -507,7 +507,7 @@ impl Bar { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: Bar::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -714,7 +714,7 @@ impl X4wd { /// Construct new _4WD from values pub fn new(x4drive: u8) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_x4drive(x4drive)?; Ok(res) } @@ -769,7 +769,7 @@ impl X4wd { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: X4wd::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -894,7 +894,7 @@ impl Amet { /// Construct new Amet from values pub fn new(one: u8, two: f32, three: u8, four: u8, five: bool) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_one(one)?; res.set_two(two)?; res.set_three(three)?; @@ -945,7 +945,7 @@ impl Amet { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: Amet::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1034,7 +1034,7 @@ impl Amet { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: Amet::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1079,7 +1079,7 @@ impl Amet { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: Amet::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1219,7 +1219,7 @@ impl Dolor { /// Construct new Dolor from values pub fn new(one_float: f32) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_one_float(one_float)?; Ok(res) } @@ -1396,7 +1396,7 @@ impl MultiplexTest { /// Construct new MultiplexTest from values pub fn new(multiplexor: u8, unmultiplexed_signal: u8) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_multiplexor(multiplexor)?; res.set_unmultiplexed_signal(unmultiplexed_signal)?; Ok(res) @@ -1447,7 +1447,7 @@ impl MultiplexTest { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: MultiplexTest::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1512,7 +1512,7 @@ impl MultiplexTest { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: MultiplexTest::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1828,7 +1828,7 @@ impl IntegerFactorOffset { byte_with_negative_offset: i16, byte_with_negative_min: i16, ) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_byte_with_offset(byte_with_offset)?; res.set_byte_with_factor(byte_with_factor)?; res.set_byte_with_both(byte_with_both)?; @@ -1879,7 +1879,7 @@ impl IntegerFactorOffset { } let factor = 1; let value = value.checked_sub(1).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: IntegerFactorOffset::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1924,7 +1924,7 @@ impl IntegerFactorOffset { } let factor = 4; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: IntegerFactorOffset::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -1969,7 +1969,7 @@ impl IntegerFactorOffset { } let factor = 2; let value = value.checked_sub(16).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: IntegerFactorOffset::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -2014,7 +2014,7 @@ impl IntegerFactorOffset { } let factor = 1; let value = value.checked_add(1).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: IntegerFactorOffset::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -2059,7 +2059,7 @@ impl IntegerFactorOffset { } let factor = 1; let value = value.checked_add(1).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: IntegerFactorOffset::MESSAGE_ID, })?; let value = (value / factor) as u8; @@ -2182,7 +2182,7 @@ impl NegativeFactorTest { unsigned_negative_factor_signal: i32, width_more_than_min_max: i16, ) -> Result { - let mut res = Self { raw: [0u8; 4] }; + let mut res = Self { raw: [0x00; 4] }; res.set_unsigned_negative_factor_signal(unsigned_negative_factor_signal)?; res.set_width_more_than_min_max(width_more_than_min_max)?; Ok(res) @@ -2230,7 +2230,7 @@ impl NegativeFactorTest { } let factor = -1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: NegativeFactorTest::MESSAGE_ID, })?; let value = (value / factor) as u16; @@ -2276,7 +2276,7 @@ impl NegativeFactorTest { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: NegativeFactorTest::MESSAGE_ID, })?; let value = (value / factor) as i16; @@ -2385,7 +2385,7 @@ impl LargerIntsWithOffsets { /// Construct new LargerIntsWithOffsets from values pub fn new(twelve: i16, sixteen: i32) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_twelve(twelve)?; res.set_sixteen(sixteen)?; Ok(res) @@ -2438,7 +2438,7 @@ impl LargerIntsWithOffsets { let value = value .checked_add(1000) .ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: LargerIntsWithOffsets::MESSAGE_ID, })?; let value = (value / factor) as u16; @@ -2487,7 +2487,7 @@ impl LargerIntsWithOffsets { let value = value .checked_add(1000) .ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: LargerIntsWithOffsets::MESSAGE_ID, })?; let value = (value / factor) as u16; @@ -2586,7 +2586,7 @@ impl MsgWithoutSignals { /// Construct new MsgWithoutSignals from values pub fn new() -> Result { - let res = Self { raw: [0u8; 8] }; + let res = Self { raw: [0x00; 8] }; Ok(res) } @@ -2682,7 +2682,7 @@ impl TruncatedBeSignal { /// Construct new TruncatedBeSignal from values pub fn new(foo: i16) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_foo(foo)?; Ok(res) } @@ -2730,7 +2730,7 @@ impl TruncatedBeSignal { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: TruncatedBeSignal::MESSAGE_ID, })?; let value = (value / factor) as i16; @@ -2829,7 +2829,7 @@ impl TruncatedLeSignal { /// Construct new TruncatedLeSignal from values pub fn new(foo: i16) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_foo(foo)?; Ok(res) } @@ -2877,7 +2877,7 @@ impl TruncatedLeSignal { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: TruncatedLeSignal::MESSAGE_ID, })?; let value = (value / factor) as i16; @@ -2976,7 +2976,7 @@ impl MsgExtendedId { /// Construct new MsgExtendedId from values pub fn new(dummy: u8) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_dummy(dummy)?; Ok(res) } @@ -3023,7 +3023,7 @@ impl MsgExtendedId { } let factor = 1; let value = value.checked_sub(0).ok_or(CanError::ParameterOutOfRange { - message_id: Self::MESSAGE_ID, + message_id: MsgExtendedId::MESSAGE_ID, })?; let value = (value / factor) as u8;