stak_macro_util/
lib.rs

1//! Macro utilities.
2
3use core::error::Error;
4use proc_macro2::TokenStream;
5use quote::quote;
6use std::{env, fs::read_to_string, path::Path};
7use syn::LitStr;
8
9/// Reads a source file.
10pub fn read_source_file(path: LitStr) -> Result<String, Box<dyn Error>> {
11    Ok(read_to_string(
12        Path::new(&env::var("CARGO_MANIFEST_DIR")?)
13            .join("src")
14            .join(path.value()),
15    )?)
16}
17
18/// Converts a macro result into a token stream.
19pub fn convert_result(result: Result<TokenStream, Box<dyn Error>>) -> TokenStream {
20    result.unwrap_or_else(|error| {
21        let message = error.to_string();
22
23        quote! { compile_error!(#message) }
24    })
25}