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
Allow deriving Parse on keywords. #19578
Merged
+145
−51
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Allow deriving Parse for keywords.
- Loading branch information
emilio
committed
Dec 15, 2017
commit 7036cb0077d7b9c051822e4417bc66803f81dea8
Unverified
This user has not uploaded their public key yet.
GPG key ID: 056B727BB9C1027C
Learn about signing commits
| @@ -0,0 +1,74 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| use cg; | ||
| use quote::Tokens; | ||
| use syn::DeriveInput; | ||
| use synstructure; | ||
|
|
||
| pub fn derive(input: DeriveInput) -> Tokens { | ||
| let name = &input.ident; | ||
|
|
||
| let mut match_body = quote! {}; | ||
|
|
||
| let style = synstructure::BindStyle::Ref.into(); | ||
| synstructure::each_variant(&input, &style, |bindings, variant| { | ||
| assert!( | ||
| bindings.is_empty(), | ||
| "Parse is only supported for single-variant enums for now" | ||
| ); | ||
|
|
||
| let identifier = cg::to_css_identifier(variant.ident.as_ref()); | ||
| match_body = quote! { | ||
| #match_body | ||
| #identifier => Ok(#name::#variant), | ||
| } | ||
| }); | ||
|
|
||
| let parse_trait_impl = quote! { | ||
| impl ::parser::Parse for #name { | ||
| #[inline] | ||
| fn parse<'i, 't>( | ||
| _: &::parser::ParserContext, | ||
| input: &mut ::cssparser::Parser<'i, 't>, | ||
| ) -> Result<Self, ::style_traits::ParseError<'i>> { | ||
| Self::parse(input) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // TODO(emilio): It'd be nice to get rid of these, but that makes the | ||
| // conversion harder... | ||
| let methods_impl = quote! { | ||
| impl #name { | ||
| /// Parse this keyword. | ||
| #[inline] | ||
| pub fn parse<'i, 't>( | ||
| input: &mut ::cssparser::Parser<'i, 't>, | ||
| ) -> Result<Self, ::style_traits::ParseError<'i>> { | ||
| let location = input.current_source_location(); | ||
| let ident = input.expect_ident()?; | ||
| Self::from_ident(ident.as_ref()).map_err(|()| { | ||
| location.new_unexpected_token_error( | ||
| ::cssparser::Token::Ident(ident.clone()) | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /// Parse this keyword from a string slice. | ||
| #[inline] | ||
| pub fn from_ident(ident: &str) -> Result<Self, ()> { | ||
| match_ignore_ascii_case! { ident, | ||
| #match_body | ||
| _ => Err(()), | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| quote! { | ||
| #parse_trait_impl | ||
| #methods_impl | ||
| } | ||
| } |
ProTip!
Use n and p to navigate between commits in a pull request.
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.
We should also add
"Servo"here.