MCPcopy Index your code
hub / github.com/dtolnay/macro-string

github.com/dtolnay/macro-string @0.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.2.0 ↗ · + Follow
29 symbols 34 edges 8 files 2 documented · 7%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.

Supported macros: concat!, env!, include!, include_str!, stringify!

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 std::fs;
use syn::parse_macro_input;

#[proc_macro]
pub fn include_json(input: TokenStream) -> TokenStream {
    let macro_string = parse_macro_input!(input as MacroString);
    let path = match macro_string.eval() {
        Ok(path) => path,
        Err(err) => return TokenStream::from(err.to_compile_error()),
    };

    let content = match fs::read(&path) {
        Ok(content) => content,
        Err(err) => return TokenStream::from(macro_string.error(err).to_compile_error()),
    };

    let json: serde_json::Value = match serde_json::from_slice(&content) {
        Ok(json) => json,
        Err(err) => return TokenStream::from(macro_string.error(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.

Core symbols most depended-on inside this repo

surround
called by 5
src/lib.rs
error
called by 2
src/lib.rs
fs_read
called by 2
src/lib.rs
eval
called by 1
src/lib.rs
parse
called by 0
src/lib.rs
parse_strict
called by 0
src/lib.rs
parse_any
called by 0
src/lib.rs
to_tokens
called by 0
src/lib.rs

Shape

Function 14
Method 7
Class 6
Enum 2

Languages

Rust100%

Modules by API surface

src/lib.rs17 symbols
tests/test.rs6 symbols
tests/ui/unsupported_macro.rs1 symbols
tests/ui/suffixed_literal.rs1 symbols
tests/ui/relative_path.rs1 symbols
tests/ui/no_such_file.rs1 symbols
tests/eval/eval.rs1 symbols
tests/compiletest.rs1 symbols

For agents

$ claude mcp add macro-string \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact