-
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.
Merge pull request #39 from jewlexx/strip-enum
Add derive macro to strip enum of data
- Loading branch information
Showing
4 changed files
with
253 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
use proc_macro2::{Ident, TokenStream}; | ||
use proc_macro_error2::{abort, abort_call_site}; | ||
use quote::{quote, ToTokens}; | ||
use syn::{spanned::Spanned, DeriveInput, Meta, Variant, Visibility}; | ||
|
||
fn ignore_variant(variant: &Variant) -> bool { | ||
variant.attrs.iter().any(|attr| match attr.meta { | ||
syn::Meta::List(ref list) if list.path.is_ident("stripped") => { | ||
let mut ignored = false; | ||
|
||
let list_parser = syn::meta::parser(|meta| { | ||
if meta.path.is_ident("ignore") { | ||
ignored = true; | ||
Ok(()) | ||
} else { | ||
Err(meta.error("unsupported property")) | ||
} | ||
}); | ||
|
||
if let Err(err) = list.parse_args_with(list_parser) { | ||
abort! { | ||
err.span(), | ||
"Failed to parse attribute: {}", err; | ||
help = "Only supported properties on enum variants are `ignore`" | ||
} | ||
} | ||
|
||
ignored | ||
} | ||
_ => abort!( | ||
attr.span(), | ||
"Expected list-style (i.e #[stripped(...)]), found other style attribute macro" | ||
), | ||
}) | ||
} | ||
|
||
struct StrippedData { | ||
ident: Ident, | ||
variants: Vec<TokenStream>, | ||
meta: Vec<Meta>, | ||
vis: Visibility, | ||
} | ||
|
||
// struct MetaArgs { | ||
// meta: Vec<Expr>, | ||
// } | ||
|
||
// impl Parse for MetaArgs { | ||
// fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { | ||
// input.peek3(token) | ||
// } | ||
// } | ||
|
||
pub fn strip_enum(ast: &mut DeriveInput) -> TokenStream { | ||
let data = &ast.data; | ||
let attrs = &mut ast.attrs; | ||
|
||
let info: StrippedData = match data { | ||
syn::Data::Enum(ref e) => { | ||
let variants = e | ||
.variants | ||
.iter() | ||
.filter_map(|variant| { | ||
if ignore_variant(variant) { | ||
None | ||
} else { | ||
Some(variant.ident.to_token_stream()) | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let default_ident = { | ||
let ident = ast.ident.clone(); | ||
let span = ident.span(); | ||
move || Ident::new(&format!("{ident}Stripped"), span) | ||
}; | ||
|
||
let new_ident = if let Some(info_attr_pos) = attrs | ||
.iter() | ||
.position(|attr| attr.path().is_ident("stripped")) | ||
{ | ||
let info_attr = attrs.remove(info_attr_pos); | ||
|
||
let mut new_ident: Option<Ident> = None; | ||
|
||
let ident_parser = syn::meta::parser(|meta| { | ||
if meta.path.is_ident("ident") { | ||
new_ident = Some(meta.value()?.parse()?); | ||
Ok(()) | ||
} else { | ||
Err(meta.error("unsupported property")) | ||
} | ||
}); | ||
|
||
if let Err(err) = info_attr.parse_args_with(ident_parser) { | ||
abort! { | ||
err.span(), | ||
"Failed to parse attribute: {}", err; | ||
help = "Only supported properties on enum definitions are `ident`" | ||
} | ||
} | ||
|
||
new_ident.unwrap_or_else(default_ident) | ||
} else { | ||
default_ident() | ||
}; | ||
|
||
let meta_list: Vec<syn::Meta> = attrs | ||
.iter() | ||
.filter(|attr| attr.path().is_ident("stripped_meta")) | ||
.flat_map(|meta_attr| match &meta_attr.meta { | ||
Meta::List(meta_data) => match meta_data.parse_args::<syn::Meta>() { | ||
Ok(meta) => vec![meta], | ||
Err(err) => { | ||
abort! { | ||
err.span(), | ||
"Failed to parse specified metadata: {}", err; | ||
help = "Make sure the provided arguments are in the form of Rust metadata. (i.e the tokens contained within `#[...]`)" | ||
} | ||
} | ||
}, | ||
// Meta::NameValue(MetaNameValue { | ||
// value: | ||
// syn::Expr::Lit(syn::ExprLit { | ||
// lit: syn::Lit::Str(path), | ||
// .. | ||
// }), | ||
// .. | ||
// }) => { | ||
// if &path.value() == "inherit" { | ||
// attrs | ||
// .iter() | ||
// .filter(|attr| !attr.path().is_ident("stripped_meta")) | ||
// .map(|attr| attr.meta.clone()) | ||
// .collect() | ||
// } else { | ||
// abort!(path.span(), "Expected `inherit`"); | ||
// } | ||
// } | ||
_ => abort!( | ||
meta_attr.span(), | ||
"Expected #[stripped_meta(...)]. Found other style attribute." | ||
), | ||
}) | ||
.collect(); | ||
|
||
StrippedData { | ||
ident: new_ident, | ||
variants, | ||
meta: meta_list, | ||
vis: ast.vis.clone(), | ||
} | ||
} | ||
_ => abort_call_site!("`Strip` can only be derived for enums"), | ||
}; | ||
|
||
let StrippedData { | ||
ident, | ||
variants, | ||
meta, | ||
vis, | ||
} = info; | ||
|
||
// panic!("{:?}", meta); | ||
|
||
quote! { | ||
#(#[#meta])* | ||
#vis enum #ident { | ||
#(#variants),* | ||
} | ||
} | ||
} |
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,60 @@ | ||
#![allow(dead_code)] | ||
|
||
use std::fmt::Display; | ||
|
||
use strum::{Display, EnumIter, IntoEnumIterator}; | ||
|
||
use quork_proc::Strip; | ||
|
||
pub fn enum_to_string<T: IntoEnumIterator + Display>() -> String { | ||
T::iter().map(|v| v.to_string()).collect::<String>() | ||
} | ||
|
||
struct DummyStruct; | ||
|
||
#[derive(Strip)] | ||
#[stripped_meta(derive(EnumIter, Display))] | ||
#[stripped_meta(strum(serialize_all = "kebab-case"))] | ||
enum EnumWithData { | ||
Test1(DummyStruct), | ||
Test2(DummyStruct), | ||
} | ||
|
||
#[test] | ||
fn has_all_variants() { | ||
let variants = enum_to_string::<EnumWithDataStripped>(); | ||
|
||
assert_eq!(variants, "test1test2"); | ||
} | ||
|
||
#[derive(Strip)] | ||
#[stripped_meta(derive(EnumIter, Display))] | ||
#[stripped_meta(strum(serialize_all = "kebab-case"))] | ||
enum EnumExclude { | ||
Test1(DummyStruct), | ||
#[stripped(ignore)] | ||
Test2(DummyStruct), | ||
Test3(DummyStruct), | ||
} | ||
|
||
#[derive(Strip)] | ||
#[stripped_meta(derive(EnumIter))] | ||
#[stripped_meta(strum(serialize_all = "kebab-case"))] | ||
enum EnumWithInherit { | ||
Test1(DummyStruct), | ||
} | ||
|
||
#[derive(Strip)] | ||
#[stripped_meta(derive(EnumIter))] | ||
#[stripped_meta(strum(serialize_all = "kebab-case"))] | ||
#[stripped(ident = IChoseThisIdent)] | ||
enum EnumWithCustomIdent { | ||
Test1(DummyStruct), | ||
} | ||
|
||
#[test] | ||
fn excludes_no_hook_variant() { | ||
let variants = enum_to_string::<EnumExcludeStripped>(); | ||
|
||
assert_eq!(variants, "test1test3"); | ||
} |