bon_macros/builder/builder_gen/member/config/
setters.rs

1use crate::parsing::{ItemSigConfig, ItemSigConfigParsing, SpannedKey};
2use crate::util::prelude::*;
3use darling::FromMeta;
4
5const DOCS_CONTEXT: &str = "builder struct's impl block";
6
7fn parse_setter_fn(meta: &syn::Meta) -> Result<SpannedKey<ItemSigConfig>> {
8    let params = ItemSigConfigParsing {
9        meta,
10        reject_self_mentions: Some(DOCS_CONTEXT),
11    }
12    .parse()?;
13
14    SpannedKey::new(meta.path(), params)
15}
16
17fn parse_docs(meta: &syn::Meta) -> Result<SpannedKey<Vec<syn::Attribute>>> {
18    crate::parsing::parse_docs_without_self_mentions(DOCS_CONTEXT, meta)
19}
20
21#[derive(Debug, FromMeta)]
22pub(crate) struct SettersConfig {
23    pub(crate) name: Option<SpannedKey<syn::Ident>>,
24    pub(crate) vis: Option<SpannedKey<syn::Visibility>>,
25
26    #[darling(rename = "doc", default, with = parse_docs, map = Some)]
27    pub(crate) docs: Option<SpannedKey<Vec<syn::Attribute>>>,
28
29    #[darling(flatten)]
30    pub(crate) fns: SettersFnsConfig,
31}
32
33#[derive(Debug, FromMeta)]
34pub(crate) struct SettersFnsConfig {
35    /// Config for the setter that accepts the value of type T for a member of
36    /// type `Option<T>` or with `#[builder(default)]`.
37    ///
38    /// By default, it's named `{member}` without any prefix or suffix.
39    #[darling(default, with = parse_setter_fn, map = Some)]
40    pub(crate) some_fn: Option<SpannedKey<ItemSigConfig>>,
41
42    /// The setter that accepts the value of type `Option<T>` for a member of
43    /// type `Option<T>` or with `#[builder(default)]`.
44    ///
45    /// By default, it's named `maybe_{member}`.
46    #[darling(default, with = parse_setter_fn, map = Some)]
47    pub(crate) option_fn: Option<SpannedKey<ItemSigConfig>>,
48}