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
// CLI Logic Module - Extracted for testability
//
// This module contains pure functions that return structured results
// instead of printing directly. The commands.rs file acts as a thin shim
// that calls these functions and handles I/O.
//
// Architecture:
// - logic.rs: Hub that re-exports from focused submodules
// - logic_lint.rs: Lint processing, filtering, and rule handling
// - logic_dockerfile.rs: Dockerfile purification, analysis, build helpers
// - logic_format.rs: Formatting, display, and report generation
// - logic_shell.rs: Shell detection, normalization, file type detection
// - logic_validate.rs: Validation, verification, and checking
// - commands.rs: Thin I/O shim (reads files, calls logic, prints output)
//
// This separation enables:
// - Unit testing of all business logic
// - High test coverage (95%+ target)
// - Clear separation of concerns

// =============================================================================
// SUBMODULES
// =============================================================================

#[path = "logic_dockerfile.rs"]
mod dockerfile;
#[path = "logic_format.rs"]
mod format;
#[path = "logic_lint.rs"]
mod lint;
#[path = "logic_shell.rs"]
mod shell;
#[path = "logic_validate.rs"]
mod validate;

// =============================================================================
// RE-EXPORTS - All public items from submodules
// =============================================================================

// From logic_lint.rs
pub use lint::{
    build_ignored_rules, convert_lint_profile, determine_min_severity, diagnostic_matches_rules,
    filter_diagnostics, filter_diagnostics_by_rules, parse_rule_codes, parse_rule_filter,
    process_lint, process_purify_bash, severity_icon, FileType, LintDiagnostic, LintOptions,
    LintProcessResult, PurificationStats, PurifyProcessResult, Transformation,
};

// From logic_dockerfile.rs
pub use dockerfile::{
    add_no_install_recommends, add_package_manager_cleanup, convert_add_to_copy_if_local,
    dockerfile_find_cmd_line, dockerfile_has_user_directive, dockerfile_is_scratch,
    estimate_build_time_seconds, find_devcontainer_json, format_build_time,
    format_build_time_estimate, format_size_comparison, layer_has_slow_operation, parse_size_limit,
    parse_size_string, pin_base_image_version, purify_dockerfile_source, size_exceeds_limit,
    size_percentage_of_limit,
};

// From logic_format.rs
pub use format::{
    calculate_percentage, coverage_class, coverage_status, format_bytes_human,
    format_duration_human, format_purify_report_human, format_purify_report_json,
    format_purify_report_markdown, format_score_human, format_timestamp, generate_diff_lines,
    grade_interpretation, grade_symbol, hex_encode, score_status, test_pass_rate,
    test_result_status, truncate_str,
};

// From logic_shell.rs
pub use shell::{
    count_duplicate_path_entries, detect_platform, is_dockerfile, is_makefile,
    is_shell_script_file, is_stdio_path, normalize_shell_script, parse_shell_dialect,
    should_output_to_stdout,
};

// From logic_validate.rs
pub use validate::{
    extract_exit_code, process_check, validate_gate_tier, validate_proof_data, verify_scripts,
    CheckResult, GateResult, VerifyResult,
};

// =============================================================================
// TYPES THAT REMAIN IN THE HUB
// (Data-only types with no complex logic or cross-module dependencies)
// =============================================================================

/// Result of test processing
#[derive(Debug, Clone)]
pub struct TestProcessResult {
    pub passed: usize,
    pub failed: usize,
    pub skipped: usize,
    pub total: usize,
    pub test_results: Vec<TestCaseResult>,
    pub duration_ms: u64,
}

/// Result of a single test case
#[derive(Debug, Clone)]
pub struct TestCaseResult {
    pub name: String,
    pub passed: bool,
    pub message: Option<String>,
    pub duration_ms: u64,
}

impl TestProcessResult {
    pub fn success_rate(&self) -> f64 {
        if self.total == 0 {
            100.0
        } else {
            (self.passed as f64 / self.total as f64) * 100.0
        }
    }

    pub fn all_passed(&self) -> bool {
        self.failed == 0
    }
}

