Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,30 @@ fn build_command() -> Command {
If the option is not given, the time unit is determined automatically. \
This option affects the standard output as well as all export formats except for CSV and JSON."),
)
.arg(
Arg::new("export")
.long("export")
.action(ArgAction::Append)
.value_name("FILE")
.value_hint(ValueHint::FilePath)
.num_args(0..=1)
.default_missing_value("export.json")
.help(
"Export timing results to the given FILE.
\n\
Supported formats:\n\
.json Includes summary and individual run timings (in seconds).\n\
.csv Summary only (in seconds).\n\
.md Markdown table.\n\
.adoc AsciiDoc table.\n\
.org Org-mode table.\n\
\n\
Notes:\n\
- Repeat --export to generate multiple formats.\n\
- Except for JSON and CSV, the output time unit can be changed using '--time-unit' option.\n\
- Defaults to JSON for unsupported or missing file extensions."
),
)
.arg(
Arg::new("export-asciidoc")
.long("export-asciidoc")
Expand Down
27 changes: 27 additions & 0 deletions src/export/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::ffi::OsStr;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::Path;

mod asciidoc;
mod csv;
Expand Down Expand Up @@ -83,6 +85,14 @@ impl ExportManager {
time_unit,
sort_order,
};

if let Some(args) = matches.get_many::<String>("export") {
for filename in args {
let exporttype = get_export_type_from_filename(filename);
export_manager.add_exporter(exporttype, filename)?;
}
}

{
let mut add_exporter = |flag, exporttype| -> Result<()> {
if let Some(filename) = matches.get_one::<String>(flag) {
Expand All @@ -96,6 +106,7 @@ impl ExportManager {
add_exporter("export-markdown", ExportType::Markdown)?;
add_exporter("export-orgmode", ExportType::Orgmode)?;
}

Ok(export_manager)
}

Expand Down Expand Up @@ -160,3 +171,19 @@ fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
file.write_all(content)
.with_context(|| format!("Failed to export results to '{filename}'"))
}

/// Deduces the export type from the file extension. Defaults to JSON.
fn get_export_type_from_filename(filename: &str) -> ExportType {
match Path::new(filename)
.extension()
.and_then(OsStr::to_str)
.map(|s| s.to_ascii_lowercase())
.as_deref()
{
Some("adoc" | "asciidoc") => ExportType::Asciidoc,
Some("csv") => ExportType::Csv,
Some("md") => ExportType::Markdown,
Some("org") => ExportType::Orgmode,
Some("json") | _ => ExportType::Json,
}
}
Loading