Macro rsvg::make_property

source ·
macro_rules! make_property {
    ($(#[$attr:meta])*
     $name: ident,
     default: $default: ident,
     inherits_automatically: $inherits_automatically: expr,
     identifiers:
     $($str_prop: expr => $variant: ident,)+
    ) => { ... };
    ($(#[$attr:meta])*
     $name: ident,
     default: $default: ident,
     identifiers: { $($str_prop: expr => $variant: ident,)+ },
     property_impl: { $prop: item }
    ) => { ... };
    ($(#[$attr:meta])*
     $name: ident,
     default: $default: expr,
     inherits_automatically: $inherits_automatically: expr,
     newtype_parse: $type: ty,
    ) => { ... };
    ($(#[$attr:meta])*
     $name: ident,
     default: $default: expr,
     property_impl: { $prop: item }
    ) => { ... };
    ($name: ident,
     default: $default: expr,
     inherits_automatically: $inherits_automatically: expr,
    ) => { ... };
    ($name: ident,
     default: $default: expr,
     inherits_automatically: $inherits_automatically: expr,
     parse_impl: { $parse: item }
    ) => { ... };
    ($(#[$attr:meta])*
     $name: ident,
     default: $default: expr,
     newtype: $type: ty,
     property_impl: { $prop: item },
     parse_impl: { $parse: item }
    ) => { ... };
    ($(#[$attr:meta])*
     $name: ident,
     default: $default: expr,
     inherits_automatically: $inherits_automatically: expr,
     newtype: $type: ty,
     parse_impl: { $parse: item },
    ) => { ... };
    ($(#[$attr:meta])*
     $name: ident,
     inherits_automatically: $inherits_automatically: expr,
     fields: {
       $($field_name: ident : $field_type: ty, default: $field_default : expr,)+
     }
     parse_impl: { $parse: item }
    ) => { ... };
}
Expand description

Generates a type for a CSS property.

Writing a property by hand takes a bit of boilerplate:

  • Define a type to represent the property’s values.

  • A Parse implementation to parse the property.

  • A Default implementation to define the property’s initial value.

  • A Property implementation to define whether the property inherits from the parent element, and how the property derives its computed value.

When going from SpecifiedValues to ComputedValues, properties which inherit automatically from the parent element will just have their values cloned. Properties which do not inherit will be reset back to their initial value (i.e. their Default).

The default implementation of Property::compute() is to just clone the property’s value. Properties which need more sophisticated computation can override this.

This macro allows defining properties of different kinds; see the following sections for examples.

Simple identifiers

Many properties are just sets of identifiers and can be represented by simple enums. In this case, you can use the following:

make_property!(
  /// Documentation here.
  StrokeLinejoin,
  default: Miter,
  inherits_automatically: true,

  identifiers:
    "miter" => Miter,
    "round" => Round,
    "bevel" => Bevel,
);

This generates a simple enum like the following, with implementations of Parse, Default, and Property.

pub enum StrokeLinejoin { Miter, Round, Bevel }

Properties from an existing, general-purpose type

For example, both the lightingColor and floodColor properties can be represented with a cssparser::Color, but their intial values are different. In this case, the macro can generate a newtype around cssparser::Color for each case:

make_property!(
    /// Documentation here.
    FloodColor,
    default: cssparser::Color::RGBA(cssparser::RGBA::new(0, 0, 0, 0)),
    inherits_automatically: false,
    newtype_parse: cssparser::Color,
);

Properties from custom specific types

For example, font-related properties have custom, complex types that require an implentation of Property::compute that is more than a simple clone. In this case, define the custom type separately, and use the macro to specify the default value and the Property implementation.