1
Fork 0
mirror of https://github.com/RGBCube/embd-rs synced 2025-07-28 05:57:44 +00:00

Better error messages when procmacro2_semver_exempt is not set

This commit is contained in:
RGBCube 2024-05-22 10:57:48 +03:00
parent 661d2d54c0
commit 0c9527c653
No known key found for this signature in database
2 changed files with 30 additions and 2 deletions

View file

@ -1,16 +1,28 @@
#![allow(unexpected_cfgs)] #![allow(unexpected_cfgs)]
#![cfg(procmacro2_semver_exempt)] #[cfg(procmacro2_semver_exempt)]
use std::{ use std::{
fs, fs,
path::Path, path::Path,
}; };
#[cfg(not(procmacro2_semver_exempt))]
compile_error!(
r#"pass `--cfg procmacro2_semver_exempt` to rustc to compile embd-macros or add this to your `.cargo/config.toml`:
[build]
rustflags = [ "--cfg", "procmacro2_semver_exempt" ]
"#
);
#[cfg(procmacro2_semver_exempt)]
use proc_macro as pm1; use proc_macro as pm1;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{ use quote::{
quote, quote,
ToTokens, ToTokens,
}; };
#[cfg(procmacro2_semver_exempt)]
use syn::{ use syn::{
parse_macro_input, parse_macro_input,
spanned::Spanned, spanned::Spanned,
@ -30,6 +42,7 @@ impl ToTokens for TokenVec {
} }
#[proc_macro] #[proc_macro]
#[cfg(procmacro2_semver_exempt)]
pub fn __dir(input: pm1::TokenStream) -> pm1::TokenStream { pub fn __dir(input: pm1::TokenStream) -> pm1::TokenStream {
let input2 = input.clone(); let input2 = input.clone();
let path = parse_macro_input!(input2 as LitStr).value(); let path = parse_macro_input!(input2 as LitStr).value();
@ -52,12 +65,14 @@ pub fn __dir(input: pm1::TokenStream) -> pm1::TokenStream {
.into() .into()
} }
#[cfg(procmacro2_semver_exempt)]
fn dir_debug(path: &str) -> TokenStream { fn dir_debug(path: &str) -> TokenStream {
quote! { quote! {
::embd::__dir_runtime(file!(), #path) ::embd::__dir_runtime(file!(), #path)
} }
} }
#[cfg(procmacro2_semver_exempt)]
fn dir_release(input: TokenStream, path: &str) -> TokenStream { fn dir_release(input: TokenStream, path: &str) -> TokenStream {
let neighbor = TokenStream::from(input).span().source_file().path(); let neighbor = TokenStream::from(input).span().source_file().path();
@ -80,6 +95,7 @@ fn dir_release(input: TokenStream, path: &str) -> TokenStream {
} }
} }
#[cfg(procmacro2_semver_exempt)]
fn read_dir(directory: &Path) -> TokenVec { fn read_dir(directory: &Path) -> TokenVec {
let mut entries = Vec::new(); let mut entries = Vec::new();

View file

@ -1,11 +1,20 @@
#![allow(unexpected_cfgs)] #![allow(unexpected_cfgs)]
#![cfg(procmacro2_semver_exempt)]
use std::{ use std::{
borrow::Cow, borrow::Cow,
fs, fs,
path::Path, path::Path,
}; };
#[cfg(not(procmacro2_semver_exempt))]
compile_error!(
r#"pass `--cfg procmacro2_semver_exempt` to rustc to compile embd or add this to your `.cargo/config.toml`:
[build]
rustflags = [ "--cfg", "procmacro2_semver_exempt" ]
"#
);
#[doc(hidden)] #[doc(hidden)]
pub fn __string_runtime(neighbor: &str, path: &str) -> String { pub fn __string_runtime(neighbor: &str, path: &str) -> String {
let file = Path::new(neighbor) let file = Path::new(neighbor)
@ -27,6 +36,7 @@ pub fn __string_runtime(neighbor: &str, path: &str) -> String {
/// } /// }
/// ``` /// ```
#[macro_export] #[macro_export]
#[cfg(procmacro2_semver_exempt)]
macro_rules! string { macro_rules! string {
($path:literal) => {{ ($path:literal) => {{
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -62,6 +72,7 @@ pub fn __bytes_runtime(neighbor: &str, path: &str) -> Vec<u8> {
/// } /// }
/// ``` /// ```
#[macro_export] #[macro_export]
#[cfg(procmacro2_semver_exempt)]
macro_rules! bytes { macro_rules! bytes {
($path:literal) => {{ ($path:literal) => {{
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -223,4 +234,5 @@ pub fn __dir_runtime(neighbor: &str, path: &str) -> Dir {
/// let content: embd::Dir = embd::dir!("../assets"); /// let content: embd::Dir = embd::dir!("../assets");
/// } /// }
/// ``` /// ```
#[cfg(procmacro2_semver_exempt)]
pub use embd_macros::__dir as dir; pub use embd_macros::__dir as dir;