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: 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: 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!(attribute.directive, CodamaDirective::Type(_)));
60 }
61
62 #[test]
63 fn test_feature_gated_codama_attribute() {
64 let ast = parse_quote! { #[cfg_attr(feature = "some_feature", codama(type = boolean))] };
65 let file = syn::File::empty();
66 let ctx = AttributeContext::File(&file);
67 let attribute = CodamaAttribute::parse(&ast, &ctx).unwrap();
68
69 assert_eq!(attribute.ast, &ast);
70 assert!(matches!(attribute.directive, CodamaDirective::Type(_)));
71 }
72}