bon_macros/
bon.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::builder;
use crate::normalization::{ExpandCfg, Expansion};
use crate::util::prelude::*;
use darling::ast::NestedMeta;
use darling::FromMeta;

pub(crate) fn generate(params: TokenStream, item: TokenStream) -> TokenStream {
    crate::error::handle_errors(item.clone(), || try_generate(params, item))
        .unwrap_or_else(std::convert::identity)
}

pub(crate) fn try_generate(params: TokenStream, item: TokenStream) -> Result<TokenStream> {
    let item: syn::Item = syn::parse2(item)?;

    let ctx = ExpandCfg {
        current_macro: format_ident!("bon"),
        config: params,
        item,
    };

    let input = match ctx.expand_cfg()? {
        Expansion::Expanded(input) => input,
        Expansion::Recurse(output) => return Ok(output),
    };

    let params = NestedMeta::parse_meta_list(input.config)?;
    let params = FromMeta::from_list(&params)?;

    match input.item {
        syn::Item::Impl(item_impl) => builder::item_impl::generate(params, item_impl),
        _ => bail!(
            &input.item,
            "`#[bon]` attribute is expected to be placed on an `impl` block \
             but it was placed on other syntax instead"
        ),
    }
}