codama_attributes/
codama_attribute.rs1use crate::{utils::SetOnce, Attribute, AttributeContext, CodamaDirective};
2use codama_errors::CodamaError;
3use codama_syn_helpers::extensions::*;
4
5#[derive(Debug, PartialEq)]
6pub struct CodamaAttribute<'a> {
7 pub ast: &'a syn::Attribute,
8 pub directive: Box<CodamaDirective>,
9}
10
11impl<'a> CodamaAttribute<'a> {
12 pub fn parse(ast: &'a syn::Attribute, ctx: &AttributeContext) -> syn::Result<Self> {
13 let unfeatured = ast.unfeatured();
15 let attr = unfeatured.as_ref().unwrap_or(ast);
16
17 let list = attr.meta.require_list()?;
19 if !list.path.is_strict("codama") {
20 return Err(list.path.error("expected #[codama(...)]"));
21 };
22
23 let mut directive = SetOnce::<CodamaDirective>::new("codama");
24 list.each(|ref meta| directive.set(CodamaDirective::parse(meta, ctx)?, meta))?;
25 Ok(Self {
26 ast,
27 directive: Box::new(directive.take(attr)?),
28 })
29 }
30}
31
32impl<'a> TryFrom<&'a Attribute<'a>> for &'a CodamaAttribute<'a> {
33 type Error = CodamaError;
34
35 fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
36 match attribute {
37 Attribute::Codama(a) => Ok(a),
38 _ => Err(CodamaError::InvalidAttribute {
39 expected: "codama".to_string(),
40 actual: attribute.name(),
41 }),
42 }
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use syn::parse_quote;
50
51 #[test]
52 fn test_codama_attribute() {
53 let ast = parse_quote! { #[codama(type = boolean)] };
54 let file = syn::File::empty();
55 let ctx = AttributeContext::File(&file);
56 let attribute = CodamaAttribute::parse(&ast, &ctx).unwrap();
57
58 assert_eq!(attribute.ast, &ast);
59 assert!(matches!(
60 attribute.directive.as_ref(),
61 CodamaDirective::Type(_)
62 ));
63 }
64
65 #[test]
66 fn test_feature_gated_codama_attribute() {
67 let ast = parse_quote! { #[cfg_attr(feature = "some_feature", codama(type = boolean))] };
68 let file = syn::File::empty();
69 let ctx = AttributeContext::File(&file);
70 let attribute = CodamaAttribute::parse(&ast, &ctx).unwrap();
71
72 assert_eq!(attribute.ast, &ast);
73 assert!(matches!(
74 attribute.directive.as_ref(),
75 CodamaDirective::Type(_)
76 ));
77 }
78}