Skip to content

Commit

Permalink
Merge pull request #286 from chanced/issue_284
Browse files Browse the repository at this point in the history
Adds a check for `strip_option` when using `try_setter`
  • Loading branch information
TedDriggs committed Jul 25, 2023
2 parents 0be32a4 + c141f36 commit 2778cae
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions derive_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - 2023-07-25
- Make try-setters inherit `strip_option` from `setter` for `try_setter`. Using these settings together previously caused a compile error #284

## [0.13.0] - 2023-07-24
- Bump MSRV to 1.56.0

Expand Down
22 changes: 21 additions & 1 deletion derive_builder/tests/setter_custom.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryInto;

#[macro_use]
extern crate pretty_assertions;
#[macro_use]
Expand All @@ -16,6 +18,8 @@ struct SetterCustom {
setter_custom_with_explicit_default: u32,
#[builder(setter(custom = "true", strip_option))]
setter_custom_with_strip_option: Option<u32>,
#[builder(try_setter, setter(custom = "true", strip_option))]
setter_custom_with_strip_option_try_setter: Option<u32>,
}

// compile test
Expand Down Expand Up @@ -44,6 +48,19 @@ impl SetterCustomBuilder {
self.setter_custom_with_strip_option = Some(Some(6));
self
}
fn setter_custom_with_strip_option_try_setter(&mut self) -> &mut Self {
self.setter_custom_with_strip_option_try_setter = Some(Some(32));
self
}
}

struct TryIntoU32 {}
impl TryInto<u32> for TryIntoU32 {
type Error = ();

fn try_into(self) -> Result<u32, Self::Error> {
Ok(32)
}
}

#[test]
Expand All @@ -58,6 +75,7 @@ fn setter_custom_defaults() {
setter_custom_by_explicit_opt_out: 0,
setter_custom_with_explicit_default: 4,
setter_custom_with_strip_option: None,
setter_custom_with_strip_option_try_setter: None
}
);
}
Expand All @@ -70,6 +88,7 @@ fn setter_custom_setters_called() {
.setter_custom_by_explicit_opt_out(42)
.setter_custom_with_explicit_default() // set to 43
.setter_custom_with_strip_option() // set to 6
.setter_custom_with_strip_option_try_setter() // set to 32
.build()
.unwrap();

Expand All @@ -80,7 +99,8 @@ fn setter_custom_setters_called() {
setter_custom_shorthand: 2,
setter_custom_by_explicit_opt_out: 42,
setter_custom_with_explicit_default: 43,
setter_custom_with_strip_option: Some(6)
setter_custom_with_strip_option: Some(6),
setter_custom_with_strip_option_try_setter: Some(32)
}
);
}
6 changes: 6 additions & 0 deletions derive_builder/tests/try_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ fn renamed() {
.build()
.expect("All fields were provided");
}

#[derive(Debug, PartialEq, Builder)]
#[builder(try_setter, setter(into, strip_option))]
struct MaybeIpsum {
pub source: Option<MyAddr>,
}
41 changes: 41 additions & 0 deletions derive_builder_core/src/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ impl<'a> ToTokens for Setter<'a> {
if builder_field_is_option {
converted = wrap_expression_in_some(crate_root, converted);
}
if stripped_option {
converted = wrap_expression_in_some(crate_root, converted);
}

tokens.append_all(quote!(
#(#attrs)*
Expand Down Expand Up @@ -463,6 +466,44 @@ mod tests {
.to_string()
);
}
#[test]
fn strip_option_try_setter() {
let ty = parse_quote!(Option<Foo>);
let mut setter = default_setter!();
setter.strip_option = true;
setter.try_setter = true;
setter.generic_into = true;
setter.field_type = BuilderFieldType::Optional(&ty);
#[rustfmt::skip]
assert_eq!(
quote!(#setter).to_string(),
quote!(
#[allow(unused_mut)]
pub fn foo<VALUE: ::db::export::core::convert::Into<Foo>>(
&mut self,
value: VALUE
) -> &mut Self {
let mut new = self;
new.foo = ::db::export::core::option::Option::Some(
::db::export::core::option::Option::Some(value.into())
);
new
}
pub fn try_foo<VALUE: ::db::export::core::convert::TryInto<Foo>>(
&mut self,
value: VALUE
) -> ::db::export::core::result::Result<&mut Self, VALUE::Error> {
let converted: Foo = value.try_into()?;
let mut new = self;
new.foo = ::db::export::core::option::Option::Some(
::db::export::core::option::Option::Some(converted)
);
Ok(new)
}
)
.to_string()
);
}

// including try_setter
#[test]
Expand Down

0 comments on commit 2778cae

Please sign in to comment.