2 releases

new 0.1.1 Feb 2, 2025
0.1.0 Feb 2, 2025
0.0.0 Feb 2, 2025

#422 in Procedural macros

Download history 561/week @ 2025-02-01

561 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

20KB
352 lines

macro-string

github crates.io docs.rs build status

This crate is a helper library for procedural macros to perform eager evaluation of standard library string macros like concat! and env! in macro input.

For example, to implement a macro such as the following:

// Parses JSON at compile time and expands to a serde_json::Value.
let j = include_json!(concat!(env!("CARGO_MANIFEST_DIR"), "/manifest.json"));

the implementation of include_json! will need to parse and eagerly evaluate the two macro calls within its input tokens.

use macro_string::MacroString;
use proc_macro::TokenStream;
use proc_macro2::Span;
use std::fs;
use syn::parse_macro_input;

#[proc_macro]
pub fn include_json(input: TokenStream) -> TokenStream {
    let MacroString(path) = parse_macro_input!(input);

    let content = match fs::read(&path) {
        Ok(content) => content,
        Err(err) => {
            return TokenStream::from(syn::Error::new(Span::call_site(), err).to_compile_error());
        }
    };

    let json: serde_json::Value = match serde_json::from_slice(&content) {
        Ok(json) => json,
        Err(err) => {
            return TokenStream::from(syn::Error::new(Span::call_site(), err).to_compile_error());
        }
    };

    /*TODO: print serde_json::Value to TokenStream*/
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~230–670KB
~16K SLoC