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

Add a serde feature for (de)serializing Selectors #227

Merged
merged 3 commits into from
Feb 2, 2025

Conversation

jakubslaby09
Copy link
Contributor

Allow Selectors to be (de)serialized with serde.
This will enable devs who rely on serde to use this crate without writing their own wrapper type. One of the use cases is storing selectors in config files when building a program with scraper as a dependency.

Parsing errors are handled as custom serde errors

fn visit_str<...>(self, v: &str) -> Result<Self::Value, E> {
    Ok(Selector::parse(v).map_err(serde::de::Error::custom)?)
}

@adamreichold
Copy link
Member

I think the one thing that is worth discussing here is whether we want to commit to this serialization format when the implementation can be done downstream using existing public API. The CSS representation is obviously somewhat canonical, but there might be a more efficient binary representation?

@jakubslaby09
Copy link
Contributor Author

I feel like the need for a more efficient selector format is rare, so I would provide string serialization as a readable and time-proven default.
In case someone really needs to optimize for speed at the cost of readability and reliability, but still needs to use serde (i.e. when someone stores thousands of selectors, but a different program in a pipeline only supports json), then they can use something like a custom wrapper type with their own implementation.

Co-authored-by: Adam Reichold <adamreichold@users.noreply.github.com>
@adamreichold
Copy link
Member

Please have a look at the Clippy lint.

@adamreichold adamreichold merged commit 1a55337 into rust-scraper:master Feb 2, 2025
5 checks passed
jakubslaby09 added a commit to jakubslaby09/scwape that referenced this pull request Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants