use axum::{Router, body::Bytes, http::Uri};
use super::{DocsError, resource};
#[cfg(feature = "redoc-embedded")]
pub const EMBEDDED_REDOC_VERSION: &str = "2.5.4";
#[cfg(feature = "rapidoc-embedded")]
pub const EMBEDDED_RAPIDOC_VERSION: &str = "14.0.0";
#[derive(Debug)]
#[non_exhaustive]
pub enum DocsUi {
Redoc(DocsAssets),
RapiDoc(DocsAssets),
}
impl DocsUi {
#[cfg(feature = "redoc-embedded")]
#[cfg_attr(docsrs, doc(cfg(feature = "redoc-embedded")))]
pub fn embedded_redoc() -> Self {
Self::Redoc(DocsAssets::bundled(
include_bytes!("embedded/redoc/redoc.standalone.js"),
include_bytes!("embedded/redoc/LICENSES.txt"),
))
}
#[cfg(feature = "rapidoc-embedded")]
#[cfg_attr(docsrs, doc(cfg(feature = "rapidoc-embedded")))]
pub fn embedded_rapidoc() -> Self {
Self::RapiDoc(DocsAssets::bundled(
include_bytes!("embedded/rapidoc/rapidoc-min.js"),
include_bytes!("embedded/rapidoc/LICENSES.txt"),
))
}
pub(super) fn name(&self) -> &'static str {
match self {
Self::Redoc(_) => "redoc",
Self::RapiDoc(_) => "rapidoc",
}
}
pub(super) fn assets(&self) -> &DocsAssets {
match self {
Self::Redoc(assets) | Self::RapiDoc(assets) => assets,
}
}
pub(super) fn into_assets(self) -> DocsAssets {
match self {
Self::Redoc(assets) | Self::RapiDoc(assets) => assets,
}
}
}
enum Source {
Local {
bundle: Bytes,
notices: Option<Bytes>,
},
Url(String),
}
#[must_use]
pub struct DocsAssets(Source);
impl std::fmt::Debug for DocsAssets {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
Source::Local { bundle, .. } => f
.debug_struct("DocsAssets::Local")
.field("bytes", &bundle.len())
.finish_non_exhaustive(),
Source::Url(url) => f.debug_tuple("DocsAssets::Url").field(url).finish(),
}
}
}
impl DocsAssets {
pub fn local(bundle: impl Into<Bytes>) -> Self {
Self(Source::Local {
bundle: bundle.into(),
notices: None,
})
}
pub fn url(url: impl Into<String>) -> Self {
Self(Source::Url(url.into()))
}
#[cfg(any(feature = "redoc-embedded", feature = "rapidoc-embedded"))]
fn bundled(bundle: &'static [u8], notices: &'static [u8]) -> Self {
Self(Source::Local {
bundle: Bytes::from_static(bundle),
notices: Some(Bytes::from_static(notices)),
})
}
pub(super) fn script_url(&self) -> Result<&str, DocsError> {
match &self.0 {
Source::Local { .. } => Ok("./ui.js"),
Source::Url(url) => {
let valid = !url.is_empty()
&& !url.starts_with("//")
&& !url
.chars()
.any(|c| c.is_whitespace() || c.is_control() || c == '\\')
&& url.parse::<Uri>().is_ok_and(|uri| match uri.scheme_str() {
Some("http" | "https") => uri.host().is_some(),
Some(_) => false,
None => !url.split('/').next().unwrap_or_default().contains(':'),
});
if valid {
Ok(url)
} else {
Err(DocsError::InvalidAssetUrl(url.clone()))
}
}
}
}
pub(super) fn mount<S: Clone + Send + Sync + 'static>(
self,
mut router: Router<S>,
mount: &str,
) -> Router<S> {
if let Source::Local { bundle, notices } = self.0 {
router = router.route(
&format!("{mount}/ui.js"),
resource("text/javascript; charset=utf-8", bundle),
);
if let Some(notices) = notices {
router = router.route(
&format!("{mount}/ui.LICENSE.txt"),
resource("text/plain; charset=utf-8", notices),
);
}
}
router
}
}