diffguard-core 0.2.0

Core engine for diffguard governance linter
Documentation
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
//! CSV and TSV output renderers.
//!
//! Converts CheckReceipt to CSV or TSV format for spreadsheet/data analysis.
//! CSV follows RFC 4180 for proper escaping.

use diffguard_types::{CheckReceipt, Finding};

/// CSV header row.
const CSV_HEADER: &str = "file,line,rule_id,severity,message,snippet";

/// Renders a CheckReceipt as a CSV report (RFC 4180 compliant).
///
/// Columns: file, line, rule_id, severity, message, snippet
pub fn render_csv_for_receipt(receipt: &CheckReceipt) -> String {
    let mut out = String::new();

    // Header row
    out.push_str(CSV_HEADER);
    out.push('\n');

    // Data rows
    for f in &receipt.findings {
        out.push_str(&render_csv_row(f));
    }

    out
}

/// Renders a CheckReceipt as a TSV report.
///
/// Columns: file, line, rule_id, severity, message, snippet
pub fn render_tsv_for_receipt(receipt: &CheckReceipt) -> String {
    let mut out = String::new();

    // Header row
    out.push_str(&CSV_HEADER.replace(',', "\t"));
    out.push('\n');

    // Data rows
    for f in &receipt.findings {
        out.push_str(&render_tsv_row(f));
    }

    out
}

/// Renders a single finding as a CSV row.
fn render_csv_row(f: &Finding) -> String {
    format!(
        "{},{},{},{},{},{}\n",
        escape_csv_field(&f.path),
        f.line,
        escape_csv_field(&f.rule_id),
        f.severity.as_str(),
        escape_csv_field(&f.message),
        escape_csv_field(&f.snippet)
    )
}

/// Renders a single finding as a TSV row.
fn render_tsv_row(f: &Finding) -> String {
    format!(
        "{}\t{}\t{}\t{}\t{}\t{}\n",
        escape_tsv_field(&f.path),
        f.line,
        escape_tsv_field(&f.rule_id),
        f.severity.as_str(),
        escape_tsv_field(&f.message),
        escape_tsv_field(&f.snippet)
    )
}

/// Escapes a field for CSV according to RFC 4180.
///
/// Fields containing commas, double quotes, or newlines are quoted.
/// Double quotes within the field are escaped by doubling them.
fn escape_csv_field(s: &str) -> String {
    let needs_quoting = s.contains(',') || s.contains('"') || s.contains('\n') || s.contains('\r');

    if needs_quoting {
        let escaped = s.replace('"', "\"\"");
        format!("\"{}\"", escaped)
    } else {
        s.to_string()
    }
}

/// Escapes a field for TSV.
///
/// Tabs and newlines are escaped with backslash notation.
fn escape_tsv_field(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('\t', "\\t")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
}

#[cfg(test)]
mod tests {
    use super::*;
    use diffguard_types::{
        CHECK_SCHEMA_V1, CheckReceipt, DiffMeta, Finding, Scope, Severity, ToolMeta, Verdict,
        VerdictCounts, VerdictStatus,
    };

    fn create_test_receipt_with_findings() -> CheckReceipt {
        CheckReceipt {
            schema: CHECK_SCHEMA_V1.to_string(),
            tool: ToolMeta {
                name: "diffguard".to_string(),
                version: "0.1.0".to_string(),
            },
            diff: DiffMeta {
                base: "origin/main".to_string(),
                head: "HEAD".to_string(),
                context_lines: 0,
                scope: Scope::Added,
                files_scanned: 3,
                lines_scanned: 42,
            },
            findings: vec![
                Finding {
                    rule_id: "rust.no_unwrap".to_string(),
                    severity: Severity::Error,
                    message: "Avoid unwrap/expect in production code.".to_string(),
                    path: "src/lib.rs".to_string(),
                    line: 15,
                    column: Some(10),
                    match_text: ".unwrap()".to_string(),
                    snippet: "let value = result.unwrap();".to_string(),
                },
                Finding {
                    rule_id: "rust.no_dbg".to_string(),
                    severity: Severity::Warn,
                    message: "Remove dbg!/println! before merging.".to_string(),
                    path: "src/main.rs".to_string(),
                    line: 23,
                    column: Some(5),
                    match_text: "dbg!".to_string(),
                    snippet: "    dbg!(config);".to_string(),
                },
            ],
            verdict: Verdict {
                status: VerdictStatus::Fail,
                counts: VerdictCounts {
                    info: 0,
                    warn: 1,
                    error: 1,
                    ..Default::default()
                },
                reasons: vec![
                    "1 error-level finding".to_string(),
                    "1 warning-level finding".to_string(),
                ],
            },
            timing: None,
        }
    }

