admin管理员组

文章数量:1025498

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!

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!

本文标签: rustPass a string concatenation to a macroStack Overflow