最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

rust - Pass a string concatenation to a macro - Stack Overflow

matteradmin6PV0评论

I'm trying to generate an enum type at compile-time, based on a json file.

Following guides like / I made this :

  • An external crate my_macros since proc_macros should apparently be in an external crate, in which I have :
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};
use serde_json;

#[proc_macro]
pub fn generate_myenum(input: TokenStream) -> TokenStream {
  // Parse the JSON file path from the input
  let input = parse_macro_input!(input as LitStr);
  let json_path = input.value();

  // Read and parse the JSON file
  let json_data = std::fs::read_to_string(json_path)
    .expect("Failed to read JSON file");
  let deserialized_data: Vec<serde_json::Value> = serde_json::from_str(&json_data)
    .expect("Invalid JSON format");

  // Extract the names I want, which will be the generated enum fields :
  let mut my_names = Vec::new();
  for some_elt in deserialized_data {
    if let Some(arr) = some_elt.get("mylist").and_then(|s| s.as_array()) {
      for e in arr {
        if let Some(name) = e.get("name").and_then(|n| n.as_str()) {
          my_names.push(name.to_string());
        }
      }
    }
  }

  // Generate the enum
  let enum_variants = my_names
    .iter()
    .map(|name| syn::Ident::new(name, proc_macro2::Span::call_site()));

  let output = quote! {
    pub enum UserEnum {
      #(#enum_variants),*
      }
  };

  TokenStream::from(output)
}

It works well if the user gives a pure literal string to the macro like :

generate_myenum!("/home/foo/myconfigfile.json"); 

But it would very practical if the user could point to its own development folder (for example) like :

generate_myenum!(concat!(env!("CARGO_MANIFEST_DIR"), "/userdata/myconfigfile.json"));

However, due to the evaluation order of macros I have the error "expected a string literal" if I try this.

It's frustrating because I've done most of the work (and I wouldn't have thought that something as powerful as generating type could work so easily!) ... and I'm stuck on this simple problem. Even more frustrating, the env! macro works on buildtime, so a priori all the elements were there for it to work!

Post a comment

comment list (0)

  1. No comments so far