samscott89 / serde_qs

Serde support for querystring-style strings
Apache License 2.0
193 stars 68 forks source link

`csv_vectors` example doesn't work #83

Open Gordon01 opened 1 year ago

Gordon01 commented 1 year ago

I've added a test in this PR https://github.com/samscott89/serde_qs/pull/82 which highlights the problem:

thread 'deserialize_sequence' panicked at 'assertion failed: `(left == right)`
  left: `Query { r: [], s: 12 }`,
 right: `Query { r: [1, 2, 3], s: 12 }`', examples\csv_vectors.rs:32:5
samscott89 commented 1 year ago

Oof, thanks for finding that and raising!

Would you be up for working on a fix?

Gordon01 commented 1 year ago

Unfortunately, I couldn't make it to work due to my relatively low experience with serde. I've just made an ad-hoc solution that worked in my case:

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
            <T as FromStr>::Err: fmt::Display,
        {
            v.split(',')
                .map(|i| i.parse::<T>().map_err(E::custom))
                .collect()
        }

I would be much happier in making a PR, adding such functionality into serde_qs itself because too many people need such a parser for sequences. Something like a:

#[derive(Serialize, Deserialize)]
struct Users {
    #[serde_qs(value_delimiter = ",")]
    users: Vec<u64>,
}

instead of

#[derive(Serialize, Deserialize)]
struct Users {
    #[serde(default, deserialize_with = "deserialize_list")]
    users: Vec<u64>,
}

would be much greater)

But I don't know how to code it right now (