codama_attributes/
derive_attribute.rsuse codama_syn_helpers::extensions::*;
#[derive(Debug, PartialEq)]
pub struct DeriveAttribute<'a> {
pub ast: &'a syn::Attribute,
pub derives: Vec<syn::Path>,
}
impl<'a> DeriveAttribute<'a> {
pub fn parse(ast: &'a syn::Attribute) -> syn::Result<Self> {
let unfeatured = ast.unfeatured();
let attr = unfeatured.as_ref().unwrap_or(ast);
let list = attr.meta.require_list()?;
if !list.path.is_strict("derive") {
return Err(list.path.error("expected #[derive(...)]"));
};
let derives = list.parse_comma_args::<syn::Path>()?;
Ok(Self { ast, derives })
}
}
#[cfg(test)]
mod tests {
use super::*;
use syn::parse_quote;
#[test]
fn test_derive_attribute() {
let ast = parse_quote! { #[derive(Debug, PartialEq)] };
let attribute = DeriveAttribute::parse(&ast).unwrap();
assert_eq!(attribute.ast, &ast);
assert_eq!(
attribute.derives,
[(parse_quote! { Debug }), (parse_quote! { PartialEq }),]
);
}
#[test]
fn test_feature_gated_derive_attribute() {
let ast = parse_quote! { #[cfg_attr(feature = "some_feature", derive(Debug, PartialEq))] };
let attribute = DeriveAttribute::parse(&ast).unwrap();
assert_eq!(attribute.ast, &ast);
assert_eq!(
attribute.derives,
[(parse_quote! { Debug }), (parse_quote! { PartialEq })]
);
}
}