#![cfg_attr(not(test), allow(dead_code, unused_imports))]
use ruststream::runtime::{App, AppInfo, RustStream};
use ruststream::{AppId, AppIdError, Contact, ExternalDocs, License, Tag};
fn info() -> AppInfo {
AppInfo::new("payments", "1.4.0")
.description("Everything the payment domain publishes.")
.terms_of_service("https://example.com/terms")
.contact(contact())
.license(license())
.tag(tag())
.external_docs(external_docs())
}
fn contact() -> Contact {
Contact::new()
.name("Payments team")
.url("https://example.com/teams/payments")
.email("payments@example.com")
}
fn license() -> License {
License::new("Apache-2.0").url("https://spdx.org/licenses/Apache-2.0.html")
}
fn tag() -> Tag {
Tag::new("payments")
.description("Everything that moves money")
.external_docs(ExternalDocs::new("https://example.com/domains/payments"))
}
fn external_docs() -> ExternalDocs {
ExternalDocs::new("https://example.com/runbooks/payments").description("The runbook")
}
fn app(id: &str) -> Result<impl App, AppIdError> {
let id: AppId = id.parse()?;
Ok(RustStream::new(info().id(id)))
}
fn main() {}
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::AppIdError;
use ruststream::asyncapi::build_spec;
use serde_json::{Value, json};
use super::app;
fn document() -> Result<Value, Box<dyn Error>> {
Ok(serde_json::from_str(
&build_spec(&app("urn:example:payments")?).to_json()?,
)?)
}
#[test]
fn the_info_section_carries_what_app_info_holds() -> Result<(), Box<dyn Error>> {
let spec = document()?;
assert_eq!(
spec["info"],
json!({
"title": "payments",
"version": "1.4.0",
"description": "Everything the payment domain publishes.",
"termsOfService": "https://example.com/terms",
"contact": {
"name": "Payments team",
"url": "https://example.com/teams/payments",
"email": "payments@example.com",
},
"license": {
"name": "Apache-2.0",
"url": "https://spdx.org/licenses/Apache-2.0.html",
},
"tags": [{
"name": "payments",
"description": "Everything that moves money",
"externalDocs": { "url": "https://example.com/domains/payments" },
}],
"externalDocs": {
"url": "https://example.com/runbooks/payments",
"description": "The runbook",
},
})
);
Ok(())
}
#[test]
fn the_id_is_the_documents_root_id() -> Result<(), Box<dyn Error>> {
let spec = document()?;
assert_eq!(spec["id"], "urn:example:payments");
Ok(())
}
#[test]
fn an_id_that_is_not_a_uri_stops_the_app() {
assert!(matches!(app("payments"), Err(AppIdError::NoScheme)));
assert!(matches!(
app("1st:payments"),
Err(AppIdError::MalformedScheme)
));
}
}