-
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
08b40f1
commit ba889de
Showing
7 changed files
with
82 additions
and
11 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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
mod default; | ||
pub use default::enum_trait_default; | ||
|
||
mod from; | ||
pub use from::enum_trait_from; | ||
|
||
mod default; | ||
pub use default::enum_trait_default; | ||
mod try_into; | ||
pub use try_into::enum_trait_try_into; |
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,62 @@ | ||
use proc_macro2::{Delimiter, Ident, TokenStream}; | ||
use quote::quote; | ||
use syn::Variant; | ||
|
||
use crate::attributes::Attribute; | ||
use crate::config::{build_config, build_enum_doc}; | ||
use crate::expand::Context; | ||
use crate::tokens::{destructure_data, destructure_types, get_delimiter}; | ||
|
||
build_enum_doc! { | ||
ConfigDoc, | ||
"Converts into the associated data if it is the [`{}::{}`] variant. Otherwise, returns `Err(self)`.", | ||
} | ||
|
||
build_config! { | ||
Config, | ||
(doc, ConfigDoc, false), | ||
} | ||
|
||
pub fn enum_trait_try_into( | ||
context: &Context, | ||
variant: &Variant, | ||
attribute: &Attribute, | ||
) -> 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 doc = &config.doc; | ||
let trait_ident = syn::Ident::new("TryInto", attribute.ident.span()); | ||
let method_ident = Ident::new("try_into", attribute.ident.span()); | ||
|
||
let content = quote! { | ||
type Error = Self; | ||
|
||
#[doc = #doc] | ||
fn #method_ident (self) -> Result<#ty, Self> { | ||
match self { | ||
Self:: #variant_ident #destruct => Ok(#ret), | ||
other => Err(other), | ||
} | ||
} | ||
}; | ||
|
||
Ok(context.in_impl( | ||
quote! { ::core::convert::#trait_ident<#ty> for }, | ||
&content, | ||
None, | ||
)) | ||
} |
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