Compare commits

...

5 Commits

Author SHA1 Message Date
6f91e1e1a6 ver: bump patch (0.1.2) 2026-03-24 22:54:30 +01:00
4add7f22f9 feat: add str shasum prop 2026-03-24 22:53:53 +01:00
6a37f26eaa ver: bump path (0.1.1) 2026-03-24 22:24:34 +01:00
2167ee28fd fix: make path resolution relative to project root
this was already how it mostly behaved but there seemed to be exceptions
like LSP warnings (in these tests themselves)
2026-03-24 22:24:14 +01:00
34b1140573 fix: oops print 2026-03-24 22:17:15 +01:00
3 changed files with 26 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "const-macros" name = "const-macros"
description = "Crate with some useful macros for const-time stuff" description = "Crate with some useful macros for const-time stuff"
version = "0.1.0" version = "0.1.2"
edition = "2024" edition = "2024"
license = "MIT" license = "MIT"
repository = "https://git.javalsai.tuxcord.net/rust/const-macros" repository = "https://git.javalsai.tuxcord.net/rust/const-macros"

View File

@@ -1,11 +1,10 @@
#![feature(proc_macro_value)] #![feature(proc_macro_value)]
use std::{fs::File, io::Read}; use std::{fs::File, io::Read, path::Path};
use proc_macro::{Delimiter, Group, Literal, Punct, TokenStream, TokenTree}; use proc_macro::{Delimiter, Group, Literal, Punct, TokenStream, TokenTree};
use sha2::Digest; use sha2::Digest;
fn read_path_literal(input: TokenStream) -> String { fn read_path_literal(input: TokenStream) -> String {
println!("{input:?}");
let mut input_iter = input.into_iter(); let mut input_iter = input.into_iter();
let path_token = input_iter.next().expect("expected a path"); let path_token = input_iter.next().expect("expected a path");
assert!(input_iter.next().is_none(), "only expected one argument"); assert!(input_iter.next().is_none(), "only expected one argument");
@@ -88,6 +87,14 @@ fn extend_asset_tuple(ts: &mut TokenStream, buf: &[u8], mime: &str, sha256sum: [
extend_array(&mut ts, &sha256sum); extend_array(&mut ts, &sha256sum);
ts ts
}, },
{
let mut ts = TokenStream::new();
let sha_str = sha256sum
.iter()
.fold(String::new(), |acc, b| acc + &format!("{b:02x}"));
ts.extend(std::iter::once(Literal::string(&sha_str)));
ts
},
] ]
.into_iter(), .into_iter(),
); );
@@ -115,7 +122,10 @@ fn get_sha256sum(buf: &[u8]) -> [u8; 32] {
pub fn include_asset(input: TokenStream) -> TokenStream { pub fn include_asset(input: TokenStream) -> TokenStream {
let path = read_path_literal(input); let path = read_path_literal(input);
let mut buf = Vec::new(); let mut buf = Vec::new();
File::open(path)
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR to be set");
File::open(Path::new(&manifest_dir).join(&path))
.expect("couldn't open file") .expect("couldn't open file")
.read_to_end(&mut buf) .read_to_end(&mut buf)
.expect("error reading file"); .expect("error reading file");

View File

@@ -2,12 +2,13 @@
// learn about [`proc_macro`] so I'm not gonna use [`quote`] or [`syn`] // learn about [`proc_macro`] so I'm not gonna use [`quote`] or [`syn`]
pub mod file { pub mod file {
pub type IncludeAssetMacroTy = (&'static [u8], &'static str, [u8; 32]); pub type IncludeAssetMacroTy = (&'static [u8], &'static str, [u8; 32], &'static str);
pub struct FileAsset { pub struct FileAsset {
pub bytes: &'static [u8], pub bytes: &'static [u8],
pub mime: &'static str, pub mime: &'static str,
pub shasum: [u8; 32], pub shasum: [u8; 32],
pub shasum_str: &'static str,
} }
impl FileAsset { impl FileAsset {
@@ -17,16 +18,21 @@ pub mod file {
bytes: tuple.0, bytes: tuple.0,
mime: tuple.1, mime: tuple.1,
shasum: tuple.2, shasum: tuple.2,
shasum_str: tuple.3,
} }
} }
} }
impl From<IncludeAssetMacroTy> for FileAsset { impl From<IncludeAssetMacroTy> for FileAsset {
fn from(value: (&'static [u8], &'static str, [u8; 32])) -> Self { fn from(value: IncludeAssetMacroTy) -> Self {
Self::from_tuple(value) 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_export]
macro_rules! include_asset { macro_rules! include_asset {
($path:literal) => { ($path:literal) => {
@@ -46,6 +52,10 @@ pub mod file {
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.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.mime, "text/plain; charset=us-ascii");
assert_eq!(
ASSET1.shasum_str,
"4f72ae5a7e2d70fec413af0e298f1b7a7c1d8d9caf4fd624460d2fe2d81b8328"
);
assert_eq!( assert_eq!(
ASSET1.shasum, ASSET1.shasum,
[ [