BurntSushi / rust-csv

A CSV parser for Rust, with Serde support.
The Unlicense
1.72k stars 219 forks source link

Add Support for serde_transcode::transcode #343

Closed justin-lyon closed 11 months ago

justin-lyon commented 11 months ago

Thank you for taking the time to file a bug report. The following describes some guidelines to creating a minimally useful ticket.

Above all else: do not describe your problem, SHOW your problem.

What version of the csv crate are you using?

1.3.0

Briefly describe the question, bug or feature request.

I'm attempting to follow the serde transcode example from the serde docs here, but attempting to go from one file format into a csv. rust-csv doesn't appear to support this behavior because it only serializes one row at a time. Here is the rust cookbook for copying one csv into another csv which has to loop over rows and serialize them one at a time. This is prohibitive when the source data has a large schema that is too big for me to write a struct for every file I want to convert.

Include a complete program demonstrating a problem.

use std::fs::File;
use std::io::{self, BufReader, BufWriter};

fn main() {
  let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR");
  let source_path = format!("{}/assets/source/somefile.yaml", cargo_manifest_dir);
  let target_path = format!("{}/assets/target/somefile.csv", cargo_manifest_dir);

  let in_file = BufReader::new(File::open(source_path).unwrap());
  let mut out_file = BufWriter::new(File::create(target_path).unwrap());

  let mut deserializer = serde_yaml::Deserializer::from_reader(in_file);
  let mut serializer = csv::Writer::from_writer(out_file);

  serde_transcode::transcode(&mut deserializer, &mut serializer).unwrap();
}

What is the observed behavior of the code above?

image

What is the expected or desired behavior of the code above?

Convert a whole file from yaml to csv using the crate serde-transcode

BurntSushi commented 11 months ago

I don't think it's feasible to add support for this. You can't convert arbitrary yaml into csv. Csv is a flat format. Yaml (and the serde data model) is not.

justin-lyon commented 11 months ago

Of course. You're right. I see the issue there.

Thanks for your patient response.