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
版权声明:本文标题:rust - Pass a string concatenation to a macro - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629453a2160070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论