pub trait ToTyped {
// Provided methods
fn to_typed(&self, _dest: &mut ThinVec<TypedValue>) -> Result<(), ()> { ... }
fn to_typed_value(&self) -> Option<TypedValue> { ... }
fn to_typed_value_list(&self) -> Option<TypedValueList> { ... }
}Expand description
Reifies a value into its Typed OM representation.
This trait is the Typed OM analogue of ToCss. Instead of serializing
values into CSS syntax, it converts them into TypedValues that can be
exposed to the DOM as CSSStyleValue subclasses.
Most consumers should use ToTyped::to_typed_value or
ToTyped::to_typed_value_list, depending on whether they need a single
reified value or the full list of reified values.
This trait is derivable with #[derive(ToTyped)]. The derived
implementation currently supports:
-
Keyword enums: Enums whose variants are all unit variants are automatically reified as
TypedValue::Keyword, using the same serialization logic asToCss. -
Structs and data-carrying variants: When the
#[typed_value(derive_fields)]attribute is present, the derive attempts to call.to_typed()recursively on supported fields or variant payloads, producingTypedValues when possible. -
Other cases: If no automatic mapping is defined or recursion is not enabled, the derived implementation falls back to the default method (which returns
Err(()), and thusto_typed_value()returnsNone).
The derive_fields attribute is intentionally opt-in for now to avoid
forcing types that do not participate in reification to implement
ToTyped. Once Typed OM coverage stabilizes, this behavior is expected
to become the default (see the corresponding follow-up bug).
Over time, the derive may be extended to handle additional CSS value categories such as numeric, color, and transform types.
Summary of derive attributes recognized by #[derive(ToTyped)]:
-
#[typed_value(derive_fields)]on the type enables limited recursion for structs and data-carrying enum variants. -
#[css(skip)],#[typed_value(skip)], or#[typed_value(todo)]on a variant cause that variant to be treated as unsupported (the derived implementation returnsErr(())). -
#[css(skip)]on a field causes that field to be ignored during reification. -
#[typed_value(skip_if = "...")]on a field conditionally disables reification for that field. If the provided function returnstruefor the field value, the field is ignored. -
#[css(keyword = "...")]on a unit variant overrides the keyword that would otherwise be derived from the Rust identifier. -
#[css(comma)]on the variant indicates that supported fields may reify to multiple separate values. When this attribute is present, multipleTypedValueitems may be produced. If it is not present and the derived implementation would produce more than one item, it returnsErr(()). -
#[css(iterable)]on a field indicates that the field represents a list of values. Each item in the iterable is reified individually by callingToTyped::to_typedon the element type. -
#[css(if_empty = "...")]on an iterable field specifies a keyword value that should be produced when the iterable is empty.
Provided Methods§
Sourcefn to_typed(&self, _dest: &mut ThinVec<TypedValue>) -> Result<(), ()>
fn to_typed(&self, _dest: &mut ThinVec<TypedValue>) -> Result<(), ()>
Attempt to convert self into one or more TypedValue items.
Implementations append any resulting values to dest. This is the
low-level entry point used by the Typed OM reification infrastructure.
Most callers should prefer ToTyped::to_typed_value or
ToTyped::to_typed_value_list.
Returning Err(()) indicates that the value cannot be represented as
a property-agnostic Typed OM value.
Sourcefn to_typed_value(&self) -> Option<TypedValue>
fn to_typed_value(&self) -> Option<TypedValue>
Attempt to convert self into a TypedValue.
Returns the first reified value as Some(TypedValue) if the value can
be reified into a property-agnostic CSSStyleValue subclass. Returns
None if the value is unrepresentable, in which case consumers
produce a property-tied CSSStyleValue instead.
Sourcefn to_typed_value_list(&self) -> Option<TypedValueList>
fn to_typed_value_list(&self) -> Option<TypedValueList>
Attempt to convert self into a TypedValueList.
Returns Some(TypedValueList) if the value can be reified into one or
more property-agnostic Typed OM values. Returns None if the value is
unrepresentable, in which case consumers produce a property-tied
CSSStyleValue instead.