Skip to main content

cbtop/export_reporting/
builder.rs

1//! Fluent report builder API.
2
3use std::collections::HashMap;
4
5use super::exporter::ReportExporter;
6use super::types::{
7    BenchmarkMetric, BenchmarkReport, ComparisonEntry, ComparisonReport, ExportFormat, ReportType,
8};
9
10/// Report builder for fluent API
11#[derive(Debug)]
12pub struct ReportBuilder {
13    /// Report type
14    report_type: ReportType,
15    /// Export format
16    format: ExportFormat,
17    /// Title
18    title: String,
19    /// Benchmark metrics
20    metrics: Vec<BenchmarkMetric>,
21    /// Comparison entries
22    comparisons: Vec<ComparisonEntry>,
23    /// Metadata
24    metadata: HashMap<String, String>,
25}
26
27impl ReportBuilder {
28    /// Create new builder
29    pub fn new(report_type: ReportType) -> Self {
30        Self {
31            report_type,
32            format: ExportFormat::Json,
33            title: format!("{} Report", report_type.name()),
34            metrics: Vec::new(),
35            comparisons: Vec::new(),
36            metadata: HashMap::new(),
37        }
38    }
39
40    /// Set format
41    pub fn format(mut self, format: ExportFormat) -> Self {
42        self.format = format;
43        self
44    }
45
46    /// Set title
47    pub fn title(mut self, title: &str) -> Self {
48        self.title = title.to_string();
49        self
50    }
51
52    /// Add metric
53    pub fn metric(mut self, name: &str, value: f64, unit: &str) -> Self {
54        self.metrics.push(BenchmarkMetric::new(name, value, unit));
55        self
56    }
57
58    /// Add comparison
59    pub fn comparison(mut self, metric: &str, baseline: f64, current: f64, unit: &str) -> Self {
60        self.comparisons
61            .push(ComparisonEntry::new(metric, baseline, current, unit));
62        self
63    }
64
65    /// Add metadata
66    pub fn metadata(mut self, key: &str, value: &str) -> Self {
67        self.metadata.insert(key.to_string(), value.to_string());
68        self
69    }
70
71    /// Build and export
72    pub fn build(self) -> String {
73        match self.report_type {
74            ReportType::Benchmark | ReportType::Summary => {
75                let mut report = BenchmarkReport::new(&self.title);
76                report.metrics = self.metrics;
77                report.metadata = self.metadata;
78                ReportExporter::export_benchmark(&report, self.format)
79            }
80            ReportType::Comparison | ReportType::Regression => {
81                let mut report = ComparisonReport::new(&self.title);
82                report.entries = self.comparisons;
83                ReportExporter::export_comparison(&report, self.format)
84            }
85        }
86    }
87}