bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Rich ASCII Reporting Module (ML-011, ML-012)
//!
//! Provides visual feedback using ASCII box drawing and sparklines
//! following Toyota Way principles of visual management (Mieruka).
//!
//! # Toyota Way Principles
//!
//! - **Mieruka** (Visual Management): Make problems visible immediately
//! - **Andon**: Signal when quality issues are detected
//! - **Genchi Genbutsu**: Go and see the actual data
//!
//! # References
//!
//! - BASHRS-SPEC-ML-011: ASCII Box Drawing
//! - BASHRS-SPEC-ML-012: Sparkline Generation
//! - Tufte, E. (2001). "The Visual Display of Quantitative Information"

use std::fmt::Write;
use std::time::Duration;

/// Characters for box drawing (Unicode box-drawing characters)
pub mod chars {
    pub const TOP_LEFT: char = '';
    pub const TOP_RIGHT: char = '';
    pub const BOTTOM_LEFT: char = '';
    pub const BOTTOM_RIGHT: char = '';
    pub const HORIZONTAL: char = '';
    pub const VERTICAL: char = '';
    pub const T_DOWN: char = '';
    pub const T_UP: char = '';
    pub const T_RIGHT: char = '';
    pub const T_LEFT: char = '';
    pub const CROSS: char = '';

    // Sparkline characters (Unicode block elements)
    pub const SPARK_EMPTY: char = ' ';
    pub const SPARK_1_8: char = '';
    pub const SPARK_2_8: char = '';
    pub const SPARK_3_8: char = '';
    pub const SPARK_4_8: char = '';
    pub const SPARK_5_8: char = '';
    pub const SPARK_6_8: char = '';
    pub const SPARK_7_8: char = '';
    pub const SPARK_FULL: char = '';

    // Status indicators
    pub const CHECK: &str = "";
    pub const CROSS_MARK: &str = "";
    pub const WARNING: &str = "";
    pub const INFO: &str = "";
    pub const BULLET: &str = "";
}

/// Quality grade based on score
/// Ordered from worst (F) to best (APlus) for comparison
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Grade {
    /// F (< 50%)
    F,
    /// D (50-54%)
    D,
    /// C- (55-59%)
    CMinus,
    /// C (60-64%)
    C,
    /// C+ (65-69%)
    CPlus,
    /// B- (70-74%)
    BMinus,
    /// B (75-79%)
    B,
    /// B+ (80-84%)
    BPlus,
    /// A- (85-89%)
    AMinus,
    /// A (90-94%)
    A,
    /// A+ (95-100%)
    APlus,
}

impl Grade {
    /// Calculate grade from percentage score
    pub fn from_percentage(pct: f64) -> Self {
        match pct as u32 {
            95..=100 => Grade::APlus,
            90..=94 => Grade::A,
            85..=89 => Grade::AMinus,
            80..=84 => Grade::BPlus,
            75..=79 => Grade::B,
            70..=74 => Grade::BMinus,
            65..=69 => Grade::CPlus,
            60..=64 => Grade::C,
            55..=59 => Grade::CMinus,
            50..=54 => Grade::D,
            _ => Grade::F,
        }
    }

    /// Get letter representation
    pub fn as_letter(&self) -> &'static str {
        match self {
            Grade::APlus => "A+",
            Grade::A => "A",
            Grade::AMinus => "A-",
            Grade::BPlus => "B+",
            Grade::B => "B",
            Grade::BMinus => "B-",
            Grade::CPlus => "C+",
            Grade::C => "C",
            Grade::CMinus => "C-",
            Grade::D => "D",
            Grade::F => "F",
        }
    }

    /// Check if grade is passing (C- or above)
    pub fn is_passing(&self) -> bool {
        *self >= Grade::CMinus
    }
}

impl std::fmt::Display for Grade {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_letter())
    }
}

/// Generate a sparkline from a slice of values
pub fn sparkline(values: &[f64]) -> String {
    if values.is_empty() {
        return String::new();
    }

    let min = values.iter().copied().fold(f64::INFINITY, f64::min);
    let max = values.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    let range = max - min;

    let sparks = [
        chars::SPARK_1_8,
        chars::SPARK_2_8,
        chars::SPARK_3_8,
        chars::SPARK_4_8,
        chars::SPARK_5_8,
        chars::SPARK_6_8,
        chars::SPARK_7_8,
        chars::SPARK_FULL,
    ];

    values
        .iter()
        .map(|&v| {
            if range == 0.0 {
                sparks.get(3).copied().unwrap_or(chars::SPARK_4_8) // Middle value when all same
            } else {
                let normalized = (v - min) / range;
                let index = ((normalized * 7.0).round() as usize).min(7);
                sparks.get(index).copied().unwrap_or(chars::SPARK_FULL)
            }
        })
        .collect()
}

