this was already how it mostly behaved but there seemed to be exceptions like LSP warnings (in these tests themselves)
66 lines
2.2 KiB
Rust
66 lines
2.2 KiB
Rust
// So hand working with [`TokenStream`] might be a bad idea BUT I partially made this crate to
|
|
// learn about [`proc_macro`] so I'm not gonna use [`quote`] or [`syn`]
|
|
|
|
pub mod file {
|
|
pub type IncludeAssetMacroTy = (&'static [u8], &'static str, [u8; 32]);
|
|
|
|
pub struct FileAsset {
|
|
pub bytes: &'static [u8],
|
|
pub mime: &'static str,
|
|
pub shasum: [u8; 32],
|
|
}
|
|
|
|
impl FileAsset {
|
|
#[must_use]
|
|
pub const fn from_tuple(tuple: IncludeAssetMacroTy) -> Self {
|
|
Self {
|
|
bytes: tuple.0,
|
|
mime: tuple.1,
|
|
shasum: tuple.2,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<IncludeAssetMacroTy> for FileAsset {
|
|
fn from(value: (&'static [u8], &'static str, [u8; 32])) -> Self {
|
|
Self::from_tuple(value)
|
|
}
|
|
}
|
|
|
|
/// Similar to [`include_bytes!`], but it returns a lot more information about the file, like
|
|
/// its mime type, or its sha256sum (could be changed), all at compile time.
|
|
///
|
|
/// The path is relative to the package's root.
|
|
#[macro_export]
|
|
macro_rules! include_asset {
|
|
($path:literal) => {
|
|
$crate::file::FileAsset::from_tuple($crate::file::_include_asset!($path))
|
|
};
|
|
}
|
|
pub use include_asset;
|
|
|
|
pub use const_macros_proc::include_asset as _include_asset;
|
|
|
|
#[cfg(test)]
|
|
pub mod test {
|
|
#[test]
|
|
fn works() {
|
|
const ASSET1: super::FileAsset = super::include_asset!("assets/DATA1");
|
|
const ASSET2: super::FileAsset = super::include_asset!("assets/DATA2");
|
|
|
|
assert_eq!(ASSET1.bytes, b"This is just a bunch of bytes that will hash to a known sequence and I can test for validity\n");
|
|
assert_eq!(ASSET1.mime, "text/plain; charset=us-ascii");
|
|
assert_eq!(
|
|
ASSET1.shasum,
|
|
[
|
|
0x4f, 0x72, 0xae, 0x5a, 0x7e, 0x2d, 0x70, 0xfe, 0xc4, 0x13, 0xaf, 0x0e, 0x29,
|
|
0x8f, 0x1b, 0x7a, 0x7c, 0x1d, 0x8d, 0x9c, 0xaf, 0x4f, 0xd6, 0x24, 0x46, 0x0d,
|
|
0x2f, 0xe2, 0xd8, 0x1b, 0x83, 0x28,
|
|
]
|
|
);
|
|
|
|
assert_eq!(ASSET2.mime, "image/png; charset=binary");
|
|
}
|
|
}
|
|
}
|