Crate for providing metadata for Gear programs.
Metadata is used to describe the interface of a Gear program. For example, it can be used when uploading a program using https://idea.gear-tech.io. The metadata informs the user about the program's interface and allows them to interact with it using custom types on web applications UI.
Another use case is to parse metadata in JavaScript using the gear-js
library and get the metadata details for some custom UI.
Note that metadata is not required for a Gear program to work. It is only used to provide additional information about the program. Also, metadata can be used for various purposes but we will focus on the use cases related to the https://idea.gear-tech.io.
To generate a metadata output file for a program, you need:
- Add
gmeta
crate to yourCargo.toml
file. - Define an empty struct that will identify the program metadata.
- Implement the [
Metadata
] trait for this struct by defining the associated types of the trait. - Option 1: Call
gear_wasm_builder::build_with_metadata
function inbuild.rs
file. - Option 2: Convert metadata to hex string using [
MetadataRepr::hex
] function and write it to the text file.
Examples
In this example we will create a simple ping-pong program. Let's define
message types and metadata in a separate ping-io
crate to be able to use
it in both program and build.rs
files.
We will define message types for handle()
and state()
functions.
ping-io
crate:
use ;
use *;
// Message type for `handle()` function.
// Metadata struct.
;
ping
program crate:
use ;
use ;
# const IGNORE: &'static str = stringify! ;
// Counter that will be incremented on each `Ping` message.
static mut COUNTER: i32 = 0;
#
#
#
extern "C"
extern "C"
build.rs
file:
# const IGNORE: &'static str = stringify! {
use ping_io::ProgramMetadata;
# };
#
# pub struct ProgramMetadata;
# impl gmeta::Metadata for ProgramMetadata {
# type Init = ();
# type Handle = ();
# type Others = ();
# type Reply = ();
# type Signal = ();
# type State = ();
# }
fn main() {
gear_wasm_builder::build_with_metadata::<ProgramMetadata>();
}
You can also generate metadata manually and write it to the file without
using build.rs
:
use ;
# const IGNORE: &'static str = stringify! ;
use fs;
#
#
#
# ;
#
#
let metadata_hex = repr.hex;
assert_eq!;
write.expect;
You can parse generated metadata file using gear-js
API in JavaScript:
import from '@gear-js/api';
import from 'fs';
const metadataHex = ;
const metadata = ;
console.log;
console.log;
This will print the following:
Registry: Map(2) {
0 => { name: 'RustOutPingPong', def: '{"_enum":["Ping","Pong"]}' },
1 => { name: 'i32', def: null }
}
Types: {
init: { input: null, output: null },
handle: { input: 0, output: 0 },
reply: { input: null, output: null },
others: { input: null, output: null },
signal: null,
state: 1
}