Skip to content
Closed
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
94 changes: 92 additions & 2 deletions Cargo.lock

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

19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub struct Config<'a> {
#[builder(default)]
pub impl_serde: FeatureConfig<'a>,

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

/// Optional: `impl Error` for generated error type. Default: `Never`.
///
/// Note: this feature depends on `std`.
Expand All @@ -85,6 +89,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 @@ -153,6 +161,10 @@ pub fn codegen(config: Config<'_>, out: impl Write) -> Result<()> {
writeln!(w, "use arbitrary::{{Arbitrary, Unstructured}};")
})?;

config.impl_defmt.fmt_cfg(&mut w, |w| {
writeln!(w, "use defmt::Format;")
})?;

config.impl_serde.fmt_cfg(&mut w, |w| {
writeln!(w, "use serde::{{Serialize, Deserialize}};")
})?;
Expand Down Expand Up @@ -258,6 +270,7 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D
writeln!(w, "#[derive(Clone, Copy)]")?;
config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?;
config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?;
config.impl_serde.fmt_attr(&mut w, "derive(defmt::Format)")?;
writeln!(w, "pub struct {} {{", type_name(msg.message_name()))?;
{
let mut w = PadAdapter::wrap(&mut w);
Expand Down Expand Up @@ -345,8 +358,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 Expand Up @@ -986,6 +1000,7 @@ fn write_enum(
config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
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)")?;
writeln!(w, "pub enum {} {{", type_name)?;
{
let mut w = PadAdapter::wrap(&mut w);
Expand Down Expand Up @@ -1418,6 +1433,7 @@ fn render_multiplexor_enums(
config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
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)")?;
writeln!(
w,
"pub enum {} {{",
Expand Down Expand Up @@ -1445,6 +1461,7 @@ fn render_multiplexor_enums(
config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?;
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)")?;
writeln!(w, r##"#[derive(Default)]"##)?;
writeln!(
w,
Expand Down
1 change: 1 addition & 0 deletions testing/can-embedded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build-messages = ["dep:can-messages"]
[dependencies]
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.
Expand Down
1 change: 1 addition & 0 deletions testing/can-messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
bitvec = { version = "1.0", default-features = false }
arbitrary = { version = "1.0", optional = true }
embedded-can = "0.4.1"
defmt = "0.3.8"

[build-dependencies]
anyhow = "1.0"
Expand Down
1 change: 1 addition & 0 deletions testing/can-messages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() -> Result<()> {
.dbc_content(&dbc_file)
.debug_prints(true)
.impl_debug(FeatureConfig::Always)
.impl_defmt(FeatureConfig::Always)
.impl_error(FeatureConfig::Gated("std"))
.impl_arbitrary(FeatureConfig::Gated("arb"))
.check_ranges(FeatureConfig::Always)
Expand Down
Loading