Skip to content

Commit

Permalink
Enum emplacer namespace, re-export string module
Browse files Browse the repository at this point in the history
  • Loading branch information
agerasev committed Jun 9, 2024
1 parent 2d14813 commit ac3ba2b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ proc-macro = true
syn = "1.0"
quote = "1.0"
proc-macro2 = "1.0"
convert_case = "0.6.0"
25 changes: 25 additions & 0 deletions macros/src/items/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::{
utils::{generic, type_list},
Context,
};
use convert_case::{Case, Casing};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
punctuated::Punctuated, spanned::Spanned, token::Comma, Data, DeriveInput, Field, Fields, GenericParam, Ident, Index,
Visibility,
};

struct Param<'a> {
Expand Down Expand Up @@ -130,6 +132,7 @@ pub fn struct_(ctx: &Context, input: &DeriveInput) -> TokenStream {
Data::Enum(data) => {
let mut items = quote! {};
let mut variants = quote! {};
let mut namespace_items = quote! {};
for var in data.variants.iter() {
let var_ident = &var.ident;
let prefix = format!("{}__{}", prefix, var_ident);
Expand All @@ -140,6 +143,11 @@ pub fn struct_(ctx: &Context, input: &DeriveInput) -> TokenStream {
};

let var_init_ident = variant_ident(init_ident, var_ident);
namespace_items = quote! {
#namespace_items
#var_init_ident as #var_ident,
};

let var_args = make_args(fields_params(&var.fields, "").iter());
let (body, semi) = collect_fields(&var.fields, "", true);
items = quote! {
Expand All @@ -151,7 +159,24 @@ pub fn struct_(ctx: &Context, input: &DeriveInput) -> TokenStream {
}

let args = make_args(params.iter().flatten());

let namespace_name = Ident::new(&init_ident.to_string().as_str().to_case(Case::Snake), init_ident.span());
let namespace = if let Visibility::Inherited = vis {
quote! {}
} else {
quote! {
#[allow(unused_imports)]
#vis mod #namespace_name {
pub use super::{
#namespace_items
};
}
}
};

quote! {
#namespace

#[allow(non_camel_case_types)]
#vis enum #init_ident<#args> { #variants }

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub use flatty_base::{
pub use flatty_containers::bytes::AlignedBytes;
pub use flatty_containers::{
flex::{self, flex_vec, FlexVec},
string::FlatString,
string::{self, FlatString},
vec::{self, flat_vec, FlatVec},
wrap::{FlatWrap, TrustedRef},
};
Expand Down

0 comments on commit ac3ba2b

Please sign in to comment.