/// Generate a progress bar
pub fn progress_bar(current: f64, total: f64, width: usize) -> String {
    let pct = if total > 0.0 { current / total } else { 0.0 };
    let filled = ((pct * width as f64).round() as usize).min(width);
    let empty = width.saturating_sub(filled);

    format!(
        "[{}{}] {:.1}%",
        chars::SPARK_FULL.to_string().repeat(filled),
        chars::SPARK_EMPTY.to_string().repeat(empty),
        pct * 100.0
    )
}

/// Rich report builder following Toyota Way visual management
pub struct ReportBuilder {
    sections: Vec<ReportSection>,
    title: String,
    width: usize,
}

/// A section in the report
#[derive(Debug, Clone)]
pub struct ReportSection {
    pub title: String,
    pub items: Vec<ReportItem>,
}

/// An item in a report section
#[derive(Debug, Clone)]
pub enum ReportItem {
    /// Key-value pair
    KeyValue { key: String, value: String },
    /// Status indicator with message
    Status { passed: bool, message: String },
    /// Progress bar
    Progress {
        label: String,
        current: f64,
        total: f64,
    },
    /// Sparkline with label
    Sparkline { label: String, values: Vec<f64> },
    /// Table row
    TableRow { cells: Vec<String> },
    /// Plain text
    Text(String),
    /// Metric with grade
    Metric {
        name: String,
        value: f64,
        unit: String,
        grade: Grade,
    },
}

impl ReportBuilder {
    /// Create a new report builder
    pub fn new(title: &str) -> Self {
        Self {
            sections: Vec::new(),
            title: title.to_string(),
            width: 60,
        }
    }

    /// Set report width
    pub fn width(mut self, width: usize) -> Self {
        self.width = width;
        self
    }

    /// Add a section
    pub fn section(mut self, title: &str) -> Self {
        self.sections.push(ReportSection {
            title: title.to_string(),
            items: Vec::new(),
        });
        self
    }

    /// Add an item to the current section
    pub fn item(mut self, item: ReportItem) -> Self {
        if let Some(section) = self.sections.last_mut() {
            section.items.push(item);
        }
        self
    }

    /// Add a key-value pair
    pub fn kv(self, key: &str, value: &str) -> Self {
        self.item(ReportItem::KeyValue {
            key: key.to_string(),
            value: value.to_string(),
        })
    }

    /// Add a status item
    pub fn status(self, passed: bool, message: &str) -> Self {
        self.item(ReportItem::Status {
            passed,
            message: message.to_string(),
        })
    }

    /// Add a progress bar
    pub fn progress(self, label: &str, current: f64, total: f64) -> Self {
        self.item(ReportItem::Progress {
            label: label.to_string(),
            current,
            total,
        })
    }

    /// Add a sparkline
    pub fn sparkline(self, label: &str, values: Vec<f64>) -> Self {
        self.item(ReportItem::Sparkline {
            label: label.to_string(),
            values,
        })
    }

    /// Add a metric with grade
    pub fn metric(self, name: &str, value: f64, unit: &str) -> Self {
        let grade = Grade::from_percentage(value);
        self.item(ReportItem::Metric {
            name: name.to_string(),
            value,
            unit: unit.to_string(),
            grade,
        })
    }

    /// Build the final report
    pub fn build(&self) -> RichReport {
        RichReport {
            title: self.title.clone(),
            sections: self.sections.clone(),
            width: self.width,
        }
    }
}

/// A rich visual report
#[derive(Debug, Clone)]
pub struct RichReport {
    pub title: String,
    pub sections: Vec<ReportSection>,
    pub width: usize,
}

impl RichReport {
    /// Render report to string
    pub fn render(&self) -> String {
        let mut out = String::new();

        // Title box
        self.draw_title_box(&mut out);

        // Sections
        for section in &self.sections {
            self.draw_section(&mut out, section);
        }

        // Footer
        self.draw_footer(&mut out);

        out
    }

