domainforge-core 0.21.2

Rust core library implementing the SEA DSL primitives and validation engine.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
/// Diagnostic formatters for ValidationError
///
/// Provides multiple output formats for error diagnostics:
/// - JSON: Machine-readable format for CI/CD tools
/// - Human: Color-coded format with source snippets for developers
/// - LSP: Language Server Protocol compatible format for IDEs
use crate::validation_error::{SourceRange, ValidationError};
use serde::{Deserialize, Serialize};

/// Trait for formatting validation errors
pub trait DiagnosticFormatter {
    /// Format a validation error with optional source code
    fn format(&self, error: &ValidationError, source: Option<&str>) -> String;
}

/// JSON diagnostic format for machine parsing (CI/CD tools)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonDiagnostic {
    pub code: String,
    pub severity: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<JsonRange>,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hint: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRange {
    pub start: JsonPosition,
    pub end: JsonPosition,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonPosition {
    pub line: usize,
    pub column: usize,
}

impl From<SourceRange> for JsonRange {
    fn from(range: SourceRange) -> Self {
        JsonRange {
            start: JsonPosition {
                line: range.start.line,
                column: range.start.column,
            },
            end: JsonPosition {
                line: range.end.line,
                column: range.end.column,
            },
        }
    }
}

impl JsonDiagnostic {
    pub fn from_validation_error(error: &ValidationError) -> Self {
        let code = error.error_code();
        let severity = Self::determine_severity(error);
        let range = error.range().map(JsonRange::from);
        let message = error.to_string();
        let hint = Self::extract_hint(error);

        JsonDiagnostic {
            code: code.as_str().to_string(),
            severity,
            range,
            message,
            hint,
        }
    }

    fn determine_severity(error: &ValidationError) -> String {
        match error {
            ValidationError::SyntaxError { .. }
            | ValidationError::TypeError { .. }
            | ValidationError::UnitError { .. }
            | ValidationError::UndefinedReference { .. }
            | ValidationError::DuplicateDeclaration { .. } => "error".to_string(),
            ValidationError::DeterminismError { .. } => "warning".to_string(),
            ValidationError::ScopeError { .. } | ValidationError::InvalidExpression { .. } => {
                "error".to_string()
            }
        }
    }

    fn extract_hint(error: &ValidationError) -> Option<String> {
        match error {
            ValidationError::TypeError { suggestion, .. }
            | ValidationError::UnitError { suggestion, .. }
            | ValidationError::ScopeError { suggestion, .. }
            | ValidationError::UndefinedReference { suggestion, .. }
            | ValidationError::InvalidExpression { suggestion, .. } => suggestion.clone(),
            ValidationError::DeterminismError { hint, .. } => Some(hint.clone()),
            _ => None,
        }
    }
}

/// JSON formatter implementation
pub struct JsonFormatter;

impl DiagnosticFormatter for JsonFormatter {
    fn format(&self, error: &ValidationError, _source: Option<&str>) -> String {
        let diagnostic = JsonDiagnostic::from_validation_error(error);
        serde_json::to_string_pretty(&diagnostic).unwrap_or_else(|_| {
            // Fallback: create a minimal JSON object manually but safely
            let fallback = serde_json::json!({
                "code": error.error_code().as_str(),
                "severity": "error",
                "message": error.to_string()
            });
            serde_json::to_string(&fallback).unwrap_or_else(|_| {
                r#"{"code": "UNKNOWN", "severity": "error", "message": "Failed to serialize error"}"#.to_string()
            })
        })
    }
}

/// Format multiple errors as a JSON array
pub fn format_errors_json(errors: &[ValidationError]) -> String {
    let diagnostics: Vec<JsonDiagnostic> = errors
        .iter()
        .map(JsonDiagnostic::from_validation_error)
        .collect();

    serde_json::to_string_pretty(&diagnostics).unwrap_or_else(|_| "[]".to_string())
}

/// Human-readable diagnostic formatter with color support
pub struct HumanFormatter {
    pub use_color: bool,
    pub show_source: bool,
}

impl Default for HumanFormatter {
    fn default() -> Self {
        Self {
            use_color: true,
            show_source: true,
        }
    }
}

impl HumanFormatter {
    pub fn new(use_color: bool, show_source: bool) -> Self {
        Self {
            use_color,
            show_source,
        }
    }

    fn colorize(&self, text: &str, color: &str) -> String {
        if !self.use_color {
            return text.to_string();
        }

        let color_code = match color {
            "red" => "\x1b[31m",
            "yellow" => "\x1b[33m",
            "blue" => "\x1b[34m",
            "cyan" => "\x1b[36m",
            "bold" => "\x1b[1m",
            _ => "",
        };

        format!("{}{}\x1b[0m", color_code, text)
    }

    fn format_source_snippet(&self, source: &str, range: SourceRange) -> String {
        let lines: Vec<&str> = source.lines().collect();
        let start_line = range.start.line.saturating_sub(1); // Convert to 0-indexed
        let end_line = range.end.line.saturating_sub(1);

        if start_line >= lines.len() {
            return String::new();
        }

        let mut output = String::new();
        let line_num_width = (end_line + 1).to_string().len();

        // Show one line before if available
        if start_line > 0 {
            output.push_str(&format!(
                "{:>width$} | {}\n",
                start_line,
                lines[start_line - 1],
                width = line_num_width
            ));
        }

        let last_line = end_line.min(lines.len().saturating_sub(1));
        if last_line < start_line {
            return output;
        }

        for (line_idx, line) in lines
            .iter()
            .enumerate()
            .skip(start_line)
            .take(last_line.saturating_sub(start_line) + 1)
        {
            let line_num = line_idx + 1;
            output.push_str(&format!(
                "{} | {}\n",
                self.colorize(
                    &format!("{:>width$}", line_num, width = line_num_width),
                    "blue"
                ),
                line
            ));

            // Add caret indicators for the error range
            if line_idx == start_line {
                let padding = " ".repeat(line_num_width + 3);
                let start_col = range.start.column.saturating_sub(1);
                let end_col = if line_idx == end_line {
                    range.end.column.saturating_sub(1)
                } else {
                    line.len()
                };

                let caret_padding = " ".repeat(start_col);
                let carets = "^".repeat(end_col.saturating_sub(start_col).max(1));
                output.push_str(&format!(
                    "{}{}{}\n",
                    padding,
                    caret_padding,
                    self.colorize(&carets, "red")
                ));
            }
        }

        // Show one line after if available
        if end_line + 1 < lines.len() {
            output.push_str(&format!(
                "{:>width$} | {}\n",
                end_line + 2,
                lines[end_line + 1],
                width = line_num_width
            ));
        }

        output
    }
}

impl DiagnosticFormatter for HumanFormatter {
    fn format(&self, error: &ValidationError, source: Option<&str>) -> String {
        let code = error.error_code();
        let severity = match error {
            ValidationError::DeterminismError { .. } => "warning",
            _ => "error",
        };

        let severity_colored = match severity {
            "error" => self.colorize("error", "red"),
            "warning" => self.colorize("warning", "yellow"),
            _ => severity.to_string(),
        };

        let mut output = format!(
            "{}[{}]: {}\n",
            severity_colored,
            self.colorize(code.as_str(), "bold"),
            error
        );

        // Add source snippet if available
        if self.show_source {
            if let (Some(src), Some(range)) = (source, error.range()) {
                output.push_str(&format!("  {} {}\n", self.colorize("-->", "blue"), range));
                output.push_str(&self.format_source_snippet(src, range));
            } else if let Some(location) = error.location_string() {
                output.push_str(&format!(
                    "  {} {}\n",
                    self.colorize("-->", "blue"),
                    location
                ));
            }
        }

        // Add hint if available
        if let Some(hint) = JsonDiagnostic::extract_hint(error) {
            output.push_str(&format!("  {} {}\n", self.colorize("hint:", "cyan"), hint));
        }

        output
    }
}

/// LSP (Language Server Protocol) compatible formatter
pub struct LspFormatter;

impl DiagnosticFormatter for LspFormatter {
    fn format(&self, error: &ValidationError, _source: Option<&str>) -> String {
        let code = error.error_code();
        let severity = match error {
            ValidationError::DeterminismError { .. } => 2, // Warning
            _ => 1,                                        // Error
        };

        let range = error.range();
        let message = error.to_string();
        let hint = JsonDiagnostic::extract_hint(error);

        let full_message = if let Some(h) = hint {
            format!("{}\n\n{}", message, h)
        } else {
            message
        };

        let range_value = if let Some(r) = range {
            serde_json::json!({
                "start": {
                    "line": r.start.line.saturating_sub(1),
                    "character": r.start.column.saturating_sub(1)
                },
                "end": {
                    "line": r.end.line.saturating_sub(1),
                    "character": r.end.column.saturating_sub(1)
                }
            })
        } else {
            serde_json::json!({
                "start": { "line": 0, "character": 0 },
                "end": { "line": 0, "character": 0 }
            })
        };

        let diagnostic = serde_json::json!({
            "range": range_value,
            "severity": severity,
            "code": code.as_str(),
            "source": "domainforge",
            "message": full_message
        });

        serde_json::to_string(&diagnostic).unwrap_or_else(|_| {
            r#"{"severity": 1, "message": "Failed to serialize diagnostic"}"#.to_string()
        })
    }
}

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

    #[test]
    fn test_json_formatter() {
        let error = ValidationError::syntax_error("unexpected token", 10, 5);
        let formatter = JsonFormatter;
        let output = formatter.format(&error, None);

        assert!(output.contains("E005"));
        assert!(output.contains("error"));
        assert!(output.contains("unexpected token"));
    }

    #[test]
    fn test_json_formatter_with_range() {
        let error = ValidationError::syntax_error_with_range("test", 10, 5, 10, 15);
        let formatter = JsonFormatter;
        let output = formatter.format(&error, None);

        assert!(output.contains(r#""line": 10"#));
        assert!(output.contains(r#""column": 5"#));
    }

    #[test]
    fn test_json_formatter_with_hint() {
        let candidates = vec!["Warehouse".to_string()];
        let error =
            ValidationError::undefined_entity_with_candidates("Warehous", "line 10", &candidates);
        let formatter = JsonFormatter;
        let output = formatter.format(&error, None);

        assert!(output.contains("E001"));
        assert!(output.contains("Warehouse"));
    }

    #[test]
    fn test_human_formatter_no_color() {
        let error = ValidationError::syntax_error("test error", 1, 1);
        let formatter = HumanFormatter::new(false, false);
        let output = formatter.format(&error, None);

        assert!(output.contains("error[E005]"));
        assert!(output.contains("test error"));
        assert!(!output.contains("\x1b[")); // No ANSI codes
    }

    #[test]
    fn test_human_formatter_with_color() {
        let error = ValidationError::syntax_error("test error", 1, 1);
        let formatter = HumanFormatter::new(true, false);
        let output = formatter.format(&error, None);

        assert!(output.contains("\x1b[")); // Has ANSI codes
    }

    #[test]
    fn test_lsp_formatter() {
        let error = ValidationError::syntax_error_with_range("test", 10, 5, 10, 15);
        let formatter = LspFormatter;
        let output = formatter.format(&error, None);

        let json: serde_json::Value = serde_json::from_str(&output).expect("should be valid json");
        assert_eq!(json["severity"], 1);
        assert_eq!(json["code"], "E005");
        assert_eq!(json["source"], "domainforge");
        // LSP uses 0-indexed lines
        assert_eq!(json["range"]["start"]["line"], 9);
    }

    #[test]
    fn test_format_multiple_errors() {
        let errors = vec![
            ValidationError::syntax_error("error 1", 1, 1),
            ValidationError::undefined_entity("Test", "line 5"),
        ];

        let output = format_errors_json(&errors);
        assert!(output.contains("E005"));
        assert!(output.contains("E001"));
    }
}