Skip to content

implement Deserialize for Regex? #532

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

Closed
matthiasbeyer opened this issue Oct 25, 2018 · 4 comments
Closed

implement Deserialize for Regex? #532

matthiasbeyer opened this issue Oct 25, 2018 · 4 comments
Labels

Comments

@matthiasbeyer
Copy link

This is somewhat related to #258

I am thinking about the following usecase: When I have a application where I can define regexes in a config file and the app reads the config on boot, I cannot use serde_derive with Regex:

#[derive(Deserialize)]
struct Config {
    something: Regex,
}

But it would be nice to have that possibility. What do you think? Or is there a boilerplate-free way around this I do not see right now?

@BurntSushi
Copy link
Member

serde_regex is what you want I think. :-)

@RReverser
Copy link
Contributor

@BurntSushi Oh thank you, finally someone did it and I don't have to reimplement wrapper each time myself :) (was being too lazy)

@sameer
Copy link

sameer commented Oct 31, 2018

I too didn't know this existed -- thanks!

@teskje
Copy link

teskje commented Mar 21, 2024

Using serde_regex might actually not be a great idea. A serialize-deserialize roundtrip will drop flags, as reported here: tailhook/serde-regex#14. Which means after deserialization you can end up with a Regex that's semantically different from what you had before, which depending on your use case might cause all kinds of correctness bugs.

Skimming the API of the Regex type it doesn't seem to provide a way to retrieve the flags used to compile it, so there is not much serde_regex can do about this.

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

5 participants