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

Fix empty output in csv_vectors example #89

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions examples/csv_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_qs as qs;

use serde::de::DeserializeOwned;

use std::default::Default;

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -24,21 +22,25 @@ fn main() {
fn from_csv<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: serde::Deserializer<'de>,
T: DeserializeOwned,
T: DeserializeOwned + std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Debug,
{
deserializer.deserialize_str(CSVVecVisitor::<T>::default())
}

/// Visits a string value of the form "v1,v2,v3" into a vector of bytes Vec<u8>
struct CSVVecVisitor<T: DeserializeOwned>(std::marker::PhantomData<T>);
struct CSVVecVisitor<T: DeserializeOwned + std::str::FromStr>(std::marker::PhantomData<T>);

impl<T: DeserializeOwned> Default for CSVVecVisitor<T> {
impl<T: DeserializeOwned + std::str::FromStr> Default for CSVVecVisitor<T> {
fn default() -> Self {
CSVVecVisitor(std::marker::PhantomData)
}
}

impl<'de, T: DeserializeOwned> serde::de::Visitor<'de> for CSVVecVisitor<T> {
impl<'de, T: DeserializeOwned + std::str::FromStr> serde::de::Visitor<'de> for CSVVecVisitor<T>
where
<T as std::str::FromStr>::Err: std::fmt::Debug, // handle the parse error in a generic way
{
type Value = Vec<T>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
Expand All @@ -49,12 +51,31 @@ impl<'de, T: DeserializeOwned> serde::de::Visitor<'de> for CSVVecVisitor<T> {
where
E: serde::de::Error,
{
// Treat the comma-separated string as a single record in a CSV.
let mut rdr = csv::ReaderBuilder::new()
.has_headers(false)
.from_reader(s.as_bytes());

// Try to get the record and collect its values into a vector.
let mut output = Vec::new();
let mut items = csv::Reader::from_reader(s.as_bytes());
for res in items.deserialize() {
let item: T = res
.map_err(|e| E::custom(format!("could not deserialize sequence value: {:?}", e)))?;
output.push(item);
for result in rdr.records() {
match result {
Ok(record) => {
for field in record.iter() {
output.push(
field
.parse::<T>()
.map_err(|_| E::custom("Failed to parse field"))?,
);
}
}
Err(e) => {
return Err(E::custom(format!(
"could not deserialize sequence value: {:?}",
e
)));
}
}
}

Ok(output)
Expand Down