Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bon-macros/src/builder/builder_gen/member/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) struct MemberConfig {
/// member no longer has the default of `None`. It also becomes a required
/// member unless a separate `#[builder(default = ...)]` attribute is
/// also specified.
pub(crate) transparent: darling::util::Flag,
pub(crate) required: darling::util::Flag,
}

#[derive(PartialEq, Eq, Clone, Copy)]
Expand All @@ -76,7 +76,7 @@ enum ParamName {
Setters,
Skip,
StartFn,
Transparent,
Required,
With,
}

Expand All @@ -91,7 +91,7 @@ impl fmt::Display for ParamName {
Self::Setters => "setters",
Self::Skip => "skip",
Self::StartFn => "start_fn",
Self::Transparent => "transparent",
Self::Required => "required",
Self::With => "with",
};
f.write_str(str)
Expand Down Expand Up @@ -154,7 +154,7 @@ impl MemberConfig {
setters,
skip,
start_fn,
transparent,
required,
with,
} = self;

Expand All @@ -167,7 +167,7 @@ impl MemberConfig {
(setters.is_some(), ParamName::Setters),
(skip.is_some(), ParamName::Skip),
(start_fn.is_present(), ParamName::StartFn),
(transparent.is_present(), ParamName::Transparent),
(required.is_present(), ParamName::Required),
(with.is_some(), ParamName::With),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expected one of the following:
`ApiResult<_>`, then it'll work fine;

(*) underlying type is the type of the member stripped from the `Option<T>` wrapper
if this member is of `Option<T>` type and no `#[builder(transparent)]` annotation
if this member is of `Option<T>` type and no `#[builder(required)]` annotation
is present";

#[derive(Debug)]
Expand Down
24 changes: 12 additions & 12 deletions bon-macros/src/builder/builder_gen/member/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ impl NamedMember {

self.validate_setters_config()?;

if self.config.transparent.is_present() && !self.ty.norm.is_option() {
if self.config.required.is_present() && !self.ty.norm.is_option() {
bail!(
&self.config.transparent.span(),
"`#[builder(transparent)]` can only be applied to members of \
&self.config.required.span(),
"`#[builder(required)]` can only be applied to members of \
type `Option<T>` to disable their special handling",
);
}
Expand All @@ -159,7 +159,7 @@ impl NamedMember {
bail!(
&setter.key,
"`{}` setter function applies only to members with `#[builder(default)]` \
or members of `Option<T>` type (if #[builder(transparent)] is not set)",
or members of `Option<T>` type (if #[builder(required)] is not set)",
setter.key
);
}
Expand Down Expand Up @@ -215,9 +215,9 @@ impl NamedMember {
}

/// Returns `true` if this member is of `Option<_>` type, but returns `false`
/// if `#[builder(transparent)]` is set.
/// if `#[builder(required)]` is set.
pub(crate) fn is_special_option_ty(&self) -> bool {
!self.config.transparent.is_present() && self.ty.norm.is_option()
!self.config.required.is_present() && self.ty.norm.is_option()
}

/// Returns `false` if the member has a default value. It means this member
Expand All @@ -235,19 +235,19 @@ impl NamedMember {
}

/// Returns the normalized type of the member stripping the `Option<_>`
/// wrapper if it's present unless `#[builder(transparent)]` is set.
/// wrapper if it's present unless `#[builder(required)]` is set.
pub(crate) fn underlying_norm_ty(&self) -> &syn::Type {
self.underlying_ty(&self.ty.norm)
}

/// Returns the original type of the member stripping the `Option<_>`
/// wrapper if it's present unless `#[builder(transparent)]` is set.
/// wrapper if it's present unless `#[builder(required)]` is set.
pub(crate) fn underlying_orig_ty(&self) -> &syn::Type {
self.underlying_ty(&self.ty.orig)
}

fn underlying_ty<'m>(&'m self, ty: &'m syn::Type) -> &'m syn::Type {
if self.config.transparent.is_present() || self.config.default.is_some() {
if self.config.required.is_present() || self.config.default.is_some() {
ty
} else {
ty.option_type_param().unwrap_or(ty)
Expand All @@ -259,12 +259,12 @@ impl NamedMember {
}

pub(crate) fn merge_on_config(&mut self, on: &[OnConfig]) -> Result {
// This is a temporary hack. We only allow `on(_, transparent)` as the
// This is a temporary hack. We only allow `on(_, required)` as the
// first `on(...)` clause. Instead we should implement the extended design:
// https://github.com/elastio/bon/issues/152
if let Some(on) = on.first().filter(|on| on.transparent.is_present()) {
if let Some(on) = on.first().filter(|on| on.required.is_present()) {
if self.is_special_option_ty() {
self.config.transparent = on.transparent;
self.config.required = on.required;
}
}

Expand Down
2 changes: 1 addition & 1 deletion bon-macros/src/builder/builder_gen/setters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'a> SettersCtx<'a> {
"the underlying type of this member is not `Option`; \
by default, members of type `Option` are optional and their \
'underlying type' is the type under the `Option`; \
you might be missing #[builder(transparent)]` annotation \
you might be missing #[builder(required)]` annotation \
for this member"
)
} else {
Expand Down
14 changes: 7 additions & 7 deletions bon-macros/src/builder/builder_gen/top_level_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl TopLevelConfig {
}

fn parse_for_any(meta_list: &[darling::ast::NestedMeta]) -> Result<Self> {
// This is a temporary hack. We only allow `on(_, transparent)` as the
// This is a temporary hack. We only allow `on(_, required)` as the
// first `on(...)` clause. Instead we should implement an extended design:
// https://github.com/elastio/bon/issues/152
let mut on_configs = meta_list
Expand Down Expand Up @@ -132,20 +132,20 @@ impl TopLevelConfig {

let me = Self::from_list(meta_list)?;

if let Some(on) = me.on.iter().skip(1).find(|on| on.transparent.is_present()) {
if let Some(on) = me.on.iter().skip(1).find(|on| on.required.is_present()) {
bail!(
&on.transparent.span(),
"`transparent` can only be specified in the first `on(...)` clause; \
&on.required.span(),
"`required` can only be specified in the first `on(...)` clause; \
this restriction may be lifted in the future",
);
}

if let Some(first_on) = me.on.first().filter(|on| on.transparent.is_present()) {
if let Some(first_on) = me.on.first().filter(|on| on.required.is_present()) {
if !matches!(first_on.type_pattern, syn::Type::Infer(_)) {
bail!(
&first_on.type_pattern,
"`transparent` can only be used with the wildcard type pattern \
i.e. `on(_, transparent)`; this restriction may be lifted in the future",
"`required` can only be used with the wildcard type pattern \
i.e. `on(_, required)`; this restriction may be lifted in the future",
);
}
}
Expand Down
12 changes: 6 additions & 6 deletions bon-macros/src/builder/builder_gen/top_level_config/on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) struct OnConfig {
pub(crate) type_pattern: syn::Type,
pub(crate) into: darling::util::Flag,
pub(crate) overwritable: darling::util::Flag,
pub(crate) transparent: darling::util::Flag,
pub(crate) required: darling::util::Flag,
}

impl Parse for OnConfig {
Expand All @@ -23,7 +23,7 @@ impl Parse for OnConfig {
struct Parsed {
into: darling::util::Flag,
overwritable: darling::util::Flag,
transparent: darling::util::Flag,
required: darling::util::Flag,
}

let parsed = Parsed::from_meta(&syn::parse_quote!(on(#rest)))?;
Expand All @@ -50,12 +50,12 @@ impl Parse for OnConfig {
let Parsed {
into,
overwritable,
transparent,
required,
} = &parsed;
let flags = [
("into", into),
("overwritable", overwritable),
("transparent", transparent),
("required", required),
];

if flags.iter().all(|(_, flag)| !flag.is_present()) {
Expand Down Expand Up @@ -103,14 +103,14 @@ impl Parse for OnConfig {
let Parsed {
into,
overwritable,
transparent,
required,
} = parsed;

Ok(Self {
type_pattern,
into,
overwritable,
transparent,
required,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion bon-macros/src/util/ide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub(crate) fn generate_completion_triggers(meta: Vec<Meta>) -> TokenStream {
if let Some(first) = meta.first() {
if let Meta::Path(path) = first {
if path.is_ident("into")
|| path.is_ident("transparent")
|| path.is_ident("required")
|| path.is_ident("overwritable")
{
return;
Expand Down
2 changes: 1 addition & 1 deletion bon/src/__/ide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub mod builder_top_level {
pub const into: Flag = Flag;

/// See the docs at <https://bon-rs.com/reference/builder/top-level/on>
pub const transparent: Flag = Flag;
pub const required: Flag = Flag;

/// See the docs at <https://bon-rs.com/reference/builder/top-level/on>
pub const overwritable: Flag = Flag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ mod member_level {
#[derive(Debug, Builder)]
#[allow(dead_code)]
struct Sut<T> {
#[builder(transparent)]
#[builder(required)]
regular: Option<u32>,

#[builder(transparent)]
#[builder(required)]
generic: Option<T>,

#[builder(transparent, into)]
#[builder(required, into)]
with_into: Option<u32>,

#[builder(transparent, default = Some(99))]
#[builder(required, default = Some(99))]
with_default: Option<u32>,

#[builder(transparent, default = Some(10))]
#[builder(required, default = Some(10))]
with_default_2: Option<u32>,
}

Expand Down Expand Up @@ -55,11 +55,11 @@ mod member_level {
fn test_free_fn() {
#[builder]
fn sut<T: fmt::Debug>(
#[builder(transparent)] regular: Option<u32>,
#[builder(transparent)] generic: Option<T>,
#[builder(transparent, into)] with_into: Option<u32>,
#[builder(transparent, default = Some(99))] with_default: Option<u32>,
#[builder(transparent, default = Some(10))] with_default_2: Option<u32>,
#[builder(required)] regular: Option<u32>,
#[builder(required)] generic: Option<T>,
#[builder(required, into)] with_into: Option<u32>,
#[builder(required, default = Some(99))] with_default: Option<u32>,
#[builder(required, default = Some(10))] with_default_2: Option<u32>,
) -> impl fmt::Debug {
(regular, generic, with_into, with_default, with_default_2)
}
Expand All @@ -83,23 +83,23 @@ mod member_level {
impl Sut {
#[builder]
fn sut<T: fmt::Debug>(
#[builder(transparent)] regular: Option<u32>,
#[builder(transparent)] generic: Option<T>,
#[builder(transparent, into)] with_into: Option<u32>,
#[builder(transparent, default = Some(99))] with_default: Option<u32>,
#[builder(transparent, default = Some(10))] with_default_2: Option<u32>,
#[builder(required)] regular: Option<u32>,
#[builder(required)] generic: Option<T>,
#[builder(required, into)] with_into: Option<u32>,
#[builder(required, default = Some(99))] with_default: Option<u32>,
#[builder(required, default = Some(10))] with_default_2: Option<u32>,
) -> impl fmt::Debug {
(regular, generic, with_into, with_default, with_default_2)
}

#[builder]
fn with_self<T: fmt::Debug>(
&self,
#[builder(transparent)] regular: Option<u32>,
#[builder(transparent)] generic: Option<T>,
#[builder(transparent, into)] with_into: Option<u32>,
#[builder(transparent, default = Some(99))] with_default: Option<u32>,
#[builder(transparent, default = Some(10))] with_default_2: Option<u32>,
#[builder(required)] regular: Option<u32>,
#[builder(required)] generic: Option<T>,
#[builder(required, into)] with_into: Option<u32>,
#[builder(required, default = Some(99))] with_default: Option<u32>,
#[builder(required, default = Some(10))] with_default_2: Option<u32>,
) -> impl fmt::Debug {
let _ = self;
(regular, generic, with_into, with_default, with_default_2)
Expand Down Expand Up @@ -135,7 +135,7 @@ mod attr_on {
#[test]
fn test_struct() {
#[derive(Debug, Builder)]
#[builder(on(_, transparent))]
#[builder(on(_, required))]
#[allow(dead_code)]
struct Sut<T> {
#[builder(start_fn)]
Expand Down Expand Up @@ -185,7 +185,7 @@ mod attr_on {

#[test]
fn test_free_fn() {
#[builder(on(_, transparent))]
#[builder(on(_, required))]
fn sut<T: fmt::Debug>(
#[builder(start_fn)] start_fn: u32,
regular: Option<u32>,
Expand Down Expand Up @@ -221,7 +221,7 @@ mod attr_on {

#[bon]
impl Sut {
#[builder(on(_, transparent))]
#[builder(on(_, required))]
fn sut<T: fmt::Debug>(
#[builder(start_fn)] start_fn: u32,
regular: Option<u32>,
Expand All @@ -240,7 +240,7 @@ mod attr_on {
)
}

#[builder(on(_, transparent))]
#[builder(on(_, required))]
fn with_self<T: fmt::Debug>(
&self,
#[builder(start_fn)] start_fn: u32,
Expand Down
Loading