跳到主要内容
本页尚未翻译

下面显示的是英文原文。

Bindings

Bindings carry what one protocol says about a server, a channel, an operation or a message, in that protocol's AsyncAPI vocabulary.

A Binding is one protocol's object. Binding::new takes the protocol key, the binding's version and a serde body, and writes bindingVersion into the body itself. A key outside the specification's list fails construction with a BindingError, at startup rather than in a published file. Binding::extension carries an x- object for a transport the specification has no binding for. Bindings holds one body per protocol, and an empty set leaves the object out of the document. Broker crates fill most bindings: a server through DescribeServer, a subscription through its descriptor's channel_bindings and its siblings, a publish position through its PublishPolicy. A service attaches a server binding itself with ServerSpec::bindings. The example runs with cargo test --example asyncapi_servers --features macros,memory,asyncapi.

Example​

/// The Kafka server binding: where the cluster's schema registry is.
#[derive(Serialize)]
struct KafkaServer {
#[serde(rename = "schemaRegistryUrl")]
schema_registry_url: &'static str,
}

/// Adds the binding; a protocol the specification does not list fails here, at startup.
fn with_registry(server: ServerSpec) -> Result<ServerSpec, BindingError> {
let registry = KafkaServer {
schema_registry_url: "https://registry.example.com",
};
let binding = Binding::new("kafka", "0.5.0", &registry)?;
Ok(server.bindings(Bindings::new().with(binding)))
}