Skip to content

Commit

Permalink
⚡️ Add unique prefix for anonymous structures' name.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Oct 11, 2024
1 parent f633257 commit e7e384a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
1 change: 1 addition & 0 deletions yuuka-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn derive_struct(input: TokenStream) -> TokenStream {
};
let mod_ident = syn::Ident::new(&format!("__{}", root_ident), root_ident.span());
let (structs, enums) = flatten(
root_ident.to_string(),
Rc::new(RefCell::new(0)),
utils::DeriveBox::Struct(input.clone()),
vec![],
Expand Down
4 changes: 2 additions & 2 deletions yuuka-macros/src/utils/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub struct DeriveEnum {
}

impl DeriveEnum {
pub fn pin_unique_id(&self, id: Rc<RefCell<usize>>) -> Self {
pub fn pin_unique_id(&self, root_name: String, id: Rc<RefCell<usize>>) -> Self {
let mut ret = self.clone();
ret.ident = ret.ident.pin_unique_id(*id.borrow());
ret.ident = ret.ident.pin_unique_id(root_name, *id.borrow());
*id.borrow_mut() += 1;
ret
}
Expand Down
4 changes: 2 additions & 2 deletions yuuka-macros/src/utils/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub struct DeriveStruct {
}

impl DeriveStruct {
pub fn pin_unique_id(&self, id: Rc<RefCell<usize>>) -> Self {
pub fn pin_unique_id(&self, root_name: String, id: Rc<RefCell<usize>>) -> Self {
let mut ret = self.clone();
ret.ident = ret.ident.pin_unique_id(*id.borrow());
ret.ident = ret.ident.pin_unique_id(root_name, *id.borrow());
*id.borrow_mut() += 1;
ret
}
Expand Down
85 changes: 61 additions & 24 deletions yuuka-macros/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@ pub(crate) enum DefaultValue {
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum StructName {
Named(Ident),
Unnamed(Option<usize>),
Unnamed(Option<(String, usize)>),
}

impl StructName {
pub(crate) fn to_ident(&self) -> Result<Ident, syn::Error> {
Ok(match self {
StructName::Named(v) => v.clone(),
StructName::Unnamed(v) => Ident::new(
&format!(
"_{}_anonymous",
v.ok_or(syn::Error::new(
proc_macro2::Span::call_site(),
"Unnamed struct is not supported"
))?
),
StructName::Unnamed(Some((root_name, v))) => Ident::new(
&format!("_{}_{}_anonymous", root_name, v),
proc_macro2::Span::call_site(),
),
_ => {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
"Unnamed struct is not supported",
))
}
})
}

pub(crate) fn pin_unique_id(&self, unique_id: usize) -> Self {
pub(crate) fn pin_unique_id(&self, root_name: String, unique_id: usize) -> Self {
match self {
StructName::Named(v) => StructName::Named(v.clone()),
StructName::Unnamed(v) => {
if let Some(v) = v {
StructName::Unnamed(Some(*v))
StructName::Unnamed(Some(v.clone()))
} else {
StructName::Unnamed(Some(unique_id))
StructName::Unnamed(Some((root_name, unique_id)))
}
}
}
Expand Down Expand Up @@ -102,6 +102,7 @@ pub(crate) enum DeriveBox {
}

pub(crate) fn flatten(
root_name: String,
unique_id_count: Rc<RefCell<usize>>,
parent: DeriveBox,
parent_extra_macros: ExtraMacros,
Expand All @@ -124,8 +125,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineStruct(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Struct(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()].concat(),
Expand All @@ -143,8 +147,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineStructVector(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Struct(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()].concat(),
Expand All @@ -162,8 +169,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineEnum(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Enum(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()].concat(),
Expand All @@ -181,8 +191,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineEnumVector(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Enum(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()].concat(),
Expand Down Expand Up @@ -226,8 +239,11 @@ pub(crate) fn flatten(
tuple.push(v.clone());
}
StructType::InlineStruct(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Struct(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand All @@ -241,8 +257,11 @@ pub(crate) fn flatten(
tuple.push(parse_quote! { #ty });
}
StructType::InlineStructVector(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Struct(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand All @@ -256,8 +275,11 @@ pub(crate) fn flatten(
tuple.push(parse_quote! { Vec<#ty> });
}
StructType::InlineEnum(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Enum(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand All @@ -271,8 +293,11 @@ pub(crate) fn flatten(
tuple.push(parse_quote! { #ty });
}
StructType::InlineEnumVector(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Enum(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand Down Expand Up @@ -306,8 +331,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineStruct(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Struct(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand All @@ -326,8 +354,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineStructVector(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Struct(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand All @@ -346,8 +377,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineEnum(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Enum(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand All @@ -366,8 +400,11 @@ pub(crate) fn flatten(
));
}
StructType::InlineEnumVector(v) => {
let v = v.clone().pin_unique_id(unique_id_count.clone());
let v = v
.clone()
.pin_unique_id(root_name.clone(), unique_id_count.clone());
let (sub_structs, sub_enums) = flatten(
root_name.clone(),
unique_id_count.clone(),
DeriveBox::Enum(v.clone()),
[parent.extra_macros.clone(), v.extra_macros.clone()]
Expand Down
4 changes: 2 additions & 2 deletions yuuka/tests/anonymous_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ mod test {

let mut root = Root::default();
root.a.push(Default::default());
assert_eq!(root.a, vec![__Root::_0_anonymous::Midori]);
assert_eq!(root.a, vec![__Root::_Root_0_anonymous::Midori]);
let mut root2 = Root2::default();
root2.a.push(Default::default());
assert_eq!(root2.a, vec![__Root2::_0_anonymous::Arisu(233)]);
assert_eq!(root2.a, vec![__Root2::_Root2_0_anonymous::Arisu(233)]);
}
}

0 comments on commit e7e384a

Please sign in to comment.