use super::*;
macro_rules! content_types {
($(
$(#[$attr:meta])*
type $name:ident($content_loader:ident -> $content_type:literal) {
$(#[$alloc_attr:meta])*
fn new_static($static:ty);
$(#[$static_fn_attr:meta])*
fn new_alloc($alloc:ty);
$(#[$from_file_attr:meta])*
fn from_file();
}
)+) => ($(
$(#[$attr])*
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name($content_loader);
impl $name {
$(#[$alloc_attr])*
#[inline]
pub const fn new_static(s: $static) -> Self {
Self($content_loader::new_static(s))
}
$(#[$static_fn_attr])*
#[inline]
pub fn new_alloc(s: $alloc) -> Self {
Self($content_loader::new_alloc(s))
}
$(#[$from_file_attr])*
#[inline]
pub async fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
$content_loader::from_file(path).await.map(Self)
}
}
impl From<$alloc> for $name {
#[inline]
fn from(this: $alloc) -> Self {
Self::new_alloc(this)
}
}
impl From<$static> for $name {
#[inline]
fn from(this: $static) -> Self {
Self::new_static(this)
}
}
impl fmt::Display for $name {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<$name> for Body {
#[inline]
fn from(this: $name) -> Self {
Body::from(this.0)
}
}
impl ::axum::response::IntoResponse for $name {
fn into_response(self) -> RawResponse {
RawResponse::builder()
.header("Content-Type", $content_type)
.body(self.into())
.unwrap_or_default()
}
}
)+);
}
content_types! {
type Html(StringContentLoader -> "text/html") {
fn new_static(&'static str);
fn new_alloc(String);
fn from_file();
}
type Css(StringContentLoader -> "text/css") {
fn new_static(&'static str);
fn new_alloc(String);
fn from_file();
}
type Js(StringContentLoader -> "application/javascript") {
fn new_static(&'static str);
fn new_alloc(String);
fn from_file();
}
type Json(StringContentLoader -> "application/json") {
fn new_static(&'static str);
fn new_alloc(String);
fn from_file();
}
type Xml(StringContentLoader -> "application/xml") {
fn new_static(&'static str);
fn new_alloc(String);
fn from_file();
}
type Text(StringContentLoader -> "text/plain") {
fn new_static(&'static str);
fn new_alloc(String);
fn from_file();
}
type Binary(BytesContentLoader -> "application/octet-stream") {
fn new_static(&'static [u8]);
fn new_alloc(Vec<u8>);
fn from_file();
}
}