Skip to content

Commit

Permalink
✨ Static type path can use ?: spliter now.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Jan 7, 2025
1 parent 06d12e0 commit 69c0aeb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/utils/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ pub(crate) fn flatten(
StructType::Static(v) => {
items.push((
key.clone(),
v.clone(),
match extra_type_wrapper {
ExtraTypeWrapper::Option => parse_quote! { Option<#v> },
ExtraTypeWrapper::OptionVec => parse_quote! { Option<#v> },
_ => v.clone(),
},
default_value.clone(),
extra_macros.attr_macros.clone(),
));
Expand Down Expand Up @@ -159,7 +163,11 @@ pub(crate) fn flatten(
for (ty, extra_type_wrapper) in v.iter() {
match ty {
StructType::Static(v) => {
tuple.push(v.clone());
tuple.push(match extra_type_wrapper {
ExtraTypeWrapper::Option => parse_quote! { Option<#v> },
ExtraTypeWrapper::OptionVec => parse_quote! { Option<#v> },
_ => v.clone(),
});
}
StructType::InlineStruct(v) => {
let v = v
Expand Down Expand Up @@ -264,7 +272,13 @@ pub(crate) fn flatten(
StructType::Static(v) => {
sub_items.push((
key.clone(),
v.clone(),
match extra_type_wrapper {
ExtraTypeWrapper::Option => parse_quote! { Option<#v> },
ExtraTypeWrapper::OptionVec => {
parse_quote! { Option<#v> }
}
_ => v.clone(),
},
default_value.clone(),
extra_macros.attr_macros.clone(),
));
Expand Down
29 changes: 29 additions & 0 deletions tests/option_type_struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use yuuka::derive_struct;

#[test]
fn option_type_basic() {
derive_struct!(Root {
a?: String
});

let _ = Root {
a: Some("hello".to_string()),
};
}

#[test]
fn option_type_struct() {
derive_struct!(Root {
Expand Down Expand Up @@ -45,3 +56,21 @@ fn option_type_struct_anonymous() {
}),
};
}

#[test]
fn option_type_enum_with_keys() {
derive_struct!(Root {
a?: enum AttackType {
Momoi,
Midori { b?: String },
Yuzu,
Arisu,
},
});

let _ = Root {
a: Some(AttackType::Midori {
b: Some("hello".to_string()),
}),
};
}

0 comments on commit 69c0aeb

Please sign in to comment.