codama_attributes/
repr_attribute.rs1use crate::Attribute;
2use codama_errors::CodamaError;
3use codama_nodes::{NumberFormat, NumberTypeNode};
4use codama_syn_helpers::extensions::*;
5
6#[derive(Debug, PartialEq)]
7pub struct ReprAttribute<'a> {
8 pub ast: &'a syn::Attribute,
9 pub metas: Vec<syn::Meta>,
10}
11
12impl<'a> ReprAttribute<'a> {
13 pub fn parse(ast: &'a syn::Attribute) -> syn::Result<Self> {
14 let unfeatured = ast.unfeatured();
15 let effective = unfeatured.as_ref().unwrap_or(ast);
16 Self::parse_from(ast, effective)
17 }
18
19 pub fn parse_from(ast: &'a syn::Attribute, effective: &syn::Attribute) -> syn::Result<Self> {
23 let list = effective.meta.require_list()?;
24 if !list.path.is_strict("repr") {
25 return Err(list.path.error("expected #[repr(...)]"));
26 };
27
28 let metas = list.parse_comma_args::<syn::Meta>()?;
29 Ok(Self { ast, metas })
30 }
31
32 pub fn get_number_type_node(&self) -> Option<NumberTypeNode> {
33 self.metas.iter().find_map(|meta| match meta {
34 syn::Meta::Path(p) => match NumberFormat::try_from(p.to_string()) {
35 Ok(n) => Some(NumberTypeNode::le(n)),
36 Err(_) => None,
37 },
38 _ => None,
39 })
40 }
41}
42
43impl<'a> TryFrom<&'a Attribute<'a>> for &'a ReprAttribute<'a> {
44 type Error = CodamaError;
45
46 fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
47 match attribute {
48 Attribute::Repr(a) => Ok(a),
49 _ => Err(CodamaError::InvalidAttribute {
50 expected: "repr".to_string(),
51 actual: attribute.name(),
52 }),
53 }
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use codama_nodes::NumberFormat::U32;
61 use syn::parse_quote;
62
63 #[test]
64 fn test_repr_attribute() {
65 let ast = parse_quote! { #[repr(u32, align(4))] };
66 let attribute = ReprAttribute::parse(&ast).unwrap();
67
68 assert_eq!(attribute.ast, &ast);
69 assert_eq!(
70 attribute.metas,
71 [(parse_quote! { u32 }), (parse_quote! { align(4) })]
72 );
73 }
74
75 #[test]
76 fn test_feature_gated_repr_attribute() {
77 let ast = parse_quote! { #[cfg_attr(feature = "some_feature", repr(u32, align(4)))] };
78 let attribute = ReprAttribute::parse(&ast).unwrap();
79
80 assert_eq!(attribute.ast, &ast);
81 assert_eq!(
82 attribute.metas,
83 [(parse_quote! { u32 }), (parse_quote! { align(4) })]
84 );
85 }
86
87 #[test]
88 fn test_get_number_type_node() {
89 let ast = parse_quote! { #[repr(u32, align(4), u64)] };
90 let attribute = ReprAttribute::parse(&ast).unwrap();
91
92 assert_eq!(
93 attribute.get_number_type_node(),
94 Some(NumberTypeNode::le(U32))
95 );
96 }
97}