Trait style_traits::values::ToCss
source · pub trait ToCss {
// Required method
fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Result
where W: Write;
// Provided method
fn to_css_string(&self) -> String { ... }
}
Expand description
Serialises a value according to its CSS representation.
This trait is implemented for str
and its friends, serialising the string
contents as a CSS quoted string.
This trait is derivable with #[derive(ToCss)]
, with the following behaviour:
-
unit variants get serialised as the
snake-case
representation of their name; -
unit variants whose name starts with “Moz” or “Webkit” are prepended with a “-”;
-
if
#[css(comma)]
is found on a variant, its fields are separated by commas, otherwise, by spaces; -
if
#[css(function)]
is found on a variant, the variant name gets serialised like unit variants and its fields are surrounded by parentheses; -
if
#[css(iterable)]
is found on a function variant, that variant needs to have a single member, and that member needs to be iterable. The iterable will be serialized as the arguments for the function; -
an iterable field can also be annotated with
#[css(if_empty = "foo")]
to print"foo"
if the iterator is empty; -
if
#[css(dimension)]
is found on a variant, that variant needs to have a single member. The variant would be serialized as a CSS dimension token, like:; -
if
#[css(skip)]
is found on a field, theToCss
call for that field is skipped; -
if
#[css(skip_if = "function")]
is found on a field, theToCss
call for that field is skipped iffunction
returns true. This function is provided the field as an argument; -
if
#[css(contextual_skip_if = "function")]
is found on a field, theToCss
call for that field is skipped iffunction
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 serialize the field name if the field is true, or nothing otherwise. It also collects those keywords forSpecifiedValueInfo
. -
#[css(bitflags(single="", mixed="", validate_mixed="", overlapping_bits)]
can be used to derive parse / serialize / etc on bitflags. The rules for parsing bitflags are the following:-
single
flags can only appear on their own. It’s common that bitflags properties at least have one such value likenone
orauto
. -
mixed
properties can appear mixed together, but not along any other flag that shares a bit with itself. For example, if you have three bitflags like:FOO = 1 << 0; BAR = 1 << 1; BAZ = 1 << 2; BAZZ = BAR | BAZ;
Then the following combinations won’t be valid:
- foo foo: (every flag shares a bit with itself)
- bar bazz: (bazz shares a bit with bar)
But
bar baz
will be valid, as they don’t share bits, and so wouldfoo
with any other flag, orbazz
on its own. -
validate_mixed
can be used to reject invalid mixed combinations, and also to simplify the type or add default ones if needed. -
overlapping_bits
enables some tracking during serialization of mixed flags to avoid serializing variants that can subsume other variants. In the example above, you could do: mixed=“foo,bazz,bar,baz”, overlapping_bits to ensure that if bazz is serialized, bar and baz aren’t, even though their bits are set. Note that the serialization order is canonical, and thus depends on the order you specify the flags in.
-
-
finally, one can put
#[css(derive_debug)]
on the whole type, to implementDebug
by a single call toToCss::to_css
.
Required Methods§
Provided Methods§
sourcefn to_css_string(&self) -> String
fn to_css_string(&self) -> String
Serialize self
in CSS syntax and return a string.
(This is a convenience wrapper for to_css
and probably should not be overridden.)