Skip to content

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

3 participants