mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
This reverts commit 8e15389cae
.
This commit is contained in:
parent
8e15389cae
commit
d6ae8dc112
152 changed files with 4622 additions and 5862 deletions
|
@ -5,84 +5,15 @@
|
|||
use darling::util::Override;
|
||||
use darling::FromDeriveInput;
|
||||
use darling::FromField;
|
||||
use darling::FromMeta;
|
||||
use darling::FromVariant;
|
||||
use derive_common::cg;
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use quote::{ToTokens, TokenStreamExt};
|
||||
use syn::parse_quote;
|
||||
use syn::{self, Data, Ident, Path, WhereClause};
|
||||
use syn::{self, Data, Path, WhereClause};
|
||||
use synstructure::{BindingInfo, Structure, VariantInfo};
|
||||
|
||||
fn derive_bitflags(input: &syn::DeriveInput, bitflags: &CssBitflagAttrs) -> TokenStream {
|
||||
let name = &input.ident;
|
||||
let mut body = TokenStream::new();
|
||||
for (rust_name, css_name) in bitflags.single_flags() {
|
||||
let rust_ident = Ident::new(&rust_name, Span::call_site());
|
||||
body.append_all(quote! {
|
||||
if *self == Self::#rust_ident {
|
||||
return dest.write_str(#css_name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
body.append_all(quote! {
|
||||
let mut has_any = false;
|
||||
});
|
||||
|
||||
if bitflags.overlapping_bits {
|
||||
body.append_all(quote! {
|
||||
let mut serialized = Self::empty();
|
||||
});
|
||||
}
|
||||
|
||||
for (rust_name, css_name) in bitflags.mixed_flags() {
|
||||
let rust_ident = Ident::new(&rust_name, Span::call_site());
|
||||
let serialize = quote! {
|
||||
if has_any {
|
||||
dest.write_char(' ')?;
|
||||
}
|
||||
has_any = true;
|
||||
dest.write_str(#css_name)?;
|
||||
};
|
||||
if bitflags.overlapping_bits {
|
||||
body.append_all(quote! {
|
||||
if self.contains(Self::#rust_ident) && !serialized.intersects(Self::#rust_ident) {
|
||||
#serialize
|
||||
serialized.insert(Self::#rust_ident);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
body.append_all(quote! {
|
||||
if self.intersects(Self::#rust_ident) {
|
||||
#serialize
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
body.append_all(quote! {
|
||||
Ok(())
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl style_traits::ToCss for #name {
|
||||
#[allow(unused_variables)]
|
||||
#[inline]
|
||||
fn to_css<W>(
|
||||
&self,
|
||||
dest: &mut style_traits::CssWriter<W>,
|
||||
) -> std::fmt::Result
|
||||
where
|
||||
W: std::fmt::Write,
|
||||
{
|
||||
#body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn derive(mut input: syn::DeriveInput) -> TokenStream {
|
||||
let mut where_clause = input.generics.where_clause.take();
|
||||
for param in input.generics.type_params() {
|
||||
|
@ -90,21 +21,12 @@ pub fn derive(mut input: syn::DeriveInput) -> TokenStream {
|
|||
}
|
||||
|
||||
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
||||
if matches!(input.data, Data::Enum(..)) || input_attrs.bitflags.is_some() {
|
||||
if let Data::Enum(_) = input.data {
|
||||
assert!(
|
||||
input_attrs.function.is_none(),
|
||||
"#[css(function)] is not allowed on enums or bitflags"
|
||||
"#[css(function)] is not allowed on enums"
|
||||
);
|
||||
assert!(
|
||||
!input_attrs.comma,
|
||||
"#[css(comma)] is not allowed on enums or bitflags"
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(ref bitflags) = input_attrs.bitflags {
|
||||
assert!(!input_attrs.derive_debug, "Bitflags can derive debug on their own");
|
||||
assert!(where_clause.is_none(), "Generic bitflags?");
|
||||
return derive_bitflags(&input, bitflags);
|
||||
assert!(!input_attrs.comma, "#[css(comma)] is not allowed on enums");
|
||||
}
|
||||
|
||||
let match_body = {
|
||||
|
@ -327,36 +249,6 @@ fn derive_single_field_expr(
|
|||
expr
|
||||
}
|
||||
|
||||
#[derive(Default, FromMeta)]
|
||||
pub struct CssBitflagAttrs {
|
||||
/// Flags that can only go on their own, comma-separated.
|
||||
pub single: String,
|
||||
/// Flags that can go mixed with each other, comma-separated.
|
||||
pub mixed: String,
|
||||
/// Extra validation of the resulting mixed flags.
|
||||
#[darling(default)]
|
||||
pub validate_mixed: Option<Path>,
|
||||
/// Whether there are overlapping bits we need to take care of when
|
||||
/// serializing.
|
||||
#[darling(default)]
|
||||
pub overlapping_bits: bool,
|
||||
}
|
||||
|
||||
impl CssBitflagAttrs {
|
||||
/// Returns a vector of (rust_name, css_name) of a given flag list.
|
||||
fn names(s: &str) -> Vec<(String, String)> {
|
||||
s.split(',').map(|css_name| (cg::to_scream_case(css_name), css_name.to_owned())).collect()
|
||||
}
|
||||
|
||||
pub fn single_flags(&self) -> Vec<(String, String)> {
|
||||
Self::names(&self.single)
|
||||
}
|
||||
|
||||
pub fn mixed_flags(&self) -> Vec<(String, String)> {
|
||||
Self::names(&self.mixed)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, FromDeriveInput)]
|
||||
#[darling(attributes(css), default)]
|
||||
pub struct CssInputAttrs {
|
||||
|
@ -365,7 +257,6 @@ pub struct CssInputAttrs {
|
|||
pub function: Option<Override<String>>,
|
||||
// Here because structs variants are also their whole type definition.
|
||||
pub comma: bool,
|
||||
pub bitflags: Option<CssBitflagAttrs>,
|
||||
}
|
||||
|
||||
#[derive(Default, FromVariant)]
|
||||
|
@ -375,7 +266,6 @@ pub struct CssVariantAttrs {
|
|||
// Here because structs variants are also their whole type definition.
|
||||
pub derive_debug: bool,
|
||||
pub comma: bool,
|
||||
pub bitflags: Option<CssBitflagAttrs>,
|
||||
pub dimension: bool,
|
||||
pub keyword: Option<String>,
|
||||
pub skip: bool,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue