#![forbid(unsafe_code)]
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{Attribute, Data, DeriveInput, Field, Meta, Token, parse_macro_input};
const ATTR: &str = "shep";
const SECRET: &str = "secret";
#[proc_macro_derive(DogConfig, attributes(shep))]
pub fn derive_dog_config(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand(&input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
fn expand(input: &DeriveInput) -> syn::Result<TokenStream2> {
if let Some(attr) = find_shep_attribute(&input.attrs) {
return Err(syn::Error::new_spanned(
attr,
"`#[shep(secret)]` marks a field, not the type: move it onto the \
field that holds the credential",
));
}
let mut secrets: Vec<String> = Vec::new();
for field in fields_of(input)? {
if !is_secret(field)? {
continue;
}
let Some(ident) = &field.ident else {
return Err(syn::Error::new_spanned(
field,
"`#[shep(secret)]` cannot mark an unnamed field: a JSON Schema \
property has a name and this field has none, so the mark would \
have nothing to land on. Name the field.",
));
};
let name = ident.to_string();
if !secrets.contains(&name) {
secrets.push(name);
}
}
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
Ok(quote! {
impl #impl_generics ::shep_client::dogs::DogConfig for #name #ty_generics #where_clause {
const SECRET_KEY: &'static str = ::shep_client::dogs::SECRET_KEY;
const SECRET_FIELDS: &'static [&'static str] = &[#(#secrets),*];
}
})
}
fn fields_of(input: &DeriveInput) -> syn::Result<Vec<&Field>> {
match &input.data {
Data::Struct(data) => Ok(data.fields.iter().collect()),
Data::Enum(data) => {
let mut fields = Vec::new();
for variant in &data.variants {
if let Some(attr) = find_shep_attribute(&variant.attrs) {
return Err(syn::Error::new_spanned(
attr,
"`#[shep(secret)]` marks a field, not a variant: move \
it onto the field inside that holds the credential",
));
}
fields.extend(variant.fields.iter());
}
Ok(fields)
}
Data::Union(_) => Err(syn::Error::new_spanned(
&input.ident,
"`DogConfig` cannot be derived for a union: a union has no serde \
representation, so there is no schema for a mark to go into",
)),
}
}
fn is_secret(field: &Field) -> syn::Result<bool> {
let mut secret = false;
for attr in &field.attrs {
if !attr.path().is_ident(ATTR) {
continue;
}
if !matches!(attr.meta, Meta::List(_)) {
return Err(syn::Error::new_spanned(
attr,
"`#[shep]` needs an option in parentheses: the only one is \
`secret`, as in `#[shep(secret)]`",
));
}
attr.parse_nested_meta(|meta| {
if !meta.path.is_ident(SECRET) {
return Err(meta.error(
"unknown `shep` option: the only one is `secret`, as in \
`#[shep(secret)]`",
));
}
if meta.input.peek(Token![=]) {
return Err(meta.error(
"`secret` is a flag and takes no value: write \
`#[shep(secret)]`",
));
}
secret = true;
Ok(())
})?;
}
Ok(secret)
}
fn find_shep_attribute(attrs: &[Attribute]) -> Option<&Attribute> {
attrs.iter().find(|attr| attr.path().is_ident(ATTR))
}