-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Support single level enums'
auto!
macro first.
- Loading branch information
Showing
5 changed files
with
240 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,82 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::Ident; | ||
|
||
use crate::tools::EnumsFlatten; | ||
use crate::tools::{EnumValueFlatten, EnumsFlatten}; | ||
|
||
pub(crate) fn generate_enums_auto_macros(enums: EnumsFlatten) -> Vec<TokenStream> { | ||
vec![] | ||
enums | ||
.iter() | ||
.map(|(ident, v, _default_value, _extra_macros)| { | ||
let rules = v | ||
.iter() | ||
.map(|(name, ty, _)| match ty { | ||
EnumValueFlatten::Empty => { | ||
quote! {} | ||
} | ||
EnumValueFlatten::Struct(items) => { | ||
let list = items | ||
.iter() | ||
.map(|(key, ty, _default_value, _extra_macros)| { | ||
quote! { | ||
(#name #key { $($val:tt)+ }) => { | ||
::yuuka::auto!(#ty { $($val)+ }) | ||
}; | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
quote! { | ||
#(#list)* | ||
} | ||
} | ||
EnumValueFlatten::Tuple(items) => { | ||
if items.len() == 1 { | ||
let ty = items.first().expect("Failed to get first item"); | ||
quote! { | ||
(#name { $($val:tt)+ }) => { | ||
::yuuka::auto!(#ty { $($val)+ }) | ||
}; | ||
} | ||
} else { | ||
let list = items | ||
.iter() | ||
.enumerate() | ||
.map(|(i, ty)| { | ||
let i = syn::Index::from(i); | ||
quote! { | ||
(#name #i { $($val:tt)+ }) => { | ||
::yuuka::auto!(#ty { $($val)+ }) | ||
}; | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
quote! { | ||
#(#list)* | ||
} | ||
} | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
let rules = quote! { | ||
#(#rules)* | ||
}; | ||
|
||
let ident = Ident::new(format!("__auto_{}", ident).as_str(), ident.span()); | ||
quote! { | ||
#[doc(hidden)] | ||
macro_rules! #ident { | ||
() => {}; | ||
|
||
#rules | ||
|
||
($name:ident $key:ident $val:expr) => { | ||
$val | ||
}; | ||
($name:ident $val:expr) => { | ||
$val | ||
}; | ||
} | ||
} | ||
}) | ||
.collect::<Vec<_>>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters