use litrs::Literal;
use proc_macro::TokenStream;
use std::path::{Path, PathBuf};
pub struct Input(pub PathBuf);
impl Input {
pub fn parse(input: TokenStream) -> Result<Self, TokenStream> {
let path_str = match input.into_iter().next() {
Some(tt) => {
let lit = Literal::try_from(tt).map_err(|err| err.to_compile_error())?;
match lit {
Literal::String(asset_path_str) => asset_path_str,
_ => panic!("input has to be a string literal, but this is not: {lit}"),
}
.value()
.to_owned()
}
None => "assets/".to_owned(),
};
let canonicalized = match Path::new(&path_str).canonicalize() {
Ok(path) => path,
Err(err) => panic!(
"Asset path '{}' could not be canonicalized: {err}",
path_str
),
};
Ok(Self(canonicalized))
}
#[cfg(feature = "embed-assets")]
pub fn expand_dir(&self) -> TokenStream {
crate::embedded::asset_source::parse_dir(&self.0)
}
#[cfg(not(feature = "embed-assets"))]
pub fn expand_dir(&self) -> TokenStream {
let asset_path = self.0.to_string_lossy();
quote::quote! {
chuot::assets::runtime::EmbeddedAssets(#asset_path)
}
.into()
}
}