Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a check for strip_option when using try_setter #286

Merged
merged 6 commits into from
Jul 25, 2023

Conversation

chanced
Copy link
Contributor

@chanced chanced commented Apr 5, 2023

Fixes #284.

@chanced
Copy link
Contributor Author

chanced commented Apr 5, 2023

One of the tests is failing and I don't know how to solve it. This is my first time working on a proc macro.

The test is:

   #[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);
        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()
        );
    }

The issue is formatting. The assertion fails due to commas:

Diff < left / right > :
<"# [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) }"
>"# [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) }"

Screenshot 2023-04-05 at 6 16 54 PM

@TedDriggs
Copy link
Collaborator

The issue appears to be the commas after the last parameters in the function signatures. Did rustfmt automatically add those?

If so, your best bet is probably to use #[rustfmt::skip] either on the whole test or on the assert_eq, and to add a comment saying why it needs to be disabled.

@chanced
Copy link
Contributor Author

chanced commented Apr 6, 2023

Ah, wasn't aware there was a cfg opt-out for formatting. Nice!

@chanced chanced marked this pull request as ready for review April 6, 2023 13:57
Copy link
Collaborator

@TedDriggs TedDriggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directionally looks good; some formatting and git hygiene requests and I think we'll be ready to merge.

derive_builder/CHANGELOG.md Outdated Show resolved Hide resolved
derive_builder/CHANGELOG.md Outdated Show resolved Hide resolved
derive_builder/CHANGELOG.md Outdated Show resolved Hide resolved
derive_builder_core/src/builder.rs Outdated Show resolved Hide resolved
derive_builder_core/src/setter.rs Outdated Show resolved Hide resolved
@TedDriggs
Copy link
Collaborator

I got pulled into a bunch of other changes in other projects due to the release of syn 2, but I'm coming back to this now.

@TedDriggs
Copy link
Collaborator

Looks like the syn version bump has impacted us too - I'll put together an update tomorrow that increases the MSRV to reflect that.

@TedDriggs
Copy link
Collaborator

@chanced can you rebase this?

@chanced
Copy link
Contributor Author

chanced commented Jul 25, 2023

@TedDriggs sorry, missed this earlier. Done.

@TedDriggs TedDriggs merged commit 2778cae into colin-kiegel:master Jul 25, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using both try_setter and setter(strip_option) does not compile
2 participants