Trait agsol_borsh_schema::BorshSchema[][src]

pub trait BorshSchema { }
Expand description

An empty trait that serves as a flag for the schema parser.

It has an alias attribute that can be used to annotate struct and enum fields to explicitly indicate the type of that field. This is needed because the parser reads the file as a raw string, therefore it has no way of knowing the underlying type of a type alias.

Example

use agsol_borsh_schema::BorshSchema;
use std::collections::BTreeMap;

type SomeAlias = [u8; 32];

#[derive(BorshSchema)]
struct Foo {
    foo: Option<u64>,
    bar: BTreeMap<u8, Bar>,
    #[alias([u8; 32])]
    baz: SomeAlias,
}

#[derive(BorshSchema)]
enum Bar {
    A,
    B,
    C(u64),
    D {
        foo: i32,
        bar: String,
    },
}

In the above example you may notice that Foo’s bar field doesn’t need an alias because Bar implements BorshSchema itself, however, the parser doesn’t know that SomeAlias is actually a byte array without the alias attribute. If the alias attribute is omitted, the generated TypeScript code will contain SomeAlias instead of Uint8Array.

Implementors