technocreatives / dbc-codegen

Generate Rust structs for messages from a dbc (CAN bus definition) file.
Apache License 2.0
48 stars 27 forks source link

Consider "decoded" structs #34

Open killercup opened 3 years ago

killercup commented 3 years ago

In addition to the "lazy" and "embedded friendly" structs with getters we have now, it would be neat to add a fully parsed representation as well.

I'm imagining something like

struct MessageA { raw: [u8; 8] }

struct MessageAParsed {
  signal_1: u8,
  signal_2: f64,
}

impl DecodeCan for MessageA {
  type Output = MessageAParsed;
  fn decode(&self) -> Result<Self::Output, CanError> {
    Ok(MessageAParsed {
      signal_1: self.signal_1()?,
      signal_2: self.signal_2()?,
    })
  }
}

impl EncodeCan for MessageAParsed {
  type Output = MessageA;
  fn encode(&self) -> Result<Self::Out, CanError> { … }
}

as a first step and the maybe also doing a DecodedMessage enum to go all in.

Let me know if you think this is of any use to you in practice :)