2019-09-30 12:10:35 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
use syn::{parse_macro_input, DeriveInput};
|
|
|
|
|
|
|
|
#[proc_macro_derive(ModuleConfig)]
|
|
|
|
pub fn derive_module_config(input: TokenStream) -> TokenStream {
|
|
|
|
let dinput = parse_macro_input!(input as DeriveInput);
|
|
|
|
impl_module_config(dinput)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream {
|
|
|
|
let struct_ident = &dinput.ident;
|
|
|
|
let (_impl_generics, ty_generics, where_clause) = dinput.generics.split_for_impl();
|
|
|
|
|
|
|
|
let mut from_config = quote! {};
|
|
|
|
let mut load_config = quote! {};
|
|
|
|
|
|
|
|
if let syn::Data::Struct(data) = dinput.data {
|
|
|
|
if let syn::Fields::Named(fields_named) = data.fields {
|
|
|
|
let mut load_tokens = quote! {};
|
|
|
|
|
|
|
|
for field in fields_named.named.iter() {
|
|
|
|
let ident = field.ident.as_ref().unwrap();
|
|
|
|
|
|
|
|
let new_load_tokens = quote! {
|
|
|
|
if let Some(config_str) = config.get(stringify!(#ident)) {
|
|
|
|
new_module_config.#ident = new_module_config.#ident.load_config(config_str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
load_tokens = quote! {
|
|
|
|
#load_tokens
|
|
|
|
#new_load_tokens
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
load_config = quote! {
|
|
|
|
fn load_config(&self, config: &'a toml::Value) -> Self {
|
|
|
|
let mut new_module_config = self.clone();
|
|
|
|
if let toml::Value::Table(config) = config {
|
2020-09-29 16:35:11 +00:00
|
|
|
if config.get("prefix").is_some() {
|
|
|
|
log::warn!("\"prefix\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/")
|
|
|
|
}
|
|
|
|
if config.get("suffix").is_some() {
|
|
|
|
log::warn!("\"suffix\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/")
|
2020-09-28 20:38:50 +00:00
|
|
|
}
|
2019-09-30 12:10:35 +00:00
|
|
|
#load_tokens
|
|
|
|
}
|
|
|
|
new_module_config
|
|
|
|
}
|
|
|
|
};
|
|
|
|
from_config = quote! {
|
|
|
|
fn from_config(config: &'a toml::Value) -> Option<Self> {
|
2021-03-31 15:31:55 +00:00
|
|
|
Some(Self::default().load_config(config))
|
2019-09-30 12:10:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
impl<'a> ModuleConfig<'a> for #struct_ident #ty_generics #where_clause {
|
|
|
|
#from_config
|
|
|
|
#load_config
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|