Update macro to use relative working dirs, Spans

This commit is contained in:
2025-03-03 15:57:16 +00:00
parent 0a5269eeb0
commit 6e913b15d7
2 changed files with 22 additions and 7 deletions
+20 -5
View File
@@ -1,3 +1,4 @@
#![feature(proc_macro_span)]
#![warn(
clippy::correctness,
clippy::nursery,
@@ -12,18 +13,32 @@
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use proc_macro::TokenStream;
use proc_macro::{Span, TokenStream};
use quote::quote;
use std::path::PathBuf;
use syn::{LitStr, parse_macro_input};
extern crate proc_macro;
#[proc_macro]
/// Expects the file path to be relative to the current file so this works
/// similarly to the standard Rust include! macros.
pub fn include_font(item: TokenStream) -> TokenStream {
let filename = parse_macro_input!(item as LitStr);
let file_path = filename.value();
let span = Span::call_site();
let source_file = span.source_file();
println!("Loading font: [{}]", file_path);
if !source_file.is_real() {
panic!(
"We can't handle finding files if the source file does not exist. TODO: Can we?"
)
}
let filename = parse_macro_input!(item as LitStr);
let source_filepath: PathBuf = source_file.path();
let file_path = format!(
"{}/{}",
source_filepath.parent().unwrap_or_else(|| panic!("Expected to find the calling source file in a folder like src! Got: {}", source_filepath.display())).display(),
filename.value()
);
let font_bytes = match load_file(file_path) {
Ok(bytes) => bytes,