Skip to content

Support #[from_str(rename_all = "...")] attribute (#216) #467

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

Open
wants to merge 40 commits into
base: master
Choose a base branch
from

Conversation

tyranron
Copy link
Collaborator

@tyranron tyranron commented May 6, 2025

Resolves #216
Requires #468, #469

Synopsis

We have #[display(rename_all = "...")] attribute for flat enums. The counterpart for FromStr derive is missing.

Solution

This PR adds support #[from_str(rename_all = "...")] attribute on enums. Once this attribute is specified, the matching is always exact (for now, by default, matching could be fuzzy if there is no similarly named variants).

Additionally

This PR fully reworks FromStr derive implementation to avoid using old utils machinery.

Adds support for unit structs (like struct Unit;) for the counterpart behavior of Display.

Adds tests/compile_fail/from_str.

Checklist

  • Documentation is updated
  • Tests are added/updated
  • CHANGELOG entry is added

@tyranron tyranron added this to the 2.1.0 milestone May 6, 2025
@tyranron tyranron self-assigned this May 6, 2025
@tyranron tyranron mentioned this pull request May 7, 2025
3 tasks
@tyranron
Copy link
Collaborator Author

tyranron commented May 7, 2025

@JelteF this PR has grown a little bit too much than I've expected, so I've dissected some of its logical parts into separate PRs to keep things simpler:

@tyranron tyranron mentioned this pull request May 7, 2025
3 tasks
tyranron added a commit that referenced this pull request May 8, 2025
Required for #467, #469

This PR fully reworks `FromStr` derive implementation to avoid using old
`utils` machinery.

Also, adds `compile_fail` tests for `FromStr`.
tyranron added 2 commits May 9, 2025 11:32
# Conflicts:
#	impl/src/from_str.rs
#	tests/from_str.rs
tyranron added a commit that referenced this pull request May 13, 2025
Required for #467  
Requires #468

## Synopsis

We support flat structs for `Display`:
```rust
#[derive(Display)]
struct Foo;
```
But `FromStr` doesn't support it.

## Solution

Allow using `#[derive(FromStr)]` on structs with no fields.

Similarly to enums it will use fuzzy matching. Exact matching would be
possible to enable in #469 by using `#[from_str(rename_all = "...")]`
attribute.
@tyranron tyranron marked this pull request as ready for review May 14, 2025 19:10
@tyranron tyranron enabled auto-merge (squash) May 14, 2025 19:12
@tyranron tyranron requested a review from JelteF May 14, 2025 19:12
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.

Serde like rename for Display & FromStr trait on enum
1 participant