aranya_capi_codegen/syntax/
builds.rs

1use proc_macro2::TokenStream;
2use quote::{quote, ToTokens};
3use syn::{
4    parse::{Parse, ParseStream, Result},
5    Error, Ident,
6};
7
8/// The `#[capi::builds(TYPE)]` attribute.
9///
10/// It can only be applied to structs or aliases.
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct Builds {
13    /// The name of the type being built.
14    pub ty: Ident,
15}
16
17impl Builds {
18    pub(super) fn parse(input: ParseStream<'_>) -> Result<Self> {
19        let ty = input.parse()?;
20        if !input.is_empty() {
21            return Err(Error::new(input.span(), "unexpected data"));
22        }
23        Ok(Self { ty })
24    }
25}
26
27impl Parse for Builds {
28    fn parse(input: ParseStream<'_>) -> Result<Self> {
29        Self::parse(input)
30    }
31}
32
33impl ToTokens for Builds {
34    fn to_tokens(&self, tokens: &mut TokenStream) {
35        let ty = &self.ty;
36        tokens.extend(quote! {
37            #[capi::builds(#ty)]
38        })
39    }
40}