-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor #133
Merged
Merged
Refactor #133
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69081f9
Remove unnecessary main in doctest
jplatte c851efe
Use syn::parse_macro_input!
jplatte 0875888
Add spans to attribute parse errors
jplatte 5dac450
Add spans to more attribute errors
jplatte 498177c
Fix indentation
jplatte 146732f
Consistently import syn AST types
jplatte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,56 @@ | ||
///Represents a type that can have strum metadata associated with it. | ||
use syn::{Attribute, DeriveInput, Meta, NestedMeta, Variant}; | ||
|
||
/// Represents a type that can have strum metadata associated with it. | ||
pub trait HasMetadata { | ||
/// Get all the metadata associated with a specific "tag". | ||
/// All of strum's metadata is nested inside a path such as | ||
/// #[strum(...)] so this let's us quickly filter down to only our metadata. | ||
fn get_metadata(&self, ident: &str) -> Vec<syn::Meta>; | ||
fn get_metadata(&self, ident: &str) -> syn::Result<Vec<Meta>>; | ||
} | ||
|
||
fn get_metadata_inner<'a>( | ||
ident: &str, | ||
it: impl IntoIterator<Item = &'a syn::Attribute>, | ||
) -> Vec<syn::Meta> { | ||
it.into_iter() | ||
.filter(|attr| attr.path.is_ident(ident)) | ||
.map(|attr| attr.parse_meta().unwrap()) | ||
.filter_map(|meta| match meta { | ||
syn::Meta::List(syn::MetaList { path, nested, .. }) => { | ||
if path.is_ident(ident) { | ||
Some(nested) | ||
} else { | ||
None | ||
it: impl IntoIterator<Item = &'a Attribute>, | ||
) -> syn::Result<Vec<Meta>> { | ||
let mut res = Vec::new(); | ||
|
||
for attr in it { | ||
if !attr.path.is_ident(ident) { | ||
continue; | ||
} | ||
|
||
let meta = attr.parse_meta()?; | ||
let nested = match meta { | ||
Meta::List(syn::MetaList { nested, .. }) => nested, | ||
_ => { | ||
return Err(syn::Error::new_spanned( | ||
meta, | ||
"unrecognized strum attribute form", | ||
)) | ||
} | ||
}; | ||
|
||
for nested_meta in nested { | ||
match nested_meta { | ||
NestedMeta::Meta(meta) => res.push(meta), | ||
NestedMeta::Lit(lit) => { | ||
return Err(syn::Error::new_spanned(lit, "unexpected literal")) | ||
} | ||
} | ||
_ => None, | ||
}) | ||
.flat_map(|id| id) | ||
.map(|nested| match nested { | ||
syn::NestedMeta::Meta(meta) => meta, | ||
_ => panic!("unexpected literal parsing strum attributes"), | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
Ok(res) | ||
} | ||
|
||
impl HasMetadata for syn::Variant { | ||
fn get_metadata(&self, ident: &str) -> Vec<syn::Meta> { | ||
impl HasMetadata for Variant { | ||
fn get_metadata(&self, ident: &str) -> syn::Result<Vec<Meta>> { | ||
get_metadata_inner(ident, &self.attrs) | ||
} | ||
} | ||
|
||
impl HasMetadata for syn::DeriveInput { | ||
fn get_metadata(&self, ident: &str) -> Vec<syn::Meta> { | ||
impl HasMetadata for DeriveInput { | ||
fn get_metadata(&self, ident: &str) -> syn::Result<Vec<Meta>> { | ||
get_metadata_inner(ident, &self.attrs) | ||
} | ||
} |
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,63 +1,63 @@ | ||
use syn::{Meta, MetaList, NestedMeta}; | ||
use syn::{Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path}; | ||
|
||
pub trait MetaHelpers { | ||
fn expect_metalist(&self, msg: &str) -> &MetaList; | ||
fn expect_path(&self, msg: &str) -> &syn::Path; | ||
fn expect_namevalue(&self, msg: &str) -> &syn::MetaNameValue; | ||
fn expect_metalist(&self, msg: &str) -> syn::Result<&MetaList>; | ||
fn expect_path(&self, msg: &str) -> syn::Result<&Path>; | ||
fn expect_namevalue(&self, msg: &str) -> syn::Result<&MetaNameValue>; | ||
} | ||
|
||
impl MetaHelpers for syn::Meta { | ||
fn expect_metalist(&self, msg: &str) -> &MetaList { | ||
impl MetaHelpers for Meta { | ||
fn expect_metalist(&self, msg: &str) -> syn::Result<&MetaList> { | ||
match self { | ||
Meta::List(list) => list, | ||
_ => panic!("{}", msg), | ||
Meta::List(list) => Ok(list), | ||
_ => Err(syn::Error::new_spanned(self, msg)), | ||
} | ||
} | ||
|
||
fn expect_path(&self, msg: &str) -> &syn::Path { | ||
fn expect_path(&self, msg: &str) -> syn::Result<&Path> { | ||
match self { | ||
Meta::Path(path) => path, | ||
_ => panic!("{}", msg), | ||
Meta::Path(path) => Ok(path), | ||
_ => Err(syn::Error::new_spanned(self, msg)), | ||
} | ||
} | ||
|
||
fn expect_namevalue(&self, msg: &str) -> &syn::MetaNameValue { | ||
fn expect_namevalue(&self, msg: &str) -> syn::Result<&MetaNameValue> { | ||
match self { | ||
Meta::NameValue(pair) => pair, | ||
_ => panic!("{}", msg), | ||
Meta::NameValue(pair) => Ok(pair), | ||
_ => Err(syn::Error::new_spanned(self, msg)), | ||
} | ||
} | ||
} | ||
|
||
pub trait NestedMetaHelpers { | ||
fn expect_meta(&self, msg: &str) -> &syn::Meta; | ||
fn expect_lit(&self, msg: &str) -> &syn::Lit; | ||
fn expect_meta(&self, msg: &str) -> syn::Result<&Meta>; | ||
fn expect_lit(&self, msg: &str) -> syn::Result<&Lit>; | ||
} | ||
|
||
impl NestedMetaHelpers for NestedMeta { | ||
fn expect_meta(&self, msg: &str) -> &Meta { | ||
fn expect_meta(&self, msg: &str) -> syn::Result<&Meta> { | ||
match self { | ||
syn::NestedMeta::Meta(m) => m, | ||
_ => panic!("{}", msg), | ||
NestedMeta::Meta(m) => Ok(m), | ||
_ => Err(syn::Error::new_spanned(self, msg)), | ||
} | ||
} | ||
fn expect_lit(&self, msg: &str) -> &syn::Lit { | ||
fn expect_lit(&self, msg: &str) -> syn::Result<&Lit> { | ||
match self { | ||
syn::NestedMeta::Lit(l) => l, | ||
_ => panic!("{}", msg), | ||
NestedMeta::Lit(l) => Ok(l), | ||
_ => Err(syn::Error::new_spanned(self, msg)), | ||
} | ||
} | ||
} | ||
|
||
pub trait LitHelpers { | ||
fn expect_string(&self, msg: &str) -> String; | ||
fn expect_string(&self, msg: &str) -> syn::Result<String>; | ||
} | ||
|
||
impl LitHelpers for syn::Lit { | ||
fn expect_string(&self, msg: &str) -> String { | ||
impl LitHelpers for Lit { | ||
fn expect_string(&self, msg: &str) -> syn::Result<String> { | ||
match self { | ||
syn::Lit::Str(s) => s.value(), | ||
_ => panic!("{}", msg), | ||
Lit::Str(s) => Ok(s.value()), | ||
_ => Err(syn::Error::new_spanned(self, msg)), | ||
} | ||
} | ||
} |
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,38 +1,39 @@ | ||
use std::convert::From; | ||
use std::default::Default; | ||
use syn::{DeriveInput, Lit, Meta, Path}; | ||
|
||
use crate::helpers::case_style::CaseStyle; | ||
use crate::helpers::has_metadata::HasMetadata; | ||
use crate::helpers::{MetaHelpers, NestedMetaHelpers}; | ||
|
||
pub trait HasTypeProperties { | ||
fn get_type_properties(&self) -> StrumTypeProperties; | ||
fn get_type_properties(&self) -> syn::Result<StrumTypeProperties>; | ||
} | ||
|
||
#[derive(Debug, Clone, Eq, PartialEq, Default)] | ||
pub struct StrumTypeProperties { | ||
pub case_style: Option<CaseStyle>, | ||
pub discriminant_derives: Vec<syn::Path>, | ||
pub discriminant_name: Option<syn::Path>, | ||
pub discriminant_others: Vec<syn::Meta>, | ||
pub discriminant_derives: Vec<Path>, | ||
pub discriminant_name: Option<Path>, | ||
pub discriminant_others: Vec<Meta>, | ||
} | ||
|
||
impl HasTypeProperties for syn::DeriveInput { | ||
fn get_type_properties(&self) -> StrumTypeProperties { | ||
impl HasTypeProperties for DeriveInput { | ||
fn get_type_properties(&self) -> syn::Result<StrumTypeProperties> { | ||
let mut output = StrumTypeProperties::default(); | ||
|
||
let strum_meta = self.get_metadata("strum"); | ||
let discriminants_meta = self.get_metadata("strum_discriminants"); | ||
let strum_meta = self.get_metadata("strum")?; | ||
let discriminants_meta = self.get_metadata("strum_discriminants")?; | ||
|
||
for meta in strum_meta { | ||
let meta = match meta { | ||
syn::Meta::NameValue(mv) => mv, | ||
Meta::NameValue(mv) => mv, | ||
_ => panic!("strum on types only supports key-values"), | ||
}; | ||
|
||
if meta.path.is_ident("serialize_all") { | ||
let style = match meta.lit { | ||
syn::Lit::Str(s) => s.value(), | ||
Lit::Str(s) => s.value(), | ||
_ => panic!("expected string value for 'serialize_all'"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well. |
||
}; | ||
|
||
|
@@ -48,12 +49,16 @@ impl HasTypeProperties for syn::DeriveInput { | |
|
||
for meta in discriminants_meta { | ||
match meta { | ||
syn::Meta::List(ref ls) => { | ||
Meta::List(ref ls) => { | ||
if ls.path.is_ident("derive") { | ||
let paths = ls | ||
.nested | ||
.iter() | ||
.map(|meta| meta.expect_meta("unexpected literal").path().clone()); | ||
.map(|meta| { | ||
let meta = meta.expect_meta("unexpected literal")?; | ||
Ok(meta.path().clone()) | ||
}) | ||
.collect::<syn::Result<Vec<_>>>()?; | ||
|
||
output.discriminant_derives.extend(paths); | ||
} else if ls.path.is_ident("name") { | ||
|
@@ -63,8 +68,8 @@ impl HasTypeProperties for syn::DeriveInput { | |
|
||
let value = ls.nested.first().expect("unexpected error"); | ||
let name = value | ||
.expect_meta("unexpected literal") | ||
.expect_path("name must be an identifier"); | ||
.expect_meta("unexpected literal")? | ||
.expect_path("name must be an identifier")?; | ||
|
||
if output.discriminant_name.is_some() { | ||
panic!("multiple occurrences of 'name'"); | ||
|
@@ -81,6 +86,6 @@ impl HasTypeProperties for syn::DeriveInput { | |
} | ||
} | ||
|
||
output | ||
Ok(output) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we return an Err here was well?