Skip to content

Commit

Permalink
Merge pull request #41 from andresv/fix-nonascii-message-names
Browse files Browse the repository at this point in the history
Fix message names that start with non alphabetic char
  • Loading branch information
Pascal Hertleif authored Apr 21, 2021
2 parents c55cd4f + f17975f commit b062b9a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ fn render_message(mut w: impl Write, msg: &Message, dbc: &DBC) -> Result<()> {
writeln!(
&mut w,
"pub const {sig}_MIN: {typ} = {min}_{typ};",
sig = signal.name().to_snake_case().to_uppercase(),
sig = field_name(signal.name()).to_uppercase(),
typ = typ,
min = signal.min,
)?;

writeln!(
&mut w,
"pub const {sig}_MAX: {typ} = {max}_{typ};",
sig = signal.name().to_snake_case().to_uppercase(),
sig = field_name(signal.name()).to_uppercase(),
typ = typ,
max = signal.max,
)?;
Expand Down Expand Up @@ -875,7 +875,11 @@ fn signal_to_rust_type(signal: &Signal) -> String {
}

fn type_name(x: &str) -> String {
x.to_camel_case()
if keywords::is_keyword(x) || !x.starts_with(|c: char| c.is_ascii_alphabetic()) {
format!("X{}", x.to_camel_case())
} else {
x.to_camel_case()
}
}

fn field_name(x: &str) -> String {
Expand All @@ -887,10 +891,15 @@ fn field_name(x: &str) -> String {
}

fn enum_name(msg: &Message, signal: &Signal) -> String {
// this turns signal `_4DRIVE` into `4drive`
let signal_name = signal
.name()
.trim_start_matches(|c: char| c.is_ascii_punctuation());

format!(
"{}{}",
msg.message_name().to_camel_case(),
signal.name().to_camel_case()
enum_variant_name(msg.message_name()),
signal_name.to_camel_case()
)
}

Expand Down
138 changes: 138 additions & 0 deletions testing/can-messages/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum Messages {
Foo(Foo),
/// Bar
Bar(Bar),
/// _4WD
X4wd(X4wd),
/// Amet
Amet(Amet),
/// Dolor
Expand All @@ -43,6 +45,7 @@ impl Messages {
let res = match id {
256 => Messages::Foo(Foo::try_from(payload)?),
512 => Messages::Bar(Bar::try_from(payload)?),
768 => Messages::X4wd(X4wd::try_from(payload)?),
1024 => Messages::Amet(Amet::try_from(payload)?),
1028 => Messages::Dolor(Dolor::try_from(payload)?),
200 => Messages::MultiplexTest(MultiplexTest::try_from(payload)?),
Expand Down Expand Up @@ -577,6 +580,141 @@ impl Into<bool> for BarType {
}
}

/// _4WD
///
/// - ID: 768 (0x300)
/// - Size: 8 bytes
/// - Transmitter: Ipsum
#[derive(Clone, Copy)]
pub struct X4wd {
raw: [u8; 8],
}

impl X4wd {
pub const MESSAGE_ID: u32 = 768;

pub const X4DRIVE_MIN: u8 = 0_u8;
pub const X4DRIVE_MAX: u8 = 7_u8;

/// Construct new _4WD from values
pub fn new(x4drive: u8) -> Result<Self, CanError> {
let mut res = Self { raw: [0u8; 8] };
res.set_x4drive(x4drive)?;
Ok(res)
}

/// Access message payload raw value
pub fn raw(&self) -> &[u8] {
&self.raw
}

/// _4DRIVE
///
/// - Min: 0
/// - Max: 7
/// - Unit: ""
/// - Receivers: Dolor
#[inline(always)]
pub fn x4drive(&self) -> X4wd4drive {
self.x4drive_raw().into()
}

/// Get raw value of _4DRIVE
///
/// - Start bit: 13
/// - Signal size: 3 bits
/// - Factor: 1
/// - Offset: 0
/// - Byte order: BigEndian
/// - Value type: Unsigned
#[inline(always)]
pub fn x4drive_raw(&self) -> u8 {
let signal = self.raw.view_bits::<Msb0>()[10..13].load_be::<u8>();

signal
}

/// Set value of _4DRIVE
#[inline(always)]
pub fn set_x4drive(&mut self, value: u8) -> Result<(), CanError> {
#[cfg(feature = "range_checked")]
if value < 0_u8 || 7_u8 < value {
return Err(CanError::ParameterOutOfRange { message_id: 768 });
}
self.raw.view_bits_mut::<Msb0>()[10..13].store_be(value);
Ok(())
}
}

impl core::convert::TryFrom<&[u8]> for X4wd {
type Error = CanError;

#[inline(always)]
fn try_from(payload: &[u8]) -> Result<Self, Self::Error> {
if payload.len() != 8 {
return Err(CanError::InvalidPayloadSize);
}
let mut raw = [0u8; 8];
raw.copy_from_slice(&payload[..8]);
Ok(Self { raw })
}
}

#[cfg(feature = "debug")]
impl core::fmt::Debug for X4wd {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if f.alternate() {
f.debug_struct("X4wd")
.field("x4drive", &self.x4drive())
.finish()
} else {
f.debug_tuple("X4wd").field(&self.raw).finish()
}
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for X4wd {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
let x4drive = u.int_in_range(0..=7)?;
X4wd::new(x4drive).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
/// Defined values for _4DRIVE
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub enum X4wd4drive {
Off,
X2wd,
X4wd,
All,
_Other(u8),
}

impl From<u8> for X4wd4drive {
fn from(raw: u8) -> Self {
match raw {
0 => X4wd4drive::Off,
1 => X4wd4drive::X2wd,
2 => X4wd4drive::X4wd,
3 => X4wd4drive::All,
x => X4wd4drive::_Other(x),
}
}
}

impl Into<u8> for X4wd4drive {
fn into(self) -> u8 {
match self {
X4wd4drive::Off => 0,
X4wd4drive::X2wd => 1,
X4wd4drive::X4wd => 2,
X4wd4drive::All => 3,
X4wd4drive::_Other(x) => x,
}
}
}

/// Amet
///
/// - ID: 1024 (0x400)
Expand Down
4 changes: 4 additions & 0 deletions testing/dbc-examples/example.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ BO_ 512 Bar: 8 Ipsum
SG_ Four : 10|2@0+ (1,0) [0|3] "" Dolor
SG_ Type : 30|1@0+ (1,0) [0|1] "boolean" Dolor

BO_ 768 _4WD: 8 Ipsum
SG_ _4DRIVE : 13|3@0+ (1,0) [0|7] "" Dolor

BO_ 1024 Amet: 8 Sit
SG_ One : 15|2@0+ (1,0) [0|3] "" Dolor
SG_ Two : 7|8@0+ (0.39,0) [0.00|100] "%" Dolor
Expand All @@ -40,4 +43,5 @@ BO_ 200 MultiplexTest: 8 SENSOR
VAL_ 512 Three 0 "OFF" 1 "ON" 2 "ONER" 3 "ONEST";
VAL_ 512 Four 0 "Off" 1 "On" 2 "Oner" 3 "Onest";
VAL_ 512 Type 0 "0Off" 1 "1On";
VAL_ 768 _4DRIVE 0 "OFF" 1 "2WD" 2 "4WD" 3 "ALL";
VAL_ 1028 OneFloat 3 "Dolor" 5 "Other";

0 comments on commit b062b9a

Please sign in to comment.