Skip to main content

normalize_rules/
loader.rs

1//! Diagnostic formatting helpers for fact rules.
2//!
3//! The dylib rule pack loader has been removed. Rules run as interpreted `.dl` files
4//! via `normalize-facts-rules-interpret`; there is no dynamic library loading.
5
6use normalize_facts_rules_api::Diagnostic;
7
8/// Format a diagnostic for display
9pub fn format_diagnostic(diag: &Diagnostic, use_colors: bool) -> String {
10    use normalize_facts_rules_api::DiagnosticLevel;
11
12    let level_str = match diag.level {
13        DiagnosticLevel::Hint => {
14            if use_colors {
15                "\x1b[36mhint\x1b[0m"
16            } else {
17                "hint"
18            }
19        }
20        DiagnosticLevel::Warning => {
21            if use_colors {
22                "\x1b[33mwarning\x1b[0m"
23            } else {
24                "warning"
25            }
26        }
27        DiagnosticLevel::Error => {
28            if use_colors {
29                "\x1b[31merror\x1b[0m"
30            } else {
31                "error"
32            }
33        }
34    };
35
36    let mut out = String::new();
37
38    // Location
39    if let Some(ref loc) = diag.location {
40        out.push_str(&format!("{}:{}: ", loc.file, loc.line));
41    }
42
43    // Level and rule
44    out.push_str(&format!("{} [{}]: ", level_str, diag.rule_id));
45
46    // Message
47    out.push_str(&diag.message);
48
49    // Related locations
50    for related in diag.related.iter() {
51        out.push_str(&format!("\n  --> {}:{}", related.file, related.line));
52    }
53
54    // Suggestion
55    if let Some(ref suggestion) = diag.suggestion {
56        out.push_str(&format!("\n  suggestion: {}", suggestion));
57    }
58
59    out
60}