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

feat: Support option for padding (set all unused bit to 1) #87

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
68 changes: 34 additions & 34 deletions testing/can-messages/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Foo {

/// Construct new Foo from values
pub fn new(voltage: f32, current: f32) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 4] };
let mut res = Self { raw: [0x00; 4] };
res.set_voltage(voltage)?;
res.set_current(current)?;
Ok(res)
Expand Down Expand Up @@ -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<Self, CanError> {
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)?;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -714,7 +714,7 @@ impl X4wd {

/// Construct new _4WD from values
pub fn new(x4drive: u8) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
let mut res = Self { raw: [0x00; 8] };
res.set_x4drive(x4drive)?;
Ok(res)
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<Self, CanError> {
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)?;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -1219,7 +1219,7 @@ impl Dolor {

/// Construct new Dolor from values
pub fn new(one_float: f32) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
let mut res = Self { raw: [0x00; 8] };
res.set_one_float(one_float)?;
Ok(res)
}
Expand Down Expand Up @@ -1396,7 +1396,7 @@ impl MultiplexTest {

/// Construct new MultiplexTest from values
pub fn new(multiplexor: u8, unmultiplexed_signal: u8) -> Result<Self, CanError> {
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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -1828,7 +1828,7 @@ impl IntegerFactorOffset {
byte_with_negative_offset: i16,
byte_with_negative_min: i16,
) -> Result<Self, CanError> {
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)?;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2182,7 +2182,7 @@ impl NegativeFactorTest {
unsigned_negative_factor_signal: i32,
width_more_than_min_max: i16,
) -> Result<Self, CanError> {
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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2385,7 +2385,7 @@ impl LargerIntsWithOffsets {

/// Construct new LargerIntsWithOffsets from values
pub fn new(twelve: i16, sixteen: i32) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
let mut res = Self { raw: [0x00; 8] };
res.set_twelve(twelve)?;
res.set_sixteen(sixteen)?;
Ok(res)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2586,7 +2586,7 @@ impl MsgWithoutSignals {

/// Construct new MsgWithoutSignals from values
pub fn new() -> Result<Self, CanError> {
let res = Self { raw: [0u8; 8] };
let res = Self { raw: [0x00; 8] };
Ok(res)
}

Expand Down Expand Up @@ -2682,7 +2682,7 @@ impl TruncatedBeSignal {

/// Construct new TruncatedBeSignal from values
pub fn new(foo: i16) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
let mut res = Self { raw: [0x00; 8] };
res.set_foo(foo)?;
Ok(res)
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2829,7 +2829,7 @@ impl TruncatedLeSignal {

/// Construct new TruncatedLeSignal from values
pub fn new(foo: i16) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
let mut res = Self { raw: [0x00; 8] };
res.set_foo(foo)?;
Ok(res)
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2976,7 +2976,7 @@ impl MsgExtendedId {

/// Construct new MsgExtendedId from values
pub fn new(dummy: u8) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
let mut res = Self { raw: [0x00; 8] };
res.set_dummy(dummy)?;
Ok(res)
}
Expand Down Expand Up @@ -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;

Expand Down