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

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629454a4637015.html

相关推荐

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

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

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信