Skip to content

Commit

Permalink
Generate embedded_can::Frame trait for each frame
Browse files Browse the repository at this point in the history
  • Loading branch information
trnila committed Aug 21, 2024
1 parent 89e5805 commit 56c4fcb
Show file tree
Hide file tree
Showing 2 changed files with 524 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub struct Config<'a> {
#[builder(default)]
pub impl_error: FeatureConfig<'a>,

/// Optional: Generate `embedded_can::Frame` impl for each frame. Default: `Always`
#[builder(default = FeatureConfig::Always)]
pub impl_embedded_can_frame: FeatureConfig<'a>,

/// Optional: Validate min and max values in generated signal setters. Default: `Always`
#[builder(default = FeatureConfig::Always)]
pub check_ranges: FeatureConfig<'a>,
Expand Down Expand Up @@ -428,6 +432,8 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D
writeln!(w, "}}")?;
writeln!(w)?;

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

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

render_arbitrary(&mut w, config, msg)?;
Expand Down Expand Up @@ -1285,6 +1291,56 @@ fn multiplexed_enum_variant_name(
))
}

fn render_embedded_can_frame(
w: &mut impl Write,
config: &Config<'_>,
msg: &Message,
) -> Result<(), std::io::Error> {
config.impl_embedded_can_frame.fmt_cfg(w, |w| {
writeln!(
w,
"\
impl embedded_can::Frame for {0} {{
fn new(id: impl Into<Id>, data: &[u8]) -> Option<Self> {{
if id.into() != Self::MESSAGE_ID {{
None
}} else {{
data.try_into().ok()
}}
}}
fn new_remote(_id: impl Into<Id>, _dlc: usize) -> Option<Self> {{
unimplemented!()
}}
fn is_extended(&self) -> bool {{
match self.id() {{
Id::Standard(_) => false,
Id::Extended(_) => true,
}}
}}
fn is_remote_frame(&self) -> bool {{
false
}}
fn id(&self) -> Id {{
Self::MESSAGE_ID
}}
fn dlc(&self) -> usize {{
self.raw.len()
}}
fn data(&self) -> &[u8] {{
&self.raw
}}
}}",
type_name(msg.message_name())
)
})
}

fn render_debug_impl(mut w: impl Write, config: &Config<'_>, msg: &Message) -> Result<()> {
match &config.impl_debug {
FeatureConfig::Always => {}
Expand Down
Loading

0 comments on commit 56c4fcb

Please sign in to comment.