1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Handler metadata collected by the router and consumed by `ruststream-asyncapi`.
use std::{any::type_name, borrow::Cow, marker::PhantomData};
/// Descriptive metadata for a registered subscriber handler.
///
/// Collected by the router so downstream tools (`AsyncAPI` generator, dashboards, CLI) can
/// describe the service without re-parsing source code.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct HandlerMetadata {
/// Broker name / subject the handler is bound to.
pub name: Cow<'static, str>,
/// Optional broker-provided routing key, when the broker distinguishes from `name`.
pub routing_key: Option<Cow<'static, str>>,
/// Type name of the decoded input value, as captured at registration time.
pub input_type: &'static str,
/// Type name of the response value, when the handler produces one (e.g. request / reply).
pub output_type: Option<&'static str>,
/// Free-form human description, typically pulled from a doc comment on the handler.
pub description: Option<Cow<'static, str>>,
/// The input type's JSON Schema, serialized, when the type implements
/// [`schemars::JsonSchema`] (captured under the `asyncapi` feature). Feeds the `AsyncAPI`
/// message payload schema.
pub payload_schema: Option<String>,
}
impl HandlerMetadata {
/// Constructs metadata for a raw-bytes handler bound to a name.
#[must_use]
pub fn raw(name: impl Into<Cow<'static, str>>) -> Self {
Self {
name: name.into(),
routing_key: None,
input_type: "bytes",
output_type: None,
description: None,
payload_schema: None,
}
}
/// Constructs metadata for a typed handler. The input type name is captured via
/// [`std::any::type_name`].
#[must_use]
pub fn typed<T>(name: impl Into<Cow<'static, str>>) -> Self {
let _ = PhantomData::<T>;
Self {
name: name.into(),
routing_key: None,
input_type: type_name::<T>(),
output_type: None,
description: None,
payload_schema: None,
}
}
/// Builder-style setter for the handler description.
#[must_use]
pub fn with_description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
self.description = Some(description.into());
self
}
/// Builder-style setter for the broker routing key.
#[must_use]
pub fn with_routing_key(mut self, key: impl Into<Cow<'static, str>>) -> Self {
self.routing_key = Some(key.into());
self
}
/// Builder-style setter for the response type name.
#[must_use]
pub fn with_output_type(mut self, name: &'static str) -> Self {
self.output_type = Some(name);
self
}
/// Builder-style setter for the serialized input payload schema.
#[must_use]
pub fn with_payload_schema(mut self, schema: impl Into<String>) -> Self {
self.payload_schema = Some(schema.into());
self
}
}