Merge pull request #307 from xoviat/macros

macros: handle one enum field
This commit is contained in:
xoviat 2023-11-07 00:07:08 +00:00 committed by GitHub
commit 8e0b734459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 12 deletions

View File

@ -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
}) }
} }
} }
} }

View 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,
}

View File

@ -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),

View File

@ -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 {