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

Convenient method for match and parsing values implementing FromStr #831

Closed
kaoet opened this issue Jan 25, 2022 · 1 comment
Closed

Convenient method for match and parsing values implementing FromStr #831

kaoet opened this issue Jan 25, 2022 · 1 comment
Labels

Comments

@kaoet
Copy link

kaoet commented Jan 25, 2022

I wish I could match a string and parse the captured groups in a single call. I imagine the new method return a Result<tuple, SomeError>, where each type inside that tuple is having FromStr implemented.

let log_line = "Time: 2014-11-28T12:45:59.324310806Z IP: 192.168.3.4 User: kaoet";
let re = Regex::new(r"Time: (.+?) IP: (.+?) User: (.+)");
let (time, ip, user) : (DateTime<Utc>, IpAddr, String) = re.match_and_parse(log_line).unwrap();

I know one of the problem here is the choice of SomeError, since different FromStr have different Err type. Maybe Box<dyn Error + 'lifetime_of_the_str> is a viable solution.

@BurntSushi
Copy link
Member

It's not really clear to me how this machinery is supposed to work. What enables FromStr to work on tuples, for example? The error situation also looks pretty unfortunate. And what happens when there isn't a match? On top of that, I don't think this actually saves much work, and thus isn't buying that much convenience. I'd encourage you to write out the alternative and compare them.

Finally, this would also have the same issues as #824. Your API doesn't account for captures that don't participate in a match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants