Is there any way to create a macro in rust that returns a new struct with the fields that I want. I have been working on this for weeks and cannot find a suitable solution. Seeing the problem below, is this even possible? And if so, how would I go about starting to implement this?
struct Person {
id: u32,
first_name: String,
middle_name: Option<String>,
last_name: String,
age: u8,
}
// I want to be able to have a function-like macro
// that takes the new name, old struct, and fields I want as input
// and returns a new struct based on that
sub_struct!("PersonNames", &Person, vec!["first_name", "middle_name", "last_name"])
// the above code yields the following
struct PersonNames {
first_name: String,
middle_name: Option<String>,
last_name: String,
}
another solution I can think of trying that I haven't yet is using derive-like macros.
#[sub_struct(&Person, vec!["first_name", "middle_name", "last_name"])]
struct PersonName {}