bon_macros/builder/builder_gen/member/config/with/
mod.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
mod closure;

pub(crate) use closure::*;

use crate::util::prelude::*;
use darling::FromMeta;

#[derive(Debug)]
pub(crate) enum WithConfig {
    /// Closure syntax e.g. `#[builder(with = |param: Type| body)]`
    Closure(SetterClosure),

    /// Well-known path [`Option::Some`]
    Some(syn::Path),

    /// Well-known path [`FromIterator::from_iter`] or `<_>::from_iter`
    FromIter(syn::ExprPath),
}

impl WithConfig {
    pub(crate) fn as_closure(&self) -> Option<&SetterClosure> {
        match self {
            Self::Closure(closure) => Some(closure),
            _ => None,
        }
    }
}

impl FromMeta for WithConfig {
    fn from_meta(meta: &syn::Meta) -> darling::Result<Self> {
        let err = || {
            err!(
                meta,
                "expected a closure e.g. `#[builder(with = |param: T| expression)]` or \
                a well-known function path which could be one of:\n\
                - #[builder(with = Some)]\n\
                - #[builder(with = FromIterator::from_iter)]\n\
                - #[builder(with = <_>::from_iter)] (same as above, but shorter)",
            )
        };

        let name_val = match meta {
            syn::Meta::NameValue(meta) => meta,
            _ => return Err(err()),
        };

        if let syn::Expr::Closure(_) = name_val.value {
            return SetterClosure::from_meta(meta).map(Self::Closure);
        }

        let path = match &name_val.value {
            syn::Expr::Path(path) => path,
            _ => return Err(err()),
        };

        crate::parsing::reject_syntax("attribute", &path.attrs.first())?;

        if *path == syn::parse_quote!(Some) {
            return Ok(Self::Some(path.path.clone()));
        }

        if *path == syn::parse_quote!(FromIterator::from_iter)
            || *path == syn::parse_quote!(<_>::from_iter)
        {
            return Ok(Self::FromIter(path.clone()));
        }

        Err(err())
    }
}