Do not copy export items between exporters, write from central vec

This commit is contained in:
Steve Pentland authored and David Peter committed 2018-03-19 07:53:10 +01:00
1 parent 12d8fdbf61
commit c4688a7386
2 files changed
+8 -16

No files matched your search

+2 -8
View File
@@ -4,18 +4,13 @@ use csv::WriterBuilder;
use std::io::Result;
pub struct CsvExporter {
results: Vec<ExportEntry>,
out_file: String,
}
impl ResultExporter for CsvExporter {
fn add_entry(&mut self, entry: ExportEntry) {
self.results.push(entry);
}
fn write(&self) -> Result<()> {
fn write(&self, results: &Vec<ExportEntry>) -> Result<()> {
let mut writer = WriterBuilder::new().from_path(&self.out_file)?;
for res in &self.results {
for res in results {
writer.serialize(res)?;
}
Ok(())
@@ -25,7 +20,6 @@ impl ResultExporter for CsvExporter {
impl CsvExporter {
pub fn new(file_name: String) -> Self {
CsvExporter {
results: Vec::new(),
out_file: file_name,
}
}
+6 -8
View File
@@ -42,11 +42,9 @@ pub enum ResultExportType {
/// A ResultExporter is responsible for writing all results to the
/// appropriate file
pub trait ResultExporter {
/// Add an additional entry to the exporter for writing
fn add_entry(&mut self, entry: ExportEntry);
trait ResultExporter {
/// Write all entries to the target destination
fn write(&self) -> Result<()>;
fn write(&self, values: &Vec<ExportEntry>) -> Result<()>;
}
/// The ExportManager handles coordination of multiple ResultExporters
@@ -65,12 +63,14 @@ pub trait ExportManager {
pub fn create_export_manager() -> Box<ExportManager> {
Box::new(Exporter {
exporters: Vec::new(),
results: Vec::new(),
})
}
/// The Exporter is the internal implementation of the ExportManager
struct Exporter {
exporters: Vec<Box<ResultExporter>>,
results: Vec<ExportEntry>,
}
impl ExportManager for Exporter {
@@ -82,14 +82,12 @@ impl ExportManager for Exporter {
}
fn add_result(&mut self, result: ExportEntry) {
for exp in self.exporters.iter_mut() {
exp.add_entry(result.clone());
}
self.results.push(result);
}
fn write_results(&self) -> Result<()> {
for exp in &self.exporters {
exp.write()?;
exp.write(&self.results)?;
}
Ok(())
}