Allow specifying a custom delimiter for sequence fields#29
Open
FuegoFro wants to merge 1 commit into
Open
Conversation
This allows adding a `#[recap(delimiter_regex = "...")]` attribute to a field with a sequence-type value which is used when splitting the string. Currently the string is always split on `","`, but sometimes lists aren't comma joined. This adds the ability to split strings with an arbitrary regex to handle more situations. This was inspired by https://adventofcode.com/2023/day/4, where splitting on spaces is useful. Note that this was also exposed via the functional bring-your-own-captures API in the form of a new `recap::from_captures_with_options` method. Note that this also refactors the derive code to share a common `RE` instance rather than duplicating it in each method that needs access to the regex. This common location is also where the new field options are stored. This also adds documentation for the added attribute and a doctest to both demonstrate and verify the behavior. Since this is a new attribute and behavior, it should probably be added to the changelog.
FuegoFro
added a commit
to FuegoFro/recap
that referenced
this pull request
Dec 28, 2023
This is a quick followup to softprops#29 to reduce the duplication of the tuple type `(&'a str, &'a str, Option<&'a FieldOptions>)` by just using `Val<'a>`, which is a struct with the same contents anyway. Verified by checking that the test still pass. This doesn't change the public API so no need to update the changelog.
FuegoFro
commented
Dec 28, 2023
| &format!("RECAP_IMPL_FOR_{}", item.ident.to_string()), | ||
| Span::call_site(), | ||
| ); | ||
| let injector = Ident::new(&format!("RECAP_IMPL_FOR_{}", item.ident), Span::call_site()); |
Author
There was a problem hiding this comment.
This is just removing the .to_string() since clippy called it out as unnecessary.
| struct Vars<'a, Iter>(Iter) | ||
| where | ||
| Iter: IntoIterator<Item = (&'a str, &'a str)>; | ||
| Iter: IntoIterator<Item = (&'a str, &'a str, Option<&'a FieldOptions>)>; |
Author
There was a problem hiding this comment.
I have a follow-up PR that changes all these duplicated (&'a str, &'a str, Option<&'a FieldOptions>) into Val<'a> but figured that might be better as a separate change. Happy to include it here if you'd rather though!
Comment on lines
+429
to
+437
| from_iter(re.capture_names().flatten().filter_map(|name| { | ||
| caps.name(name).map(|val| { | ||
| ( | ||
| name, | ||
| val.as_str(), | ||
| field_options.and_then(|fo| fo.get(name)), | ||
| ) | ||
| }) | ||
| })) |
Author
There was a problem hiding this comment.
Some of the reworking of chained methods here also came from clippy's suggestions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What did you implement:
This allows adding a
#[recap(delimiter_regex = "...")]attribute to a field with a sequence-type value which is used when splitting the string. Currently the string is always split on",", but sometimes lists aren't comma joined. This adds the ability to split strings with an arbitrary regex to handle more situations. This was inspired by https://adventofcode.com/2023/day/4, where splitting on spaces is useful. Note that this was also exposed via the functional bring-your-own-captures API in the form of a newrecap::from_captures_with_optionsmethod.Note that this also refactors the derive code to share a common
REinstance rather than duplicating it in each method that needs access to the regex. This common location is also where the new field options are stored.How did you verify your change:
This also adds documentation for the added attribute and a doctest to both demonstrate and verify the behavior.
What (if anything) would need to be called out in the CHANGELOG for the next release:
Since this is a new attribute and behavior, it should probably be added to the changelog.