/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use darling::util::Override; usecrate::cg; use proc_macro2::{Span, TokenStream}; use quote::{ToTokens, TokenStreamExt}; use syn::{self, Data, Ident, Path, WhereClause}; use synstructure::{BindingInfo, Structure, VariantInfo};
fn derive_bitflags(input: &syn::DeriveInput, bitflags: &CssBitflagAttrs) -> TokenStream { let name = &input.ident; letmut 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);
}
});
}
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input); if matches!(input.data, Data::Enum(..)) || input_attrs.bitflags.is_some() {
assert!(
input_attrs.function.is_none(), "#[css(function)] is not allowed on enums or bitflags"
);
assert!(
!input_attrs.comma, "#[css(comma)] is not allowed on enums or bitflags"
);
}
iflet 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);
}
let match_body = { let s = Structure::new(&input);
s.each_variant(|variant| derive_variant_arm(variant, &mut where_clause))
};
input.generics.where_clause = where_clause;
let name = &input.ident; let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let (first, attrs) = match iter.next() {
Some(pair) => pair,
None => return quote! { Ok(()) },
}; if attrs.field_bound { let ty = &first.ast().ty; // TODO(emilio): IntoIterator might not be enough for every type of // iterable thing (like ArcSlice<> or what not). We might want to expose // an `item = "T"` attribute to handle that in the future. let predicate = if attrs.iterable {
parse_quote!(<#tyas IntoIterator>::Item: style_traits::ToCss)
} else {
parse_quote!(#ty: style_traits::ToCss)
};
cg::add_predicate(where_clause, predicate);
} if !attrs.iterable && iter.peek().is_none() { letmut expr = quote! { style_traits::ToCss::to_css(#first, dest) }; iflet Some(condition) = attrs.skip_if {
expr = quote! { if !#condition(#first) { #expr
}
}
}
#[derive(Default, FromMeta)] #[darling(default)] pubstruct CssBitflagAttrs { /// Flags that can only go on their own, comma-separated. pub single: Option<String>, /// Flags that can go mixed with each other, comma-separated. pub mixed: Option<String>, /// Extra validation of the resulting mixed flags. pub validate_mixed: Option<Path>, /// Whether there are overlapping bits we need to take care of when /// serializing. pub overlapping_bits: bool,
}
impl CssBitflagAttrs { /// Returns a vector of (rust_name, css_name) of a given flag list. fn names(s: &Option<String>) -> Vec<(String, String)> { let s = match s {
Some(s) => s,
None => return vec![],
};
s.split(',')
.map(|css_name| (cg::to_scream_case(css_name), css_name.to_owned()))
.collect()
}
#[derive(Default, FromDeriveInput)] #[darling(attributes(css), default)] pubstruct CssInputAttrs { pub derive_debug: bool, // Here because structs variants are also their whole type definition. 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)] #[darling(attributes(css), default)] pubstruct CssVariantAttrs { pub function: Option<Override<String>>, // 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,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.