2 releases

0.0.1 Feb 2, 2025
0.0.0 Feb 2, 2025

#42 in #concat

Download history 109/week @ 2025-01-27 126/week @ 2025-02-03

235 downloads per month
Used in macro-string

MIT/Apache

22KB
360 lines

macro-string

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

~195–630KB
~15K SLoC