@@ -60,6 +60,10 @@ pub struct Config<'a> {
60
60
#[ builder( default ) ]
61
61
pub impl_debug : FeatureConfig < ' a > ,
62
62
63
+ /// Optional: `impl defmt::Format` for generated types. Default: `Never`.
64
+ #[ builder( default ) ]
65
+ pub impl_defmt : FeatureConfig < ' a > ,
66
+
63
67
/// Optional: `impl Arbitrary` for generated types. Default: `Never`.
64
68
#[ builder( default ) ]
65
69
pub impl_arbitrary : FeatureConfig < ' a > ,
@@ -189,6 +193,9 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
189
193
writeln ! ( w, "/// All messages" ) ?;
190
194
writeln ! ( w, "#[derive(Clone)]" ) ?;
191
195
config. impl_debug . fmt_attr ( & mut w, "derive(Debug)" ) ?;
196
+ config
197
+ . impl_defmt
198
+ . fmt_attr ( & mut w, "derive(defmt::Format)" ) ?;
192
199
config. impl_serde . fmt_attr ( & mut w, "derive(Serialize)" ) ?;
193
200
config. impl_serde . fmt_attr ( & mut w, "derive(Deserialize)" ) ?;
194
201
writeln ! ( w, "pub enum Messages {{" ) ?;
@@ -430,6 +437,8 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D
430
437
431
438
render_debug_impl ( & mut w, config, msg) ?;
432
439
440
+ render_defmt_impl ( & mut w, config, msg) ?;
441
+
433
442
render_arbitrary ( & mut w, config, msg) ?;
434
443
435
444
let enums_for_this_message = dbc. value_descriptions ( ) . iter ( ) . filter_map ( |x| {
@@ -984,6 +993,9 @@ fn write_enum(
984
993
writeln ! ( w, "/// Defined values for {}" , signal. name( ) ) ?;
985
994
writeln ! ( w, "#[derive(Clone, Copy, PartialEq)]" ) ?;
986
995
config. impl_debug . fmt_attr ( & mut w, "derive(Debug)" ) ?;
996
+ config
997
+ . impl_defmt
998
+ . fmt_attr ( & mut w, "derive(defmt::Format)" ) ?;
987
999
config. impl_serde . fmt_attr ( & mut w, "derive(Serialize)" ) ?;
988
1000
config. impl_serde . fmt_attr ( & mut w, "derive(Deserialize)" ) ?;
989
1001
writeln ! ( w, "pub enum {} {{" , type_name) ?;
@@ -1385,6 +1397,48 @@ fn render_debug_impl(mut w: impl Write, config: &Config<'_>, msg: &Message) -> R
1385
1397
Ok ( ( ) )
1386
1398
}
1387
1399
1400
+ fn render_defmt_impl ( mut w : impl Write , config : & Config < ' _ > , msg : & Message ) -> Result < ( ) > {
1401
+ match & config. impl_defmt {
1402
+ FeatureConfig :: Always => { }
1403
+ FeatureConfig :: Gated ( gate) => writeln ! ( w, r##"#[cfg(feature = {gate:?})]"## ) ?,
1404
+ FeatureConfig :: Never => return Ok ( ( ) ) ,
1405
+ }
1406
+
1407
+ let typ = type_name ( msg. message_name ( ) ) ;
1408
+ writeln ! ( w, r##"impl defmt::Format for {} {{"## , typ) ?;
1409
+ {
1410
+ let mut w = PadAdapter :: wrap ( & mut w) ;
1411
+ writeln ! ( w, "fn format(&self, f: defmt::Formatter) {{" ) ?;
1412
+ {
1413
+ let mut w = PadAdapter :: wrap ( & mut w) ;
1414
+ writeln ! ( w, r#"defmt::write!(f,"# ) ?;
1415
+ {
1416
+ let mut w = PadAdapter :: wrap ( & mut w) ;
1417
+ write ! ( w, r#""{} {{{{"# , typ) ?;
1418
+ {
1419
+ for signal in msg. signals ( ) {
1420
+ if * signal. multiplexer_indicator ( ) == MultiplexIndicator :: Plain {
1421
+ write ! ( w, r#" {}={{:?}}"# , signal. name( ) , ) ?;
1422
+ }
1423
+ }
1424
+ }
1425
+ writeln ! ( w, r#" }}}}","# ) ?;
1426
+
1427
+ for signal in msg. signals ( ) {
1428
+ if * signal. multiplexer_indicator ( ) == MultiplexIndicator :: Plain {
1429
+ writeln ! ( w, "self.{}()," , field_name( signal. name( ) ) ) ?;
1430
+ }
1431
+ }
1432
+ writeln ! ( w, r#");"# ) ?;
1433
+ }
1434
+ writeln ! ( w, "}}" ) ?;
1435
+ }
1436
+ }
1437
+ writeln ! ( w, "}}" ) ?;
1438
+ writeln ! ( w) ?;
1439
+ Ok ( ( ) )
1440
+ }
1441
+
1388
1442
fn render_multiplexor_enums (
1389
1443
mut w : impl Write ,
1390
1444
config : & Config < ' _ > ,
@@ -1416,6 +1470,9 @@ fn render_multiplexor_enums(
1416
1470
) ?;
1417
1471
1418
1472
config. impl_debug . fmt_attr ( & mut w, "derive(Debug)" ) ?;
1473
+ config
1474
+ . impl_defmt
1475
+ . fmt_attr ( & mut w, "derive(defmt::Format)" ) ?;
1419
1476
config. impl_serde . fmt_attr ( & mut w, "derive(Serialize)" ) ?;
1420
1477
config. impl_serde . fmt_attr ( & mut w, "derive(Deserialize)" ) ?;
1421
1478
writeln ! (
@@ -1443,6 +1500,9 @@ fn render_multiplexor_enums(
1443
1500
let struct_name = multiplexed_enum_variant_name ( msg, multiplexor_signal, * * switch_index) ?;
1444
1501
1445
1502
config. impl_debug . fmt_attr ( & mut w, "derive(Debug)" ) ?;
1503
+ config
1504
+ . impl_defmt
1505
+ . fmt_attr ( & mut w, "derive(defmt::Format)" ) ?;
1446
1506
config. impl_serde . fmt_attr ( & mut w, "derive(Serialize)" ) ?;
1447
1507
config. impl_serde . fmt_attr ( & mut w, "derive(Deserialize)" ) ?;
1448
1508
writeln ! ( w, r##"#[derive(Default)]"## ) ?;
0 commit comments