Skip to content

Commit adc2388

Browse files
author
Pascal Hertleif
authored
Merge pull request #2 from marcelbuesing/fix-case
Correct casing of enum variants according to Rust conventions
2 parents bc4a225 + ea4b2e1 commit adc2388

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,13 @@ fn render_signal(mut w: impl Write, signal: &Signal, dbc: &DBC, msg: &Message) -
309309
let mut w = PadAdapter::wrap(&mut w);
310310
for variant in variants {
311311
let literal = match_on_raw_type(*variant.a());
312-
writeln!(&mut w, "{} => {}::{},", literal, type_name, variant.b())?;
312+
writeln!(
313+
&mut w,
314+
"{} => {}::{},",
315+
literal,
316+
type_name,
317+
enum_variant_name(variant.b())
318+
)?;
313319
}
314320
writeln!(&mut w, "x => {}::Other(x),", type_name,)?;
315321
}
@@ -450,7 +456,7 @@ fn write_enum(
450456
{
451457
let mut w = PadAdapter::wrap(&mut w);
452458
for variant in variants {
453-
writeln!(w, "{},", variant.b())?;
459+
writeln!(w, "{},", enum_variant_name(variant.b()))?;
454460
}
455461
writeln!(w, "Other({}),", signal_to_rust_type(signal))?;
456462
}
@@ -505,3 +511,7 @@ fn enum_name(msg: &Message, signal: &Signal) -> String {
505511
signal.name().to_camel_case()
506512
)
507513
}
514+
515+
fn enum_variant_name(x: &str) -> String {
516+
x.to_camel_case()
517+
}

testing/can-messages/src/messages.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,14 @@ impl Bar {
259259
/// - Unit: ""
260260
/// - Receivers: Dolor
261261
#[inline(always)]
262-
pub fn three(&self) -> u8 {
263-
self.three_raw()
262+
pub fn three(&self) -> BarThree {
263+
match self.three_raw() {
264+
0 => BarThree::Off,
265+
1 => BarThree::On,
266+
2 => BarThree::Oner,
267+
3 => BarThree::Onest,
268+
x => BarThree::Other(x),
269+
}
264270
}
265271

266272
/// Get raw value of Three
@@ -343,6 +349,16 @@ impl core::convert::TryFrom<&[u8]> for Bar {
343349
}
344350
}
345351

352+
/// Defined values for Three
353+
#[derive(Clone, Copy)]
354+
#[cfg_attr(feature = "debug", derive(Debug))]
355+
pub enum BarThree {
356+
Off,
357+
On,
358+
Oner,
359+
Onest,
360+
Other(u8),
361+
}
346362
/// Defined values for Four
347363
#[derive(Clone, Copy)]
348364
#[cfg_attr(feature = "debug", derive(Debug))]

testing/dbc-examples/example.dbc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ BO_ 512 Bar: 8 Ipsum
2626

2727

2828

29+
VAL_ 512 Three 0 "OFF" 1 "ON" 2 "ONER" 3 "ONEST";
2930
VAL_ 512 Four 0 "Off" 1 "On" 2 "Oner" 3 "Onest";

testing/rust-integration/tests/compare_to_socketcan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn weirdly_aligned_bigendian_message_1() {
2626

2727
assert_eq!(msg.one(), 2);
2828
assert_f32_eq(msg.two(), 2_f32);
29-
assert_eq!(msg.three(), 2);
29+
assert!(matches!(msg.three(), messages::BarThree::Oner));
3030
assert!(matches!(msg.four(), messages::BarFour::Oner));
3131
}
3232

@@ -37,7 +37,7 @@ fn weirdly_aligned_bigendian_message_2() {
3737

3838
assert_eq!(msg.one(), 1);
3939
assert_f32_eq(msg.two(), 2_f32);
40-
assert_eq!(msg.three(), 3);
40+
assert!(matches!(msg.three(), messages::BarThree::Onest));
4141
assert!(matches!(msg.four(), messages::BarFour::On));
4242
}
4343

0 commit comments

Comments
 (0)