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

build_method into type parameters #104

Closed
rksm opened this issue Jul 14, 2023 · 2 comments
Closed

build_method into type parameters #104

rksm opened this issue Jul 14, 2023 · 2 comments

Comments

@rksm
Copy link

rksm commented Jul 14, 2023

Hello and thank you for this amazing project! It makes builder type state so much easier to use, it's a joy!

I've come across a small issue. I have

#[derive(typed_builder::TypedBuilder)]
#[builder(build_method(into = Result<Index>))]
pub struct IndexBuilder { ... }

impl From<IndexBuilder> for Result<Index> {
    fn from(value: IndexBuilder) -> Self { ... }
}

into = Result<Index> is not parsed correctly as apparently the macro does not expect type params. I can work around this with type IndexResult = Result<Index>; but it would be cool if this could be supported directly.

Thank you!

@idanarye
Copy link
Owner

#95 is probably a better way to do what you want to do. It's still a WiP though, so you can't use it yet.

The problem is that I parse #[builder(...)] attribute by pretending it's a function call and the settings are assignments (Rust - or at least the syn crate - treats assignments as expressions at the parser level and only disallows them at a later stage). This allows me to use complex syntax instead of wrapping everything with strings, but the downside is cases like this.

Maybe some day I'll replace it with a manual parsing (that still uses the syn crate, but doesn't pretend the attribute is something it is not) which will make such syntax possible. Don't hold your breath though.

Luckily, the turbofish syntax - which is required in expressions - can also be used for types. This means that this works:

#[builder(build_method(into = Result::<Index>))]

Not as pretty, but it's going to have to do.

@rksm
Copy link
Author

rksm commented Jul 15, 2023

Yep, I understand, parsing macros can be gnarly. Your suggested workaround is good enough, thanks!

@rksm rksm closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
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

No branches or pull requests

2 participants