use proc_macro::TokenStream;
use quote::quote;
use std::path::Path;
use syn::{parse_macro_input, LitStr};
use crate::utils::ferro;
pub fn asset_impl(input: TokenStream) -> TokenStream {
let path_lit = parse_macro_input!(input as LitStr);
let path_str = path_lit.value();
let ferro = ferro();
let ext = Path::new(&path_str)
.extension()
.and_then(|e| e.to_str())
.map(|e| e.to_ascii_lowercase())
.unwrap_or_default();
let bundle_name: String = path_str
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' {
c.to_ascii_lowercase()
} else {
'_'
}
})
.collect();
let output = quote! {
{
static __FERRO_ASSET_URL: ::std::sync::OnceLock<::std::string::String>
= ::std::sync::OnceLock::new();
__FERRO_ASSET_URL
.get_or_init(|| {
static __FERRO_ASSET_BYTES: &[u8] = include_bytes!(#path_lit);
#ferro::bundle::Bundle::new(#bundle_name, __FERRO_ASSET_BYTES)
.content_type(#ferro::bundle::mime_from_ext(#ext))
.hashed_url()
})
.as_str()
}
};
output.into()
}