macros: handle one enum field
This commit is contained in:
parent
1374ed6227
commit
69706e61e4
@ -24,8 +24,16 @@ fn impl_enum_derive(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
let variant_name = &v.ident;
|
let variant_name = &v.ident;
|
||||||
let variant_debug = format!("{}::{}", name, variant_name);
|
let variant_debug = format!("{}::{}", name, variant_name);
|
||||||
|
|
||||||
quote! {
|
match v.fields.len() {
|
||||||
#name::#variant_name => #variant_debug,
|
0 => quote! {
|
||||||
|
#name::#variant_name => ::core::fmt::Formatter::write_str(f, #variant_debug),
|
||||||
|
},
|
||||||
|
1 => quote! {
|
||||||
|
#name::#variant_name(__self_0) => ::core::fmt::Formatter::debug_tuple(f, #variant_debug)
|
||||||
|
.field(&__self_0)
|
||||||
|
.finish(),
|
||||||
|
},
|
||||||
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@ -34,9 +42,9 @@ fn impl_enum_derive(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl ::core::fmt::Debug for #name {
|
impl ::core::fmt::Debug for #name {
|
||||||
fn fmt(self: &Self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
fn fmt(self: &Self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||||
::core::fmt::Formatter::write_str(f, match self {
|
match self {
|
||||||
#match_variants
|
#match_variants
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
stm32-data-macros/tests/test_macros.rs
Normal file
14
stm32-data-macros/tests/test_macros.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use stm32_data_macros::EnumDebug;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct A {
|
||||||
|
pub b: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(EnumDebug)]
|
||||||
|
enum C {
|
||||||
|
D(A),
|
||||||
|
E,
|
||||||
|
}
|
@ -159,7 +159,7 @@ pub mod ir {
|
|||||||
pub inner: BlockItemInner,
|
pub inner: BlockItemInner,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
|
#[derive(EnumDebug, Eq, PartialEq, Clone, Deserialize)]
|
||||||
pub enum BlockItemInner {
|
pub enum BlockItemInner {
|
||||||
Block(BlockItemBlock),
|
Block(BlockItemBlock),
|
||||||
Register(Register),
|
Register(Register),
|
||||||
@ -205,7 +205,7 @@ pub mod ir {
|
|||||||
pub enumm: Option<String>,
|
pub enumm: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
|
#[derive(EnumDebug, Eq, PartialEq, Clone, Deserialize)]
|
||||||
pub enum Array {
|
pub enum Array {
|
||||||
Regular(RegularArray),
|
Regular(RegularArray),
|
||||||
Cursed(CursedArray),
|
Cursed(CursedArray),
|
||||||
|
@ -392,12 +392,7 @@ fn stringify<T: Debug>(metadata: T) -> String {
|
|||||||
metadata = format!("&{}", metadata);
|
metadata = format!("&{}", metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata
|
metadata.replace(": [", ": &[")
|
||||||
.replace(": [", ": &[")
|
|
||||||
.replace("Register(", "BlockItemInner::Register(")
|
|
||||||
.replace("Block(", "BlockItemInner::Block(")
|
|
||||||
.replace("Regular(", "Array::Regular(")
|
|
||||||
.replace("Cursed(", "Array::Cursed(")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_opts() -> generate::Options {
|
fn gen_opts() -> generate::Options {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user