Derive Macro Component

Source
#[derive(Component)]
{
    // Attributes available to this derive:
    #[component]
}
Expand description

Derive macro for the Component trait.

#[derive(BorshSerialize, BorshDeserialize, HasBorshSchema, Component)]
#[component(
    specification = "examples/schemas/ExampleData",
    schema_id = "ehlbg4aesvav6x4wt4bcdocci323a5cb2jnhyhiizj3qmbaqkk4a"
)]
struct ExampleData {
    name: String,
    age: u8,
    tags: Vec<String>,
}

The attribute options for the #[component()] attribute are:

  • name = "ComponentName" - Allows you to set the component name in the schema.
  • schema_id = "ehlbg4aesvav6x4wt4bcdocci323a5cb2jnhyhiizj3qmbaqkk4a" - Lets you add an assertion that the resulting schema digest matches the expected value.
  • specification = "path/to/schema" - Lets you specify the path to a directory containing the specification components.

§Specification

The specification is made up of a list of components. The specification directory must contain 1 file for each component you want to add to the specification.

The name of each component must contain the base32 encoded schema ID of the component. It may optionally be prefixed with an identifier ending with an underscore ( _ ) before the schema ID. It may optionally be suffixed with a . and a file extension that will be ignored.

The macro will ignore any file in the directory starting with a . or with README.

The contents of each component file must be in the Borsh format associated to the component’s schema ID.