use std::borrow::Cow;
use crate::runtime::metadata::OutgoingMessageMetadata;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Documented;
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Probed;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Undocumented;
#[cfg(feature = "asyncapi")]
#[diagnostic::on_unimplemented(
message = "`{Self}` has no JSON Schema for the generated document",
note = "registrations are documented by default: derive `schemars::JsonSchema` for `{Self}`, \
or opt this registration out with `.undocumented()`"
)]
pub trait Documentable {
fn schema_json() -> Option<String>;
}
#[cfg(feature = "asyncapi")]
impl<T: schemars::JsonSchema + ?Sized> Documentable for T {
fn schema_json() -> Option<String> {
crate::asyncapi::schema_json::<T>()
}
}
#[cfg(not(feature = "asyncapi"))]
pub trait Documentable {
fn schema_json() -> Option<String> {
None
}
}
#[cfg(not(feature = "asyncapi"))]
impl<T: ?Sized> Documentable for T {}
pub trait DocState<T: ?Sized> {
fn schema() -> Option<String>;
}
impl<T: ?Sized + Documentable> DocState<T> for Documented {
fn schema() -> Option<String> {
T::schema_json()
}
}
impl<T: ?Sized> DocState<T> for Undocumented {
fn schema() -> Option<String> {
None
}
}
impl<T: ?Sized> DocState<T> for Probed {
fn schema() -> Option<String> {
None
}
}
#[derive(Debug, Clone, Default)]
pub struct Docs {
pub(crate) description: Option<Cow<'static, str>>,
pub(crate) input_schema: Option<String>,
pub(crate) headers_schema: Option<String>,
pub(crate) message_name: Option<&'static str>,
pub(crate) message_description: Option<&'static str>,
pub(crate) outgoing: Option<Vec<OutgoingMessageMetadata>>,
}
impl Docs {
pub(crate) fn description(&self) -> Option<&str> {
self.description.as_deref()
}
}
#[doc(hidden)]
#[derive(Debug, Default)]
pub struct ProbedDocs {
pub description: Option<&'static str>,
pub input_schema: Option<String>,
pub headers_schema: Option<String>,
pub message_name: Option<&'static str>,
pub message_description: Option<&'static str>,
pub outgoing: Option<Vec<OutgoingMessageMetadata>>,
}
impl ProbedDocs {
pub(super) fn into_docs(self) -> Docs {
Docs {
description: self.description.map(Cow::Borrowed),
input_schema: self.input_schema,
headers_schema: self.headers_schema,
message_name: self.message_name,
message_description: self.message_description,
outgoing: self.outgoing,
}
}
}