Skip to content

Add support for custom error type for FromStr derive#494

Merged
tyranron merged 16 commits intoJelteF:masterfrom
kiendang:fromstr-customerror
Sep 10, 2025
Merged

Add support for custom error type for FromStr derive#494
tyranron merged 16 commits intoJelteF:masterfrom
kiendang:fromstr-customerror

Conversation

@kiendang
Copy link
Contributor

@kiendang kiendang commented Aug 1, 2025

Resolves #112
Related to #396

Synopsis

Add support for a #[from_str(error(error_ty[, error_fn]))] attribute to specify a custom error type for FromStr derive for enums (or structs with no fields) in case where the string does not match any variant. A conversion function to convert a derive_more::FromStrError to the custom error type might optionally be provided. Otherwise the custom error type T must satisfy derive_more::FromStrError: Into<T>.

strum provides similar functionality. See Peternator7/strum#380.

This approach could be used to set a custom error type for TryInto derive as well (#396).

Solution

Checklist

  • Documentation is updated (if required)
  • Tests are added/updated (if required)
  • CHANGELOG entry is added (if required)

@kiendang kiendang force-pushed the fromstr-customerror branch from 2e88556 to 3f205f5 Compare August 1, 2025 08:14
@kiendang kiendang marked this pull request as draft August 1, 2025 08:20
@kiendang kiendang force-pushed the fromstr-customerror branch from 3f205f5 to a70038a Compare August 1, 2025 08:25
@kiendang kiendang marked this pull request as ready for review August 1, 2025 08:27
@kiendang kiendang force-pushed the fromstr-customerror branch from a70038a to d9109bc Compare August 1, 2025 08:31
@tyranron tyranron assigned tyranron and kiendang and unassigned tyranron Sep 3, 2025
@tyranron tyranron added this to the 2.1.0 milestone Sep 3, 2025
Copy link
Collaborator

@tyranron tyranron left a comment

Choose a reason for hiding this comment

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

@kiendang sorry for some late feedback on this. Been quite busy lately. The implementation is quite nice, not mentioning several bikesheddings. Thank you for the great job!

Before merging this, however, I would like to approve the design with @JelteF.

@kiendang
Copy link
Contributor Author

kiendang commented Sep 4, 2025

@tyranron no worries. I do understand that's the nature of open source/volunteer work. Thanks for maintaining the library.

@kiendang kiendang force-pushed the fromstr-customerror branch from bed4972 to 38d1ca4 Compare September 6, 2025 06:19
@kiendang kiendang force-pushed the fromstr-customerror branch from ba0ea1c to c9e2a5f Compare September 6, 2025 06:47
@kiendang kiendang force-pushed the fromstr-customerror branch from c9e2a5f to 6171c02 Compare September 6, 2025 06:50
Co-authored-by: Jelte Fennema-Nio <github-tech@jeltef.nl>
Copy link
Collaborator

@tyranron tyranron left a comment

Choose a reason for hiding this comment

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

@kiendang thanks for such a nice PR and for all the effort!

I've bikeshedded a little bit the code, and also tried to add support for syn::ExprCall and syn::ExprClosure. However, the later requires syn/full feature to be enabled, which requires some other disputable workarounds in derive_more, so I'll dissect them into a separate PR.

@tyranron tyranron merged commit e0a0713 into JelteF:master Sep 10, 2025
17 checks passed
@kiendang
Copy link
Contributor Author

Thank you for the thorough review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

derive(FromStr): Support for mapping error to custom error type

3 participants