// =============================================================================
// SCORE COMMAND LOGIC
// =============================================================================

/// Result of score processing
#[derive(Debug, Clone)]
pub struct ScoreProcessResult {
    pub overall_score: f64,
    pub grade: Grade,
    pub dimensions: Vec<ScoreDimension>,
}

/// A dimension of the quality score
#[derive(Debug, Clone)]
pub struct ScoreDimension {
    pub name: String,
    pub score: f64,
    pub max_score: f64,
    pub status: &'static str,
}

// =============================================================================
// AUDIT COMMAND LOGIC
// =============================================================================

/// Result of audit processing
#[derive(Debug, Clone)]
pub struct AuditProcessResult {
    pub parse_success: bool,
    pub parse_error: Option<String>,
    pub lint_errors: usize,
    pub lint_warnings: usize,
    pub test_passed: usize,
    pub test_failed: usize,
    pub test_total: usize,
    pub score: Option<ScoreProcessResult>,
    pub overall_pass: bool,
    pub failure_reason: Option<String>,
}

impl AuditProcessResult {
    pub fn passed(&self) -> bool {
        self.overall_pass
    }
}

// =============================================================================
// COVERAGE COMMAND LOGIC
// =============================================================================

/// Result of coverage processing
#[derive(Debug, Clone)]
pub struct CoverageProcessResult {
    pub line_coverage: f64,
    pub function_coverage: f64,
    pub total_lines: usize,
    pub covered_lines: usize,
    pub total_functions: usize,
    pub covered_functions: usize,
    pub uncovered_lines: Vec<usize>,
    pub uncovered_functions: Vec<String>,
}

impl CoverageProcessResult {
    pub fn meets_threshold(&self, min_percent: f64) -> bool {
        self.line_coverage >= min_percent
    }
}

// =============================================================================
// FORMAT COMMAND LOGIC
// =============================================================================

/// Result of format processing
#[derive(Debug, Clone)]
pub struct FormatProcessResult {
    pub original: String,
    pub formatted: String,
    pub changed: bool,
    pub diff_lines: Vec<(usize, String, String)>,
}

/// Result of format check
#[derive(Debug, Clone)]
pub struct FormatCheckResult {
    pub files_checked: usize,
    pub files_formatted: usize,
    pub files_unchanged: usize,
}

impl FormatCheckResult {
    pub fn all_formatted(&self) -> bool {
        self.files_formatted == 0
    }
}

// =============================================================================
// GRADE LOGIC
// =============================================================================

/// Grade calculation based on score
/// Note: Higher grades (A) are "better" than lower grades (F)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Grade {
    A,
    B,
    C,
    D,
    F,
}

impl PartialOrd for Grade {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Grade {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // A is the best (highest), F is the worst (lowest)
        // So A > B > C > D > F
        let self_val = match self {
            Grade::A => 4,
            Grade::B => 3,
            Grade::C => 2,
            Grade::D => 1,
            Grade::F => 0,
        };
        let other_val = match other {
            Grade::A => 4,
            Grade::B => 3,
            Grade::C => 2,
            Grade::D => 1,
            Grade::F => 0,
        };
        self_val.cmp(&other_val)
    }
}

impl Grade {
    pub fn from_score(score: f64) -> Self {
        if score >= 90.0 {
            Grade::A
        } else if score >= 80.0 {
            Grade::B
        } else if score >= 70.0 {
            Grade::C
        } else if score >= 60.0 {
            Grade::D
        } else {
            Grade::F
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Grade::A => "A",
            Grade::B => "B",
            Grade::C => "C",
            Grade::D => "D",
            Grade::F => "F",
        }
    }

