-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support name
prefix
configs for setters
- Loading branch information
Showing
9 changed files
with
164 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 112 additions & 20 deletions
132
bon-macros/src/builder/builder_gen/member/config/setters.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,140 @@ | ||
use crate::parsing::{ItemSigConfig, ItemSigConfigParsing, SpannedKey}; | ||
use crate::parsing::SpannedKey; | ||
use crate::util::prelude::*; | ||
use darling::FromMeta; | ||
|
||
const DOCS_CONTEXT: &str = "builder struct's impl block"; | ||
|
||
fn parse_setter_fn(meta: &syn::Meta) -> Result<SpannedKey<ItemSigConfig>> { | ||
let params = ItemSigConfigParsing { | ||
meta, | ||
reject_self_mentions: Some(DOCS_CONTEXT), | ||
} | ||
.parse()?; | ||
fn parse_docs(meta: &syn::Meta) -> Result<SpannedKey<Vec<syn::Attribute>>> { | ||
crate::parsing::parse_docs_without_self_mentions(DOCS_CONTEXT, meta) | ||
} | ||
|
||
SpannedKey::new(meta.path(), params) | ||
#[derive(Debug)] | ||
pub(crate) enum SetterFnName { | ||
Name(syn::Ident), | ||
Prefix(syn::Ident), | ||
} | ||
|
||
fn parse_docs(meta: &syn::Meta) -> Result<SpannedKey<Vec<syn::Attribute>>> { | ||
crate::parsing::parse_docs_without_self_mentions(DOCS_CONTEXT, meta) | ||
impl SetterFnName { | ||
fn new( | ||
name: Option<SpannedKey<syn::Ident>>, | ||
prefix: Option<SpannedKey<syn::Ident>>, | ||
) -> Result<Option<SpannedKey<Self>>> { | ||
match (name, prefix) { | ||
(Some(name), None) => Ok(Some(name.map_value(SetterFnName::Name))), | ||
(None, Some(prefix)) => Ok(Some(prefix.map_value(SetterFnName::Prefix))), | ||
(None, None) => Ok(None), | ||
(Some(name), Some(prefix)) => { | ||
bail!( | ||
&name.key, | ||
"`{}` is mutually exclusive with `{}`", | ||
name.key, | ||
prefix.key, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, FromMeta)] | ||
#[derive(Debug)] | ||
pub(crate) struct SettersConfig { | ||
pub(crate) name: Option<SpannedKey<syn::Ident>>, | ||
pub(crate) name: Option<SpannedKey<SetterFnName>>, | ||
pub(crate) vis: Option<SpannedKey<syn::Visibility>>, | ||
|
||
#[darling(rename = "doc", default, with = parse_docs, map = Some)] | ||
pub(crate) docs: Option<SpannedKey<Vec<syn::Attribute>>>, | ||
|
||
#[darling(flatten)] | ||
pub(crate) fns: SettersFnsConfig, | ||
} | ||
|
||
impl FromMeta for SettersConfig { | ||
fn from_meta(meta: &syn::Meta) -> Result<Self> { | ||
#[derive(FromMeta)] | ||
struct Parsed { | ||
name: Option<SpannedKey<syn::Ident>>, | ||
prefix: Option<SpannedKey<syn::Ident>>, | ||
|
||
vis: Option<SpannedKey<syn::Visibility>>, | ||
|
||
#[darling(rename = "doc", default, map = Some, with = parse_docs)] | ||
docs: Option<SpannedKey<Vec<syn::Attribute>>>, | ||
|
||
#[darling(flatten)] | ||
fns: SettersFnsConfig, | ||
} | ||
|
||
let Parsed { | ||
name, | ||
prefix, | ||
vis, | ||
docs, | ||
fns, | ||
} = Parsed::from_meta(meta)?; | ||
|
||
Ok(SettersConfig { | ||
name: SetterFnName::new(name, prefix)?, | ||
vis, | ||
docs, | ||
fns, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, FromMeta)] | ||
pub(crate) struct SettersFnsConfig { | ||
/// Config for the setter that accepts the value of type T for a member of | ||
/// type `Option<T>` or with `#[builder(default)]`. | ||
/// | ||
/// By default, it's named `{member}` without any prefix or suffix. | ||
#[darling(default, with = parse_setter_fn, map = Some)] | ||
pub(crate) some_fn: Option<SpannedKey<ItemSigConfig>>, | ||
pub(crate) some_fn: Option<SpannedKey<SetterFnSigConfig>>, | ||
|
||
/// The setter that accepts the value of type `Option<T>` for a member of | ||
/// type `Option<T>` or with `#[builder(default)]`. | ||
/// | ||
/// By default, it's named `maybe_{member}`. | ||
#[darling(default, with = parse_setter_fn, map = Some)] | ||
pub(crate) option_fn: Option<SpannedKey<ItemSigConfig>>, | ||
pub(crate) option_fn: Option<SpannedKey<SetterFnSigConfig>>, | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct SetterFnSigConfig { | ||
pub(crate) name: Option<SpannedKey<SetterFnName>>, | ||
pub(crate) vis: Option<SpannedKey<syn::Visibility>>, | ||
pub(crate) docs: Option<SpannedKey<Vec<syn::Attribute>>>, | ||
} | ||
|
||
impl FromMeta for SetterFnSigConfig { | ||
fn from_meta(meta: &syn::Meta) -> Result<Self> { | ||
if let syn::Meta::NameValue(meta) = meta { | ||
let val = &meta.value; | ||
let name = syn::parse2(val.to_token_stream())?; | ||
|
||
return Ok(SetterFnSigConfig { | ||
name: Some(SpannedKey::new(&meta.path, SetterFnName::Name(name))?), | ||
vis: None, | ||
docs: None, | ||
}); | ||
} | ||
|
||
#[derive(Debug, FromMeta)] | ||
struct Full { | ||
name: Option<SpannedKey<syn::Ident>>, | ||
prefix: Option<SpannedKey<syn::Ident>>, | ||
|
||
vis: Option<SpannedKey<syn::Visibility>>, | ||
|
||
#[darling(rename = "doc", default, map = Some, with = parse_docs)] | ||
docs: Option<SpannedKey<Vec<syn::Attribute>>>, | ||
} | ||
|
||
let Full { | ||
name, | ||
prefix, | ||
vis, | ||
docs, | ||
} = crate::parsing::parse_non_empty_paren_meta_list_or_name_value(meta)?; | ||
|
||
let config = SetterFnSigConfig { | ||
name: SetterFnName::new(name, prefix)?, | ||
vis, | ||
docs, | ||
}; | ||
|
||
Ok(config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters