smithy_bindgen

Macro smithy_bindgen 

Source
smithy_bindgen!() { /* proc-macro */ }
Expand description

Generate code from a smithy IDL file.

§Syntax

The first parameter of the smithy_bindgen! macro can take one of three forms. The second parameter is the namespace used for code generation.

  • one wasmcloud first-party interface

    The single-file parameter is a path relative to the wasmcloud interfaces git repo wasmcloud/interfaces

    smithy_bindgen!("httpserver/httpserver.smithy", "org.wasmcloud.interfaces.httpserver");

    The above is shorthand for the following:

    smithy_bindgen!({
      url: "https://cdn.jsdelivr.net/gh/wasmcloud/interfaces",
      files: ["httpserver/httpserver.smithy"]
    }, "org.wasmcloud.interfaces.httpserver" );
  • one Model Source

    smithy_bindgen!({
      path: "./tests/test-bindgen.smithy",
    }, "org.example.interfaces.foo" );
  • array of Model Sources

    smithy_bindgen!([
      { path: "./tests/test-bindgen.smithy" },
      { url: "https://cdn.jsdelivr.net/gh/wasmcloud/interfaces/factorial/factorial.smithy" },
    ], "org.example.interfaces.foo" );

§Model Source Specification

A model source contains a url, for http(s) downloads, or a path, for local fs access, that serves as a base, plus files, an optional list of file paths that are appended to the base to build complete url download paths and local file paths. When joining the sub-paths from the files array, ‘/’ is inserted or removed as needed, so that there is exactly one between the base and the sub-path. url must begin with either ‘http://’ or ‘https://’. If path is a relative fs path, it is relative to the folder containing Cargo.toml. files may be omitted if the url or path contains the full path to the .smithy file.

All the following are (syntactically) valid model sources:

{ url: "https://example.com/interfaces/foo.smithy" }
{ url: "https://example.com/interfaces", files: [ "foo.smithy", "bar.smithy" ]}
{ path: "../interfaces/foo.smithy" }
{ path: "../interfaces", files: ["foo.smithy", "bar.smithy"]}

If a model source structure contains no url base and no path base, the url for the github wasmcloud interface repo is used:

url: "https://cdn.jsdelivr.net/gh/wasmcloud/interfaces"

Why would the code generator need to load more than one smithy file? So that interfaces can share common symbols for data structures. Most smithy interfaces already import symbols from the namespace org.wasmcloud.model, defined in wasmcloud-model.smithy. The bindgen tool resolves all symbols by assembling an in-memory schema model from all the smithy sources and namespaces, then traversing through the in-memory model, generating code only for the schema elements in the namespace declared in the second parameter of smithy_bindgen!.

§jsdelivr.net urls

cdn.jsdelivr.net mirrors open source github repositories. The url syntax can optionally include a github branch, tag, or commit sha.

§Common files

Wasmcloud common model files are always automatically included when compiling models (If you’ve used codegen.toml files, you may remember that they required all base models to be specified explicitly.)

§Namespace

Models may include symbols defined in other models via the use command. Only the symbols defined in the namespace (smithy_bindgen!’s second parameter) will be included in the generated code.