it: works
This commit is contained in:
72
src/lib.rs
72
src/lib.rs
@@ -1,15 +1,61 @@
|
||||
// #[must_use]
|
||||
// pub const fn add(left: u64, right: u64) -> u64 {
|
||||
// left + right
|
||||
// }
|
||||
// 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`]
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
pub mod file {
|
||||
pub type IncludeAssetMacroTy = (&'static [u8], &'static str, [u8; 32]);
|
||||
|
||||
// #[test]
|
||||
// fn it_works() {
|
||||
// let result = add(2, 2);
|
||||
// assert_eq!(result, 4);
|
||||
// }
|
||||
// }
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
#[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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user