Merge pull request #307 from xoviat/macros
macros: handle one enum field
This commit is contained in:
commit
8e0b734459
@ -24,8 +24,16 @@ fn impl_enum_derive(ast: &syn::DeriveInput) -> TokenStream {
|
||||
let variant_name = &v.ident;
|
||||
let variant_debug = format!("{}::{}", name, variant_name);
|
||||
|
||||
quote! {
|
||||
#name::#variant_name => #variant_debug,
|
||||
match v.fields.len() {
|
||||
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();
|
||||
@ -34,9 +42,9 @@ fn impl_enum_derive(ast: &syn::DeriveInput) -> TokenStream {
|
||||
#[automatically_derived]
|
||||
impl ::core::fmt::Debug for #name {
|
||||
fn fmt(self: &Self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||
::core::fmt::Formatter::write_str(f, match self {
|
||||
match self {
|
||||
#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,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
|
||||
#[derive(EnumDebug, Eq, PartialEq, Clone, Deserialize)]
|
||||
pub enum BlockItemInner {
|
||||
Block(BlockItemBlock),
|
||||
Register(Register),
|
||||
@ -205,7 +205,7 @@ pub mod ir {
|
||||
pub enumm: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
|
||||
#[derive(EnumDebug, Eq, PartialEq, Clone, Deserialize)]
|
||||
pub enum Array {
|
||||
Regular(RegularArray),
|
||||
Cursed(CursedArray),
|
||||
|
@ -392,12 +392,7 @@ fn stringify<T: Debug>(metadata: T) -> String {
|
||||
metadata = format!("&{}", metadata);
|
||||
}
|
||||
|
||||
metadata
|
||||
.replace(": [", ": &[")
|
||||
.replace("Register(", "BlockItemInner::Register(")
|
||||
.replace("Block(", "BlockItemInner::Block(")
|
||||
.replace("Regular(", "Array::Regular(")
|
||||
.replace("Cursed(", "Array::Cursed(")
|
||||
metadata.replace(": [", ": &[")
|
||||
}
|
||||
|
||||
fn gen_opts() -> generate::Options {
|
||||
|
Loading…
x
Reference in New Issue
Block a user