use crate::properties::ComputedValues;
pub trait Property {
fn inherits_automatically() -> bool;
fn compute(&self, _: &ComputedValues) -> Self;
}
#[doc(hidden)]
#[macro_export]
macro_rules! make_property {
($(#[$attr:meta])*
$name: ident,
default: $default: ident,
inherits_automatically: $inherits_automatically: expr,
identifiers:
$($str_prop: expr => $variant: ident,)+
) => {
$(#[$attr])*
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(C)]
pub enum $name {
$($variant),+
}
impl_default!($name, $name::$default);
impl_property!($name, $inherits_automatically);
impl $crate::parsers::Parse for $name {
fn parse<'i>(parser: &mut ::cssparser::Parser<'i, '_>) -> Result<$name, $crate::error::ParseError<'i>> {
Ok(parse_identifiers!(
parser,
$($str_prop => $name::$variant,)+
)?)
}
}
};
($(#[$attr:meta])*
$name: ident,
default: $default: ident,
identifiers: { $($str_prop: expr => $variant: ident,)+ },
property_impl: { $prop: item }
) => {
$(#[$attr])*
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(C)]
pub enum $name {
$($variant),+
}
impl_default!($name, $name::$default);
$prop
impl $crate::parsers::Parse for $name {
fn parse<'i>(parser: &mut ::cssparser::Parser<'i, '_>) -> Result<$name, $crate::error::ParseError<'i>> {
Ok(parse_identifiers!(
parser,
$($str_prop => $name::$variant,)+
)?)
}
}
};
($(#[$attr:meta])*
$name: ident,
default: $default: expr,
inherits_automatically: $inherits_automatically: expr,
newtype_parse: $type: ty,
) => {
$(#[$attr])*
#[derive(Debug, Clone, PartialEq)]
pub struct $name(pub $type);
impl_default!($name, $name($default));
impl_property!($name, $inherits_automatically);
impl $crate::parsers::Parse for $name {
fn parse<'i>(parser: &mut ::cssparser::Parser<'i, '_>) -> Result<$name, $crate::error::ParseError<'i>> {
Ok($name(<$type as $crate::parsers::Parse>::parse(parser)?))
}
}
};
($(#[$attr:meta])*
$name: ident,
default: $default: expr,
property_impl: { $prop: item }
) => {
impl_default!($name, $default);
$prop
};
($name: ident,
default: $default: expr,
inherits_automatically: $inherits_automatically: expr,
) => {
impl_default!($name, $default);
impl_property!($name, $inherits_automatically);
};
($name: ident,
default: $default: expr,
inherits_automatically: $inherits_automatically: expr,
parse_impl: { $parse: item }
) => {
impl_default!($name, $default);
impl_property!($name, $inherits_automatically);
$parse
};
($(#[$attr:meta])*
$name: ident,
default: $default: expr,
newtype: $type: ty,
property_impl: { $prop: item },
parse_impl: { $parse: item }
) => {
$(#[$attr])*
#[derive(Debug, Clone, PartialEq)]
pub struct $name(pub $type);
impl_default!($name, $name($default));
$prop
$parse
};
($(#[$attr:meta])*
$name: ident,
default: $default: expr,
inherits_automatically: $inherits_automatically: expr,
newtype: $type: ty,
parse_impl: { $parse: item },
) => {
$(#[$attr])*
#[derive(Debug, Clone, PartialEq)]
pub struct $name(pub $type);
impl_default!($name, $name($default));
impl_property!($name, $inherits_automatically);
$parse
};
($(#[$attr:meta])*
$name: ident,
inherits_automatically: $inherits_automatically: expr,
fields: {
$($field_name: ident : $field_type: ty, default: $field_default : expr,)+
}
parse_impl: { $parse: item }
) => {
$(#[$attr])*
#[derive(Debug, Clone, PartialEq)]
pub struct $name {
$(pub $field_name: $field_type),+
}
impl_default!($name, $name { $($field_name: $field_default),+ });
impl_property!($name, $inherits_automatically);
$parse
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! impl_default {
($name:ident, $default:expr) => {
impl Default for $name {
fn default() -> $name {
$default
}
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! impl_property {
($name:ident, $inherits_automatically:expr) => {
impl $crate::property_macros::Property for $name {
fn inherits_automatically() -> bool {
$inherits_automatically
}
fn compute(&self, _v: &$crate::properties::ComputedValues) -> Self {
self.clone()
}
}
};
}