Skip to content

Commit

Permalink
✨ Now we can use option type's declaration by ?:.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Dec 27, 2024
1 parent edbe38c commit 421029a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 252 deletions.
14 changes: 8 additions & 6 deletions src/tools/derive_enum_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use syn::{
token, Ident, Token, TypePath,
};

use crate::tools::ExtraTypeWrapper;

use super::{
DeriveEnum, DeriveStruct, DeriveStructItems, EnumMembers, EnumValue, ExtraMacros, StructType,
};
Expand Down Expand Up @@ -37,7 +39,7 @@ impl Parse for DeriveEnumItems {
// Ident(...),
let sub_content;
parenthesized!(sub_content in input);
let mut tuple: Vec<StructType> = Vec::new();
let mut tuple: Vec<(StructType, ExtraTypeWrapper)> = vec![];

while !sub_content.is_empty() {
if sub_content.peek(token::Bracket) {
Expand All @@ -60,7 +62,7 @@ impl Parse for DeriveEnumItems {
content
};

tuple.push(StructType::InlineEnumVector(content));
tuple.push((StructType::InlineEnum(content), ExtraTypeWrapper::Vec));
} else {
// Ident([Ident { ... }], ...),
// Ident([{ ... }], ...),
Expand All @@ -76,7 +78,7 @@ impl Parse for DeriveEnumItems {
content
};

tuple.push(StructType::InlineStructVector(content));
tuple.push((StructType::InlineStruct(content), ExtraTypeWrapper::Vec));
}
} else if sub_content.peek(Token![enum]) {
// Ident(enum Ident { ... }, ...),
Expand All @@ -92,7 +94,7 @@ impl Parse for DeriveEnumItems {
content
};

tuple.push(StructType::InlineEnum(content));
tuple.push((StructType::InlineEnum(content), ExtraTypeWrapper::Default));
} else if sub_content.peek2(token::Brace) {
// Ident(Ident { ... }, ...),
// Ident({ ... }, ...),
Expand All @@ -107,11 +109,11 @@ impl Parse for DeriveEnumItems {
content
};

tuple.push(StructType::InlineStruct(content));
tuple.push((StructType::InlineStruct(content), ExtraTypeWrapper::Default));
} else {
// Ident (TypePath, ...),
let ty: TypePath = sub_content.parse()?;
tuple.push(StructType::Static(ty));
tuple.push((StructType::Static(ty), ExtraTypeWrapper::Default));
}

if sub_content.peek(Token![,]) {
Expand Down
37 changes: 35 additions & 2 deletions src/tools/derive_struct_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use syn::{
token, Expr, Ident, Token, TypePath,
};

use crate::tools::ExtraTypeWrapper;

use super::{DefaultValue, DeriveEnum, DeriveStruct, ExtraMacros, StructMembers, StructType};

#[derive(Debug, Clone)]
Expand All @@ -23,6 +25,12 @@ impl Parse for DeriveStructItems {
};

let key = input.parse::<Ident>()?;
let optional = if input.peek(Token![?]) {
input.parse::<Token![?]>()?;
true
} else {
false
};
input.parse::<Token![:]>()?;

if input.peek(token::Bracket) {
Expand All @@ -45,7 +53,12 @@ impl Parse for DeriveStructItems {

own_struct.push((
key,
StructType::InlineEnumVector(content),
StructType::InlineEnum(content),
if optional {
ExtraTypeWrapper::OptionVec
} else {
ExtraTypeWrapper::Vec
},
{
if input.peek(Token![=]) {
input.parse::<Token![=]>()?;
Expand Down Expand Up @@ -92,7 +105,12 @@ impl Parse for DeriveStructItems {

own_struct.push((
key,
StructType::InlineStructVector(content),
StructType::InlineStruct(content),
if optional {
ExtraTypeWrapper::OptionVec
} else {
ExtraTypeWrapper::Vec
},
{
if input.peek(Token![=]) {
input.parse::<Token![=]>()?;
Expand Down Expand Up @@ -141,6 +159,11 @@ impl Parse for DeriveStructItems {
own_struct.push((
key.clone(),
StructType::InlineEnum(content),
if optional {
ExtraTypeWrapper::Option
} else {
ExtraTypeWrapper::Default
},
{
if input.peek(Token![=]) {
input.parse::<Token![=]>()?;
Expand All @@ -167,6 +190,11 @@ impl Parse for DeriveStructItems {
own_struct.push((
key.clone(),
StructType::InlineStruct(content),
if optional {
ExtraTypeWrapper::Option
} else {
ExtraTypeWrapper::Default
},
DefaultValue::None,
extra_macros,
));
Expand All @@ -177,6 +205,11 @@ impl Parse for DeriveStructItems {
own_struct.push((
key,
StructType::Static(ty),
if optional {
ExtraTypeWrapper::Option
} else {
ExtraTypeWrapper::Default
},
{
if input.peek(Token![=]) {
input.parse::<Token![=]>()?;
Expand Down
21 changes: 17 additions & 4 deletions src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,37 @@ impl StructName {
pub(crate) enum StructType {
Static(TypePath),
InlineStruct(DeriveStruct),
InlineStructVector(DeriveStruct),
InlineEnum(DeriveEnum),
InlineEnumVector(DeriveEnum),
}

#[derive(Debug, Clone)]
pub(crate) enum EnumValue {
Empty,
Tuple(Vec<StructType>),
Tuple(Vec<(StructType, ExtraTypeWrapper)>),
Struct(StructMembers),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum ExtraTypeWrapper {
Default,
Vec,
Option,
OptionVec,
}

#[derive(Debug, Clone)]
pub(crate) struct ExtraMacrosFlatten {
pub(crate) derive_macros: Vec<TypePath>,
pub(crate) attr_macros: Vec<TokenStream>,
}

pub(crate) type StructMembers = Vec<(Ident, StructType, DefaultValue, ExtraMacros)>;
pub(crate) type StructMembers = Vec<(
Ident,
StructType,
ExtraTypeWrapper,
DefaultValue,
ExtraMacros,
)>;
pub(crate) type EnumMembers = Vec<(Ident, EnumValue, ExtraMacros)>;

#[derive(Debug, Clone)]
Expand Down
Loading

0 comments on commit 421029a

Please sign in to comment.