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, the ToCss call for that field is skipped;

  • 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 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 serialize the field name if the field is true, or nothing otherwise. It also collects those keywords for SpecifiedValueInfo.

  • #[css(bitflags(single="", mixed="", validate="", 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 like none or auto.

    • 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 would foo with any other flag, or bazz on its own.

    • 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 implement Debug by a single call to ToCss::to_css.

Required Methods§

source

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

Serialize self in CSS syntax, writing to dest.

Provided Methods§

source

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.)

Implementations on Foreign Types§

source§

impl<'a> ToCss for u16

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a> ToCss for Token<'a>

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl ToCss for str

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a> ToCss for i32

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<T> ToCss for Box<T>where T: ?Sized + ToCss,

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a> ToCss for UnicodeRange

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a> ToCss for u32

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a> ToCss for i8

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl ToCss for Au

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl ToCss for String

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<T> ToCss for Option<T>where T: ToCss,

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<T> ToCss for Vec<T>where T: ToCss + OneOrMoreSeparated,

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a> ToCss for f32

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<T> ToCss for Arc<T>where T: ?Sized + ToCss,

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl<'a, T> ToCss for &'a Twhere T: ToCss + ?Sized,

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

impl ToCss for ()

source§

fn to_css<W>(&self, _: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

Implementors§