use std::path::{Path, PathBuf};
use litrs::Literal;
use proc_macro::TokenStream;
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 '{path_str}' could not be canonicalized: {err}",),
};
Ok(Self(canonicalized))
}
#[allow(unused_mut)]
pub fn expand_dir(&self) -> TokenStream {
let asset_path = self.0.to_string_lossy();
let mut source: TokenStream = quote::quote! {
chuot::AssetSource::new().with_runtime_dir(#asset_path)
}
.into();
#[cfg(feature = "embed-assets")]
source.extend(crate::embedded::asset_source::parse_dir(&self.0));
source
}
}