    pub fn meets_minimum(&self, min: &str) -> bool {
        let min_grade = match min.to_uppercase().as_str() {
            "A" => Grade::A,
            "B" => Grade::B,
            "C" => Grade::C,
            "D" => Grade::D,
            _ => Grade::F,
        };
        *self >= min_grade
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ===== TEST PROCESS RESULT TESTS =====

    #[test]
    fn test_test_process_result_success_rate() {
        let result = TestProcessResult {
            passed: 8,
            failed: 2,
            skipped: 0,
            total: 10,
            test_results: vec![],
            duration_ms: 100,
        };

        assert_eq!(result.success_rate(), 80.0);
        assert!(!result.all_passed());
    }

    #[test]
    fn test_test_process_result_all_passed() {
        let result = TestProcessResult {
            passed: 10,
            failed: 0,
            skipped: 0,
            total: 10,
            test_results: vec![],
            duration_ms: 100,
        };

        assert_eq!(result.success_rate(), 100.0);
        assert!(result.all_passed());
    }

    #[test]
    fn test_test_process_result_empty() {
        let result = TestProcessResult {
            passed: 0,
            failed: 0,
            skipped: 0,
            total: 0,
            test_results: vec![],
            duration_ms: 0,
        };

        assert_eq!(result.success_rate(), 100.0);
        assert!(result.all_passed());
    }

    // ===== COVERAGE PROCESS RESULT TESTS =====

    #[test]
    fn test_coverage_process_result_threshold() {
        let result = CoverageProcessResult {
            line_coverage: 85.0,
            function_coverage: 90.0,
            total_lines: 100,
            covered_lines: 85,
            total_functions: 10,
            covered_functions: 9,
            uncovered_lines: vec![1, 2, 3],
            uncovered_functions: vec!["foo".to_string()],
        };

        assert!(result.meets_threshold(80.0));
        assert!(result.meets_threshold(85.0));
        assert!(!result.meets_threshold(90.0));
    }

    // ===== AUDIT PROCESS RESULT TESTS =====

    #[test]
    fn test_audit_process_result_passed() {
        let result = AuditProcessResult {
            parse_success: true,
            parse_error: None,
            lint_errors: 0,
            lint_warnings: 2,
            test_passed: 10,
            test_failed: 0,
            test_total: 10,
            score: None,
            overall_pass: true,
            failure_reason: None,
        };

        assert!(result.passed());
    }

    #[test]
    fn test_audit_process_result_failed() {
        let result = AuditProcessResult {
            parse_success: true,
            parse_error: None,
            lint_errors: 5,
            lint_warnings: 2,
            test_passed: 8,
            test_failed: 2,
            test_total: 10,
            score: None,
            overall_pass: false,
            failure_reason: Some("Lint errors found".to_string()),
        };

        assert!(!result.passed());
    }

    // ===== GRADE TESTS =====

    #[test]
    fn test_grade_from_score() {
        assert_eq!(Grade::from_score(95.0), Grade::A);
        assert_eq!(Grade::from_score(90.0), Grade::A);
        assert_eq!(Grade::from_score(85.0), Grade::B);
        assert_eq!(Grade::from_score(80.0), Grade::B);
        assert_eq!(Grade::from_score(75.0), Grade::C);
        assert_eq!(Grade::from_score(65.0), Grade::D);
        assert_eq!(Grade::from_score(55.0), Grade::F);
    }

    #[test]
    fn test_grade_meets_minimum() {
        assert!(Grade::A.meets_minimum("A"));
        assert!(Grade::A.meets_minimum("B"));
        assert!(Grade::A.meets_minimum("C"));
        assert!(!Grade::B.meets_minimum("A"));
        assert!(Grade::B.meets_minimum("B"));
        assert!(Grade::C.meets_minimum("D"));
        assert!(!Grade::D.meets_minimum("C"));
    }

    // ===== FORMAT CHECK RESULT TESTS =====

    #[test]
    fn test_format_check_result_all_formatted() {
        let result = FormatCheckResult {
            files_checked: 5,
            files_formatted: 0,
            files_unchanged: 5,
        };
        assert!(result.all_formatted());
    }

    #[test]
    fn test_format_check_result_needs_formatting() {
        let result = FormatCheckResult {
            files_checked: 5,
            files_formatted: 2,
            files_unchanged: 3,
        };
        assert!(!result.all_formatted());
    }
}