-
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.
- Loading branch information
1 parent
62f9515
commit f3e2a91
Showing
7 changed files
with
71 additions
and
10 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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use proc_macro2::{Delimiter, TokenStream}; | ||
use quote::quote; | ||
use syn::Variant; | ||
|
||
use crate::attributes::{Attribute, MethodAttribute}; | ||
use crate::config::{build_config, build_enum_doc, build_enum_name}; | ||
use crate::expand::Context; | ||
use crate::tokens::{destructure_data, destructure_types, get_delimiter}; | ||
|
||
build_enum_name! { ConfigName, "try_into_{}" } | ||
build_enum_doc! { | ||
ConfigDoc, | ||
"Converts into the associated data if it is the [`{}::{}`] variant. Otherwise, returns `None`.", | ||
} | ||
|
||
build_config! { | ||
Config, | ||
(name, ConfigName, true), | ||
(doc, ConfigDoc, false), | ||
} | ||
|
||
pub fn enum_method_try_into( | ||
context: &Context, | ||
variant: &Variant, | ||
attribute: &Attribute, | ||
method_attr: &MethodAttribute, | ||
) -> syn::Result<TokenStream> { | ||
let config = Config::new(context, attribute, variant)?; | ||
|
||
let fields = &variant.fields; | ||
let delimiter = get_delimiter(fields); | ||
|
||
let ty = destructure_types(fields, quote! {}, quote! { () }, false); | ||
let destruct = destructure_data(fields, quote! {}, quote! {}, delimiter, true); | ||
let ret = destructure_data(fields, quote! {}, quote! { () }, Delimiter::Parenthesis, false); | ||
|
||
let variant_ident = &variant.ident; | ||
let keywords = method_attr.keywords(); | ||
let doc = config.doc; | ||
let method_ident = config.name; | ||
|
||
Ok(quote! { | ||
#[doc = #doc] | ||
#keywords fn #method_ident(self) -> Result<#ty, Self> { | ||
match self { | ||
Self::#variant_ident #destruct => Ok(#ret), | ||
item => Err(item), | ||
} | ||
} | ||
}) | ||
} |
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