    fn create_test_receipt_empty() -> CheckReceipt {
        CheckReceipt {
            schema: CHECK_SCHEMA_V1.to_string(),
            tool: ToolMeta {
                name: "diffguard".to_string(),
                version: "0.1.0".to_string(),
            },
            diff: DiffMeta {
                base: "origin/main".to_string(),
                head: "HEAD".to_string(),
                context_lines: 0,
                scope: Scope::Added,
                files_scanned: 5,
                lines_scanned: 120,
            },
            findings: vec![],
            verdict: Verdict {
                status: VerdictStatus::Pass,
                counts: VerdictCounts::default(),
                reasons: vec![],
            },
            timing: None,
        }
    }

    fn create_test_receipt_with_special_chars() -> CheckReceipt {
        CheckReceipt {
            schema: CHECK_SCHEMA_V1.to_string(),
            tool: ToolMeta {
                name: "diffguard".to_string(),
                version: "0.1.0".to_string(),
            },
            diff: DiffMeta {
                base: "origin/main".to_string(),
                head: "HEAD".to_string(),
                context_lines: 0,
                scope: Scope::Added,
                files_scanned: 1,
                lines_scanned: 10,
            },
            findings: vec![Finding {
                rule_id: "test.rule".to_string(),
                severity: Severity::Warn,
                message: "Message with \"quotes\" and, commas".to_string(),
                path: "src/file.rs".to_string(),
                line: 5,
                column: None,
                match_text: "test".to_string(),
                snippet: "let s = \"hello\nworld\";".to_string(),
            }],
            verdict: Verdict {
                status: VerdictStatus::Warn,
                counts: VerdictCounts {
                    info: 0,
                    warn: 1,
                    error: 0,
                    ..Default::default()
                },
                reasons: vec!["1 warning".to_string()],
            },
            timing: None,
        }
    }

    // ==================== CSV Tests ====================

    #[test]
    fn csv_has_header_row() {
        let receipt = create_test_receipt_empty();
        let csv = render_csv_for_receipt(&receipt);
        assert!(csv.starts_with("file,line,rule_id,severity,message,snippet\n"));
    }

    #[test]
    fn csv_has_correct_row_count() {
        let receipt = create_test_receipt_with_findings();
        let csv = render_csv_for_receipt(&receipt);
        let lines: Vec<&str> = csv.lines().collect();
        // 1 header + 2 data rows
        assert_eq!(lines.len(), 3);
    }

