@@ -143,6 +143,7 @@ pub fn codegen(config: Config<'_>, out: impl Write) -> Result<()> {
143
143
writeln ! ( & mut w) ?;
144
144
writeln ! ( & mut w, "use core::ops::BitOr;" ) ?;
145
145
writeln ! ( & mut w, "use bitvec::prelude::*;" ) ?;
146
+ writeln ! ( & mut w, "use embedded_can::{{Id, StandardId, ExtendedId}};" ) ?;
146
147
147
148
writeln ! ( w, r##"#[cfg(feature = "arb")]"## ) ?;
148
149
writeln ! ( & mut w, "use arbitrary::{{Arbitrary, Unstructured}};" ) ?;
@@ -210,7 +211,7 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
210
211
writeln ! ( w, "#[inline(never)]" ) ?;
211
212
writeln ! (
212
213
& mut w,
213
- "pub fn from_can_message(id: u32 , payload: &[u8]) -> Result<Self, CanError> {{" ,
214
+ "pub fn from_can_message(id: Id , payload: &[u8]) -> Result<Self, CanError> {{" ,
214
215
) ?;
215
216
{
216
217
let mut w = PadAdapter :: wrap ( & mut w) ;
@@ -221,12 +222,11 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
221
222
for msg in get_relevant_messages ( dbc) {
222
223
writeln ! (
223
224
w,
224
- "{} => Messages::{1}({1}::try_from(payload)?)," ,
225
- msg. message_id( ) . 0 ,
225
+ "{0}::MESSAGE_ID => Messages::{0}({0}::try_from(payload)?)," ,
226
226
type_name( msg. message_name( ) )
227
227
) ?;
228
228
}
229
- writeln ! ( w, r#"n => return Err(CanError::UnknownMessageId(n )),"# ) ?;
229
+ writeln ! ( w, r#"id => return Err(CanError::UnknownMessageId(id )),"# ) ?;
230
230
}
231
231
writeln ! ( & mut w, "}};" ) ?;
232
232
writeln ! ( & mut w, "Ok(res)" ) ?;
@@ -243,7 +243,10 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
243
243
fn render_message ( mut w : impl Write , config : & Config < ' _ > , msg : & Message , dbc : & DBC ) -> Result < ( ) > {
244
244
writeln ! ( w, "/// {}" , msg. message_name( ) ) ?;
245
245
writeln ! ( w, "///" ) ?;
246
- writeln ! ( w, "/// - ID: {0} (0x{0:x})" , msg. message_id( ) . 0 ) ?;
246
+ match msg. message_id ( ) {
247
+ can_dbc:: MessageId :: Standard ( id) => writeln ! ( w, "/// - Standard ID: {0} (0x{0:x})" , id) ,
248
+ can_dbc:: MessageId :: Extended ( id) => writeln ! ( w, "/// - Extended ID: {0} (0x{0:x})" , id) ,
249
+ } ?;
247
250
writeln ! ( w, "/// - Size: {} bytes" , msg. message_size( ) ) ?;
248
251
if let can_dbc:: Transmitter :: NodeName ( transmitter) = msg. transmitter ( ) {
249
252
writeln ! ( w, "/// - Transmitter: {}" , transmitter) ?;
@@ -274,8 +277,18 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D
274
277
275
278
writeln ! (
276
279
& mut w,
277
- "pub const MESSAGE_ID: u32 = {};" ,
278
- msg. message_id( ) . 0
280
+ "pub const MESSAGE_ID: embedded_can::Id = {};" ,
281
+ match msg. message_id( ) {
282
+ // use StandardId::new().unwrap() once const_option is stable
283
+ can_dbc:: MessageId :: Standard ( id) => format!(
284
+ "Id::Standard(unsafe {{ StandardId::new_unchecked({0:#x})}})" ,
285
+ id
286
+ ) ,
287
+ can_dbc:: MessageId :: Extended ( id) => format!(
288
+ "Id::Extended(unsafe {{ ExtendedId::new_unchecked({0:#x})}})" ,
289
+ id
290
+ ) ,
291
+ }
279
292
) ?;
280
293
writeln ! ( w) ?;
281
294
@@ -623,8 +636,8 @@ fn render_set_signal(
623
636
let mut w = PadAdapter :: wrap ( & mut w) ;
624
637
writeln ! (
625
638
w,
626
- r##"return Err(CanError::ParameterOutOfRange {{ message_id: {message_id} }});"## ,
627
- message_id = msg. message_id ( ) . 0 ,
639
+ r##"return Err(CanError::ParameterOutOfRange {{ message_id: {}::MESSAGE_ID }});"## ,
640
+ type_name ( msg. message_name ( ) )
628
641
) ?;
629
642
}
630
643
writeln ! ( w, r"}}" ) ?;
@@ -742,8 +755,8 @@ fn render_multiplexor_signal(
742
755
}
743
756
writeln ! (
744
757
& mut w,
745
- "multiplexor => Err(CanError::InvalidMultiplexor {{ message_id: {}, multiplexor: multiplexor.into() }})," ,
746
- msg. message_id ( ) . 0
758
+ "multiplexor => Err(CanError::InvalidMultiplexor {{ message_id: {}::MESSAGE_ID , multiplexor: multiplexor.into() }})," ,
759
+ type_name ( msg. message_name ( ) )
747
760
) ?;
748
761
}
749
762
@@ -914,8 +927,7 @@ fn signal_to_payload(mut w: impl Write, signal: &Signal, msg: &Message) -> Resul
914
927
}
915
928
writeln ! (
916
929
& mut w,
917
- " .ok_or(CanError::ParameterOutOfRange {{ message_id: {} }})?;" ,
918
- msg. message_id( ) . 0 ,
930
+ " .ok_or(CanError::ParameterOutOfRange {{ message_id: Self::MESSAGE_ID }})?;" ,
919
931
) ?;
920
932
writeln ! (
921
933
& mut w,
0 commit comments