codama_attributes/
attribute.rs

1use crate::{AttributeContext, CodamaAttribute, DeriveAttribute, UnsupportedAttribute};
2use codama_syn_helpers::extensions::*;
3use derive_more::derive::From;
4
5#[derive(Debug, PartialEq, From)]
6pub enum Attribute<'a> {
7    // E.g. `#[derive(Debug, CodamaType)]`.
8    Derive(DeriveAttribute<'a>),
9    // E.g. `#[codama(type = number(u8))]` or `#[codama(fixed_size = 32)]`.
10    Codama(CodamaAttribute<'a>),
11    // E.g. `#[some_unsupported_attribute = 42]`.
12    Unsupported(UnsupportedAttribute<'a>),
13}
14
15impl<'a> Attribute<'a> {
16    pub fn parse(attr: &'a syn::Attribute, ctx: &AttributeContext) -> syn::Result<Self> {
17        let path = attr.path();
18        match (path.prefix().as_str(), path.last_str().as_str()) {
19            ("", "derive") => Ok(DeriveAttribute::parse(attr)?.into()),
20            ("" | "codama_macros" | "codama", "codama") => {
21                Ok(CodamaAttribute::parse(attr, ctx)?.into())
22            }
23            _ => Ok(UnsupportedAttribute::new(attr).into()),
24        }
25    }
26
27    pub fn codama(&self) -> Option<&CodamaAttribute<'a>> {
28        match self {
29            Attribute::Codama(a) => Some(a),
30            _ => None,
31        }
32    }
33}