    #[test]
    fn csv_empty_receipt_has_header_only() {
        let receipt = create_test_receipt_empty();
        let csv = render_csv_for_receipt(&receipt);
        let lines: Vec<&str> = csv.lines().collect();
        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0], "file,line,rule_id,severity,message,snippet");
    }

    #[test]
    fn csv_escapes_quotes() {
        let receipt = create_test_receipt_with_special_chars();
        let csv = render_csv_for_receipt(&receipt);
        // Quotes in message should be escaped as ""
        assert!(csv.contains("\"\"quotes\"\""));
    }

    #[test]
    fn csv_escapes_commas() {
        let receipt = create_test_receipt_with_special_chars();
        let csv = render_csv_for_receipt(&receipt);
        // Field with comma should be quoted
        assert!(csv.contains("\"Message with"));
    }

    #[test]
    fn csv_escapes_newlines() {
        let receipt = create_test_receipt_with_special_chars();
        let csv = render_csv_for_receipt(&receipt);
        // Snippet with newline should be quoted
        assert!(csv.contains("\"let s = \"\"hello"));
    }

    // ==================== TSV Tests ====================

    #[test]
    fn tsv_has_header_row() {
        let receipt = create_test_receipt_empty();
        let tsv = render_tsv_for_receipt(&receipt);
        assert!(tsv.starts_with("file\tline\trule_id\tseverity\tmessage\tsnippet\n"));
    }

    #[test]
    fn tsv_has_correct_row_count() {
        let receipt = create_test_receipt_with_findings();
        let tsv = render_tsv_for_receipt(&receipt);
        let lines: Vec<&str> = tsv.lines().collect();
        // 1 header + 2 data rows
        assert_eq!(lines.len(), 3);
    }

    #[test]
    fn tsv_empty_receipt_has_header_only() {
        let receipt = create_test_receipt_empty();
        let tsv = render_tsv_for_receipt(&receipt);
        let lines: Vec<&str> = tsv.lines().collect();
        assert_eq!(lines.len(), 1);
    }

    #[test]
    fn tsv_escapes_tabs() {
        let mut receipt = create_test_receipt_with_findings();
        receipt.findings[0].snippet = "let\tx = 1;".to_string();
        let tsv = render_tsv_for_receipt(&receipt);
        // Tab in snippet should be escaped
        assert!(tsv.contains("let\\tx = 1;"));
    }

    #[test]
    fn tsv_escapes_newlines() {
        let receipt = create_test_receipt_with_special_chars();
        let tsv = render_tsv_for_receipt(&receipt);
        // Newline in snippet should be escaped as \n
        assert!(tsv.contains("hello\\nworld"));
    }

    // ==================== Field Escaping Tests ====================

    #[test]
    fn escape_csv_field_plain_text() {
        assert_eq!(escape_csv_field("plain text"), "plain text");
    }

    #[test]
    fn escape_csv_field_with_comma() {
        assert_eq!(escape_csv_field("a,b"), "\"a,b\"");
    }

    #[test]
    fn escape_csv_field_with_quote() {
        assert_eq!(escape_csv_field("say \"hello\""), "\"say \"\"hello\"\"\"");
    }

    #[test]
    fn escape_csv_field_with_newline() {
        assert_eq!(escape_csv_field("line1\nline2"), "\"line1\nline2\"");
    }

    #[test]
    fn escape_csv_field_with_carriage_return() {
        assert_eq!(escape_csv_field("line1\rline2"), "\"line1\rline2\"");
    }

    #[test]
    fn escape_tsv_field_plain_text() {
        assert_eq!(escape_tsv_field("plain text"), "plain text");
    }

    #[test]
    fn escape_tsv_field_with_tab() {
        assert_eq!(escape_tsv_field("a\tb"), "a\\tb");
    }

    #[test]
    fn escape_tsv_field_with_newline() {
        assert_eq!(escape_tsv_field("a\nb"), "a\\nb");
    }

    #[test]
    fn escape_tsv_field_with_carriage_return() {
        assert_eq!(escape_tsv_field("a\rb"), "a\\rb");
    }

    #[test]
    fn escape_tsv_field_with_backslash() {
        assert_eq!(escape_tsv_field("a\\b"), "a\\\\b");
    }

    // ==================== Snapshot Tests ====================

    #[test]
    fn snapshot_csv_with_findings() {
        let receipt = create_test_receipt_with_findings();
        let csv = render_csv_for_receipt(&receipt);
        insta::assert_snapshot!(csv);
    }

    #[test]
    fn snapshot_csv_no_findings() {
        let receipt = create_test_receipt_empty();
        let csv = render_csv_for_receipt(&receipt);
        insta::assert_snapshot!(csv);
    }

    #[test]
    fn snapshot_tsv_with_findings() {
        let receipt = create_test_receipt_with_findings();
        let tsv = render_tsv_for_receipt(&receipt);
        insta::assert_snapshot!(tsv);
    }

    #[test]
    fn snapshot_tsv_no_findings() {
        let receipt = create_test_receipt_empty();
        let tsv = render_tsv_for_receipt(&receipt);
        insta::assert_snapshot!(tsv);
    }

    #[test]
    fn snapshot_csv_special_chars() {
        let receipt = create_test_receipt_with_special_chars();
        let csv = render_csv_for_receipt(&receipt);
        insta::assert_snapshot!(csv);
    }

    #[test]
    fn snapshot_tsv_special_chars() {
        let receipt = create_test_receipt_with_special_chars();
        let tsv = render_tsv_for_receipt(&receipt);
        insta::assert_snapshot!(tsv);
    }
}