use std::collections::BTreeMap;
use serde::Serialize;
use serde_json::Value;
use crate::runtime::RustStream;
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Spec {
pub asyncapi: String,
pub info: Info,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub servers: BTreeMap<String, Server>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub channels: BTreeMap<String, Channel>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub operations: BTreeMap<String, Operation>,
pub components: Components,
}
impl Spec {
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(self)
}
pub fn to_yaml(&self) -> Result<String, serde_norway::Error> {
serde_norway::to_string(self)
}
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Server {
pub host: String,
pub protocol: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Info {
pub title: String,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Channel {
pub address: String,
pub messages: BTreeMap<String, Reference>,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Operation {
pub action: String,
pub channel: Reference,
pub messages: Vec<Reference>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Components {
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub messages: BTreeMap<String, MessageObject>,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct MessageObject {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload: Option<Value>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Reference {
#[serde(rename = "$ref")]
pub reference: String,
}
impl Reference {
fn new(target: impl Into<String>) -> Self {
Self {
reference: target.into(),
}
}
}
#[must_use]
pub fn build_spec<L>(app: &RustStream<L>) -> Spec {
let info = Info {
title: app.info().title.clone(),
version: app.info().version.clone(),
description: app.info().description.clone(),
};
let servers = app
.servers()
.iter()
.map(|(name, spec)| {
(
name.clone(),
Server {
host: spec.host.clone(),
protocol: spec.protocol.clone(),
description: spec.description.clone(),
},
)
})
.collect();
let mut channels = BTreeMap::new();
let mut operations = BTreeMap::new();
let mut messages = BTreeMap::new();
for handler in app.handlers() {
let name = handler.name.as_ref();
let payload = handler
.payload_schema
.as_deref()
.and_then(|json| serde_json::from_str::<Value>(json).ok());
let schema_str = |key: &str| {
payload
.as_ref()
.and_then(|schema| schema.get(key))
.and_then(Value::as_str)
.map(str::to_owned)
};
let message_name = handler
.message_name
.as_ref()
.map(ToString::to_string)
.or_else(|| schema_str("title"))
.unwrap_or_else(|| message_name(handler.input_type));
channels.entry(name.to_owned()).or_insert_with(|| Channel {
address: name.to_owned(),
messages: BTreeMap::from([(
message_name.clone(),
Reference::new(format!("#/components/messages/{message_name}")),
)]),
});
operations.insert(
operation_id(name),
Operation {
action: "receive".to_owned(),
channel: Reference::new(format!("#/channels/{name}")),
messages: vec![Reference::new(format!(
"#/channels/{name}/messages/{message_name}"
))],
description: handler.description.as_ref().map(ToString::to_string),
},
);
let message_description = handler
.message_description
.as_ref()
.map(ToString::to_string)
.or_else(|| schema_str("description"))
.or_else(|| handler.description.as_ref().map(ToString::to_string));
messages
.entry(message_name.clone())
.or_insert_with(|| MessageObject {
name: message_name,
description: message_description,
payload,
});
}
Spec {
asyncapi: "3.0.0".to_owned(),
info,
servers,
channels,
operations,
components: Components { messages },
}
}
#[must_use]
pub fn render_viewer_html(spec_url: &str, opts: &ViewerOptions<'_>) -> String {
let title = opts.title;
let cdn = opts.cdn_base.trim_end_matches('/');
let spec = spec_url.replace('"', """);
format!(
"<!DOCTYPE html>\n\
<html lang=\"en\">\n\
<head>\n\
<meta charset=\"utf-8\" />\n\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n\
<title>{title}</title>\n\
<link rel=\"stylesheet\" href=\"{cdn}/styles/default.min.css\" />\n\
</head>\n\
<body>\n\
<div id=\"asyncapi\"></div>\n\
<script src=\"{cdn}/browser/standalone/index.js\"></script>\n\
<script>\n\
AsyncApiStandalone.render(\n\
{{ schema: {{ url: \"{spec}\" }}, config: {{ show: {{ sidebar: true }} }} }},\n\
document.getElementById(\"asyncapi\"),\n\
);\n\
</script>\n\
</body>\n\
</html>\n"
)
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ViewerOptions<'a> {
pub title: &'a str,
pub cdn_base: &'a str,
}
impl<'a> ViewerOptions<'a> {
#[must_use]
pub const fn with_title(mut self, title: &'a str) -> Self {
self.title = title;
self
}
#[must_use]
pub const fn with_cdn_base(mut self, cdn_base: &'a str) -> Self {
self.cdn_base = cdn_base;
self
}
}
impl Default for ViewerOptions<'_> {
fn default() -> Self {
Self {
title: "AsyncAPI",
cdn_base: "https://cdn.jsdelivr.net/npm/@asyncapi/react-component@2.6.4",
}
}
}
fn message_name(type_name: &str) -> String {
type_name
.rsplit("::")
.next()
.unwrap_or(type_name)
.to_owned()
}
fn operation_id(name: &str) -> String {
let sanitized: String = name
.chars()
.map(|c| if c.is_alphanumeric() { c } else { '_' })
.collect();
format!("receive_{sanitized}")
}