Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feature/derive-defmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wallem89 committed Jan 16, 2025
2 parents 841cbbd + 5b9d3d0 commit 22a31ff
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ heck = "0.4.0"
typed-builder = "0.18.0"
embedded-can = "0.4.1"

[dev-dependencies]
defmt = "0.3.8"

[workspace]
members = [
".",
Expand Down
60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ pub struct Config<'a> {
#[builder(default)]
pub impl_debug: FeatureConfig<'a>,

/// Optional: `impl defmt::Format` for generated types. Default: `Never`.
#[builder(default)]
pub impl_defmt: FeatureConfig<'a>,

/// Optional: `impl Arbitrary` for generated types. Default: `Never`.
#[builder(default)]
pub impl_arbitrary: FeatureConfig<'a>,
Expand Down Expand Up @@ -201,6 +205,9 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
writeln!(w, "/// All messages")?;
writeln!(w, "#[derive(Clone)]")?;
config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
config
.impl_defmt
.fmt_attr(&mut w, "derive(defmt::Format)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?;
writeln!(w, "pub enum Messages {{")?;
Expand Down Expand Up @@ -444,6 +451,8 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D

render_debug_impl(&mut w, config, msg)?;

render_defmt_impl(&mut w, config, msg)?;

render_arbitrary(&mut w, config, msg)?;

let enums_for_this_message = dbc.value_descriptions().iter().filter_map(|x| {
Expand Down Expand Up @@ -998,6 +1007,9 @@ fn write_enum(
writeln!(w, "/// Defined values for {}", signal.name())?;
writeln!(w, "#[derive(Clone, Copy, PartialEq)]")?;
config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
config
.impl_defmt
.fmt_attr(&mut w, "derive(defmt::Format)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?;
config.impl_defmt.fmt_attr(&mut w, "derive(defmt::Format)")?;
Expand Down Expand Up @@ -1400,6 +1412,48 @@ fn render_debug_impl(mut w: impl Write, config: &Config<'_>, msg: &Message) -> R
Ok(())
}

fn render_defmt_impl(mut w: impl Write, config: &Config<'_>, msg: &Message) -> Result<()> {
match &config.impl_defmt {
FeatureConfig::Always => {}
FeatureConfig::Gated(gate) => writeln!(w, r##"#[cfg(feature = {gate:?})]"##)?,
FeatureConfig::Never => return Ok(()),
}

let typ = type_name(msg.message_name());
writeln!(w, r##"impl defmt::Format for {} {{"##, typ)?;
{
let mut w = PadAdapter::wrap(&mut w);
writeln!(w, "fn format(&self, f: defmt::Formatter) {{")?;
{
let mut w = PadAdapter::wrap(&mut w);
writeln!(w, r#"defmt::write!(f,"#)?;
{
let mut w = PadAdapter::wrap(&mut w);
write!(w, r#""{} {{{{"#, typ)?;
{
for signal in msg.signals() {
if *signal.multiplexer_indicator() == MultiplexIndicator::Plain {
write!(w, r#" {}={{:?}}"#, signal.name(),)?;
}
}
}
writeln!(w, r#" }}}}","#)?;

for signal in msg.signals() {
if *signal.multiplexer_indicator() == MultiplexIndicator::Plain {
writeln!(w, "self.{}(),", field_name(signal.name()))?;
}
}
writeln!(w, r#");"#)?;
}
writeln!(w, "}}")?;
}
}
writeln!(w, "}}")?;
writeln!(w)?;
Ok(())
}

fn render_multiplexor_enums(
mut w: impl Write,
config: &Config<'_>,
Expand Down Expand Up @@ -1431,6 +1485,9 @@ fn render_multiplexor_enums(
)?;

config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
config
.impl_defmt
.fmt_attr(&mut w, "derive(defmt::Format)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?;
config.impl_defmt.fmt_attr(&mut w, "derive(defmt::Format)")?;
Expand Down Expand Up @@ -1459,6 +1516,9 @@ fn render_multiplexor_enums(
let struct_name = multiplexed_enum_variant_name(msg, multiplexor_signal, **switch_index)?;

config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
config
.impl_defmt
.fmt_attr(&mut w, "derive(defmt::Format)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?;
config.impl_defmt.fmt_attr(&mut w, "derive(defmt::Format)")?;
Expand Down
1 change: 0 additions & 1 deletion testing/can-embedded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ bitvec = { version = "1.0", default-features = false }
embedded-can = "0.4.1"
defmt = "0.3.8"


# This is optional and default so we can turn it off for the embedded target.
# Then it doesn't pull in std.
[dependencies.can-messages]
Expand Down
122 changes: 121 additions & 1 deletion testing/can-messages/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use defmt::Format;
use embedded_can::{ExtendedId, Id, StandardId};

/// All messages
#[derive(Clone, Debug)]
#[derive(Clone, Debug, defmt::Format)]
pub enum Messages {
/// Foo
Foo(Foo),
Expand Down Expand Up @@ -273,6 +273,17 @@ impl core::fmt::Debug for Foo {
}
}

impl defmt::Format for Foo {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"Foo {{ Voltage={:?} Current={:?} }}",
self.voltage(),
self.current(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for Foo {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -623,6 +634,20 @@ impl core::fmt::Debug for Bar {
}
}

impl defmt::Format for Bar {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"Bar {{ One={:?} Two={:?} Three={:?} Four={:?} Type={:?} }}",
self.one(),
self.two(),
self.three(),
self.four(),
self.xtype(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for Bar {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -841,6 +866,12 @@ impl core::fmt::Debug for X4wd {
}
}

impl defmt::Format for X4wd {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "X4wd {{ _4DRIVE={:?} }}", self.x4drive(),);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for X4wd {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -1189,6 +1220,20 @@ impl core::fmt::Debug for Amet {
}
}

impl defmt::Format for Amet {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"Amet {{ One={:?} Two={:?} Three={:?} Four={:?} Five={:?} }}",
self.one(),
self.two(),
self.three(),
self.four(),
self.five(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for Amet {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -1343,6 +1388,12 @@ impl core::fmt::Debug for Dolor {
}
}

impl defmt::Format for Dolor {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "Dolor {{ OneFloat={:?} }}", self.one_float(),);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for Dolor {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -1584,6 +1635,16 @@ impl core::fmt::Debug for MultiplexTest {
}
}

impl defmt::Format for MultiplexTest {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"MultiplexTest {{ UnmultiplexedSignal={:?} }}",
self.unmultiplexed_signal(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for MultiplexTest {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -2140,6 +2201,19 @@ impl core::fmt::Debug for IntegerFactorOffset {
}
}

impl defmt::Format for IntegerFactorOffset {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f,
"IntegerFactorOffset {{ ByteWithOffset={:?} ByteWithFactor={:?} ByteWithBoth={:?} ByteWithNegativeOffset={:?} ByteWithNegativeMin={:?} }}",
self.byte_with_offset(),
self.byte_with_factor(),
self.byte_with_both(),
self.byte_with_negative_offset(),
self.byte_with_negative_min(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for IntegerFactorOffset {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -2355,6 +2429,17 @@ impl core::fmt::Debug for NegativeFactorTest {
}
}

impl defmt::Format for NegativeFactorTest {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"NegativeFactorTest {{ UnsignedNegativeFactorSignal={:?} WidthMoreThanMinMax={:?} }}",
self.unsigned_negative_factor_signal(),
self.width_more_than_min_max(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for NegativeFactorTest {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -2562,6 +2647,17 @@ impl core::fmt::Debug for LargerIntsWithOffsets {
}
}

impl defmt::Format for LargerIntsWithOffsets {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"LargerIntsWithOffsets {{ Twelve={:?} Sixteen={:?} }}",
self.twelve(),
self.sixteen(),
);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for LargerIntsWithOffsets {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -2657,6 +2753,12 @@ impl core::fmt::Debug for MsgWithoutSignals {
}
}

impl defmt::Format for MsgWithoutSignals {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "MsgWithoutSignals {{ }}",);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for MsgWithoutSignals {
fn arbitrary(_u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -2803,6 +2905,12 @@ impl core::fmt::Debug for TruncatedBeSignal {
}
}

impl defmt::Format for TruncatedBeSignal {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "TruncatedBeSignal {{ Foo={:?} }}", self.foo(),);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for TruncatedBeSignal {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -2950,6 +3058,12 @@ impl core::fmt::Debug for TruncatedLeSignal {
}
}

impl defmt::Format for TruncatedLeSignal {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "TruncatedLeSignal {{ Foo={:?} }}", self.foo(),);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for TruncatedLeSignal {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down Expand Up @@ -3095,6 +3209,12 @@ impl core::fmt::Debug for MsgExtendedId {
}
}

impl defmt::Format for MsgExtendedId {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "MsgExtendedId {{ Dummy={:?} }}", self.dummy(),);
}
}

#[cfg(feature = "arb")]
impl<'a> Arbitrary<'a> for MsgExtendedId {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
Expand Down

0 comments on commit 22a31ff

Please sign in to comment.