Skip to content

Commit

Permalink
style: Implement a version of #[css(skip_if)] that takes more context.
Browse files Browse the repository at this point in the history
I called it contextual_skip_if, though better names welcome.

Differential Revision: https://phabricator.services.mozilla.com/D21858
  • Loading branch information
emilio committed Mar 13, 2019
1 parent 94686df commit 35b8b95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 20 additions & 2 deletions components/style_derive/to_css.rs
Expand Up @@ -148,12 +148,20 @@ fn derive_variant_fields_expr(
} }
} }
} }

if let Some(condition) = attrs.contextual_skip_if {
expr = quote! {
if !#condition(#(#bindings),*) {
#expr
}
}
}
return expr; return expr;
} }


let mut expr = derive_single_field_expr(first, attrs, where_clause); let mut expr = derive_single_field_expr(first, attrs, where_clause, bindings);
for (binding, attrs) in iter { for (binding, attrs) in iter {
derive_single_field_expr(binding, attrs, where_clause).to_tokens(&mut expr) derive_single_field_expr(binding, attrs, where_clause, bindings).to_tokens(&mut expr)
} }


quote! {{ quote! {{
Expand All @@ -167,6 +175,7 @@ fn derive_single_field_expr(
field: &BindingInfo, field: &BindingInfo,
attrs: CssFieldAttrs, attrs: CssFieldAttrs,
where_clause: &mut Option<WhereClause>, where_clause: &mut Option<WhereClause>,
bindings: &[BindingInfo],
) -> TokenStream { ) -> TokenStream {
let mut expr = if attrs.iterable { let mut expr = if attrs.iterable {
if let Some(if_empty) = attrs.if_empty { if let Some(if_empty) = attrs.if_empty {
Expand Down Expand Up @@ -216,6 +225,14 @@ fn derive_single_field_expr(
} }
} }


if let Some(condition) = attrs.contextual_skip_if {
expr = quote! {
if !#condition(#(#bindings),*) {
#expr
}
}
}

expr expr
} }


Expand Down Expand Up @@ -249,5 +266,6 @@ pub struct CssFieldAttrs {
pub iterable: bool, pub iterable: bool,
pub skip: bool, pub skip: bool,
pub represents_keyword: bool, pub represents_keyword: bool,
pub contextual_skip_if: Option<Path>,
pub skip_if: Option<Path>, pub skip_if: Option<Path>,
} }
4 changes: 4 additions & 0 deletions components/style_traits/values.rs
Expand Up @@ -37,6 +37,10 @@ use std::fmt::{self, Write};
/// * if `#[css(skip_if = "function")]` is found on a field, the `ToCss` call /// * if `#[css(skip_if = "function")]` is found on a field, the `ToCss` call
/// for that field is skipped if `function` returns true. This function is /// for that field is skipped if `function` returns true. This function is
/// provided the field as an argument; /// provided the field as an argument;
/// * if `#[css(contextual_skip_if = "function")]` is found on a field, the
/// `ToCss` call for that field is skipped if `function` returns true. This
/// function is given all the fields in the current struct or variant as an
/// argument;
/// * `#[css(represents_keyword)]` can be used on bool fields in order to /// * `#[css(represents_keyword)]` can be used on bool fields in order to
/// serialize the field name if the field is true, or nothing otherwise. It /// serialize the field name if the field is true, or nothing otherwise. It
/// also collects those keywords for `SpecifiedValueInfo`. /// also collects those keywords for `SpecifiedValueInfo`.
Expand Down

0 comments on commit 35b8b95

Please sign in to comment.