    fn draw_title_box(&self, out: &mut String) {
        let inner_width = self.width - 2;
        let title_padding = (inner_width.saturating_sub(self.title.len())) / 2;

        // Top border
        out.push(chars::TOP_LEFT);
        for _ in 0..inner_width {
            out.push(chars::HORIZONTAL);
        }
        out.push(chars::TOP_RIGHT);
        out.push('\n');

        // Title line
        out.push(chars::VERTICAL);
        for _ in 0..title_padding {
            out.push(' ');
        }
        out.push_str(&self.title);
        for _ in 0..(inner_width - title_padding - self.title.len()) {
            out.push(' ');
        }
        out.push(chars::VERTICAL);
        out.push('\n');

        // Bottom border
        out.push(chars::BOTTOM_LEFT);
        for _ in 0..inner_width {
            out.push(chars::HORIZONTAL);
        }
        out.push(chars::BOTTOM_RIGHT);
        out.push('\n');
    }

    fn draw_section(&self, out: &mut String, section: &ReportSection) {
        // Section header
        out.push('\n');
        let _ = writeln!(
            out,
            "{} {} {}",
            chars::T_RIGHT,
            section.title,
            chars::HORIZONTAL
                .to_string()
                .repeat(self.width.saturating_sub(section.title.len() + 4))
        );

        // Items
        for item in &section.items {
            self.draw_item(out, item);
        }
    }

    fn draw_item(&self, out: &mut String, item: &ReportItem) {
        match item {
            ReportItem::KeyValue { key, value } => {
                let _ = writeln!(out, "  {}: {}", key, value);
            }
            ReportItem::Status { passed, message } => {
                let icon = if *passed {
                    chars::CHECK
                } else {
                    chars::CROSS_MARK
                };
                let _ = writeln!(out, "  {} {}", icon, message);
            }
            ReportItem::Progress {
                label,
                current,
                total,
            } => {
                let bar = progress_bar(*current, *total, 20);
                let _ = writeln!(out, "  {}: {}", label, bar);
            }
            ReportItem::Sparkline { label, values } => {
                let spark = sparkline(values);
                let _ = writeln!(out, "  {}: {}", label, spark);
            }
            ReportItem::TableRow { cells } => {
                let _ = writeln!(out, "  {}", cells.join(""));
            }
            ReportItem::Text(text) => {
                let _ = writeln!(out, "  {}", text);
            }
            ReportItem::Metric {
                name,
                value,
                unit,
                grade,
            } => {
                let icon = if grade.is_passing() {
                    chars::CHECK
                } else {
                    chars::CROSS_MARK
                };
                let _ = writeln!(out, "  {} {} {:.1}{} ({})", icon, name, value, unit, grade);
            }
        }
    }

    fn draw_footer(&self, out: &mut String) {
        out.push('\n');
        for _ in 0..self.width {
            out.push(chars::HORIZONTAL);
        }
        out.push('\n');
        let _ = writeln!(out, "Generated by bashrs v{}", env!("CARGO_PKG_VERSION"));
    }
}

/// Format duration in human-readable form
pub fn format_duration(d: Duration) -> String {
    let secs = d.as_secs();
    let millis = d.subsec_millis();

    if secs >= 3600 {
        format!("{}h {}m", secs / 3600, (secs % 3600) / 60)
    } else if secs >= 60 {
        format!("{}m {}s", secs / 60, secs % 60)
    } else if secs > 0 {
        format!("{}.{}s", secs, millis / 100)
    } else {
        format!("{}ms", millis)
    }
}

/// Create a quality gate report
pub fn gate_report(
    tier: u8,
    gates_passed: usize,
    gates_total: usize,
    duration: Duration,
    details: &[(String, bool, Duration)],
) -> String {
    let mut builder = ReportBuilder::new(&format!("Tier {} Quality Gate Report", tier))
        .width(60)
        .section("Summary");

    let pct = if gates_total > 0 {
        (gates_passed as f64 / gates_total as f64) * 100.0
    } else {
        100.0
    };

    builder = builder
        .metric("Pass Rate", pct, "%")
        .kv("Gates Passed", &format!("{}/{}", gates_passed, gates_total))
        .kv("Total Duration", &format_duration(duration));

    builder = builder.section("Gate Results");

    for (name, passed, dur) in details {
        builder = builder.status(*passed, &format!("{} ({})", name, format_duration(*dur)));
    }

    builder.build().render()
}

#[cfg(test)]
#[path = "report_tests_ml_011.rs"]
mod tests_extracted;