Repository: sharkdp/hyperfine
File: src/export/mod.rs (Rust)

Code before:
            add_exporter("export-json", ExportType::Json)?;
            add_exporter("export-csv", ExportType::Csv)?;
            add_exporter("export-markdown", ExportType::Markdown)?;
            add_exporter("export-orgmode", ExportType::Orgmode)?;
        }
        Ok(export_manager)
    }

    /// Add an additional exporter to the ExportManager
    pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {
        let _ = File::create(filename)
            .with_context(|| format!("Could not create export file '{}'", filename))?;

        let exporter: Box<dyn Exporter> = match export_type {
            ExportType::Asciidoc => Box::new(AsciidocExporter::default()),
            ExportType::Csv => Box::new(CsvExporter::default()),
            ExportType::Json => Box::new(JsonExporter::default()),
            ExportType::Markdown => Box::new(MarkdownExporter::default()),
            ExportType::Orgmode => Box::new(OrgmodeExporter::default()),
        };

        self.exporters.push(ExporterWithFilename {
            exporter,
            filename: if filename == "-" {
                if env::consts::OS == "windows" {
                    "con:".to_string()
                } else {
                    "/dev/stdout".to_string()
                }
            } else {
                filename.to_string()
            },
        });

        Ok(())
    }

    /// Write the given results to all Exporters contained within this manager
    pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {
        for e in &self.exporters {
            let file_content = e.exporter.serialize(results, unit)?;
            write_to_file(&e.filename, &file_content)?;
        }
        Ok(())
    }
}

/// Write the given content to a file with the specified name
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
    let mut file = OpenOptions::new().write(true).open(filename)?;
    file.write_all(content)

Review comment (suggestion):
Thank you. Have you tested this on Windows and Linux? 

To distinguish between Windows and Linux, we use `cfg!(…)` elsewhere in hyperfines source code. The difference is that it is a compile-time switch. Your runtime switch might be favorable in certain (weird) scenarios, but I think I'd prefer to use `if cfg!(windows)` here instead of the `env::consts::OS` check.

Code after:
            add_exporter("export-csv", ExportType::Csv)?;
            add_exporter("export-markdown", ExportType::Markdown)?;
            add_exporter("export-orgmode", ExportType::Orgmode)?;
        }
        Ok(export_manager)
    }

    /// Add an additional exporter to the ExportManager
    pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {
        let _ = File::create(filename)
            .with_context(|| format!("Could not create export file '{}'", filename))?;

        let exporter: Box<dyn Exporter> = match export_type {
            ExportType::Asciidoc => Box::new(AsciidocExporter::default()),
            ExportType::Csv => Box::new(CsvExporter::default()),
            ExportType::Json => Box::new(JsonExporter::default()),
            ExportType::Markdown => Box::new(MarkdownExporter::default()),
            ExportType::Orgmode => Box::new(OrgmodeExporter::default()),
        };

        self.exporters.push(ExporterWithFilename {
            exporter,
            filename: if filename == "-" {
                if cfg!(windows) {
                    "con:".to_string()
                } else {
                    "/dev/stdout".to_string()
                }
            } else {
                filename.to_string()
            },
        });

        Ok(())
    }

    /// Write the given results to all Exporters contained within this manager
    pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {
        for e in &self.exporters {
            let file_content = e.exporter.serialize(results, unit)?;
            write_to_file(&e.filename, &file_content)?;
        }
        Ok(())
    }
}

/// Write the given content to a file with the specified name
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
    let mut file = OpenOptions::new().write(true).open(filename)?;
    file.write_all(content)
        .with_context(|| format!("Failed to export results to '{}'", filename))