Derive Macro xylem::Xylem[][src]

#[derive(Xylem)]
{
    // Attributes available to this derive:
    #[xylem]
}
Expand description

Derives a Xylem implementation for a struct or enum and the corresponding Xylem::From type.

In this page, “input” refers to the struct/enum written by the user manually, and “derived” refers to the struct/enum generated by the macro to be used as the Xylem::From type.

The derived type has the same structure as the input type with each field type Type replaced by <Type as Xylem<S>>::From, except otherwise specified by the field attributes. Conversion takes place by mapping each field of the derived type to the corresponding field of the input type by calling Xylem::convert.

Container Attributes

The following attributes can be applied on the input.

#[xylem(expose = Ident)]

Expose the derived type with the specified name Ident. The type has the same visibility as the input type.

#[xylem(schema = path::to::Schema)]

Specify the schema type to define for as path::to::Schema.

#[xylem(serde(xxx))]

Apply the serde attribute xxx to the derived type.

#[xylem(derive(Foo, Bar))]

Apply the derive macros Foo, Bar on the derived type.

#[xylem(process)]

Call Processable::preprocess before conversion, call Processable::postprocess after conversion.

Requires the input type to implement the Processable trait.

Field Attributes

The following attributes can be applied on the fields in the input. As above, “input field” refers to the field written by the user manually, and “derived field” refers to the field generated by the macro that occur in the derived type. Furthermore, the following assumes that the attribute is applied on foo: Bar unless otherwise specified.

#[xylem(serde(xxx))]

Apply the serde attribute xxx to the derived field.

#[xylem(preserve)]:

The derived field will use the same type as the input field, i.e. #[xylem(preserve)] foo: Bar generats foo: Bar in the derived type. The value in the derived field is directly moved to the target field.

#[xylem(transform = path(Type))]

Generate a field foo: Type in the derived type, and call path(derived.foo) during conversion. path is the path to a function with the signature fn(Type) -> Result<Bar, S::Error>. For example, #[xylem(transform = Ok(Type))] is equivalent to #[xylem(preserve)].

#[xylem(transform_with_context = path(Type))]

Similar to transform, except path accepts an extra context parameter, giving the signature fn(Type, &mut S::Context) -> Result<Field, S::Error>.

#[xylem(default = expr)]

Always uses expr (resolved every time the struct or the enum variant is constructed) as the value. Does not generate a field in the From type. The expression should have type Result<Field, S::Error>, where Field is the field type.

Comparing default, preserve, transform and transform_with_context:

  • If a corresponding field is required in the derived type,
    • If they have different types,
      • If context is required, use transform_with_context.
      • Otherwise, use transform.
    • Otherwise, use preserve.
  • Otherwise, use default.

#[xylem(args(key1 = value1, key2 = value2))]

Pass the given arguments in the Xylem::convert call. Incompatible with default, preserve, transform and transform_with_context. key1 and key2 are visible named fields in <Bar as Xylem<S>>::Args. The values in the key are evaluated lazily and stored as a static. The generated code is equivalent to the following:

lazy_static! {
    static ref ARGS: Args = Args {
        key1: value1,
        key2: value2,
    };
}
<Bar as Xylem<S>>::convert(derived.foo, context, &*ARGS)