abi_stable_derive/get_static_equivalent/
attribute_parsing.rs

1//! For parsing the helper attributess for `#[derive(GetStaticEquivalent)]`.
2
3use std::marker::PhantomData;
4
5use as_derive_utils::parse_utils::ParseBufferExt;
6
7use syn::{parse::ParseBuffer, Attribute};
8
9use crate::impl_interfacetype::{parse_impl_interfacetype, ImplInterfaceType};
10
11/// This is derived from the helper attributes of the `#[derive(GetStaticEquivalent)]` macrp.
12#[derive(Default)]
13pub(super) struct GetStaticEquivAttrs<'a> {
14    pub(super) impl_interfacetype: Option<ImplInterfaceType>,
15    pub(super) debug_print: bool,
16    _marker: PhantomData<&'a ()>,
17}
18
19mod kw {
20    syn::custom_keyword! {debug_print}
21    syn::custom_keyword! {sabi}
22    syn::custom_keyword! {impl_InterfaceType}
23}
24
25/// Parses the helper attributes of the `#[derive(GetStaticEquivalent)]` macrp.
26pub(super) fn parse_attrs_for_get_static_equiv<'a, I>(
27    attrs: I,
28) -> Result<GetStaticEquivAttrs<'a>, syn::Error>
29where
30    I: IntoIterator<Item = &'a Attribute>,
31{
32    let mut this = GetStaticEquivAttrs::default();
33
34    for attr in attrs {
35        if attr.path.is_ident("sabi") {
36            attr.parse_args_with(|input: &ParseBuffer<'_>| parse_gse_attr(&mut this, input))?;
37        }
38    }
39
40    Ok(this)
41}
42
43// Helper function of `parse_attrs_for_get_static_equiv`.
44fn parse_gse_attr(
45    this: &mut GetStaticEquivAttrs<'_>,
46    input: &ParseBuffer<'_>,
47) -> Result<(), syn::Error> {
48    if input.check_parse(kw::impl_InterfaceType)? {
49        let content = input.parse_paren_buffer()?;
50        this.impl_interfacetype = Some(parse_impl_interfacetype(&content)?);
51    } else if input.check_parse(kw::debug_print)? {
52        this.debug_print = true;
53    } else {
54        return Err(input.error("Unrecodnized #[sabi(..)] attribute."));
55    }
56
57    Ok(())
58}