1use qanglang_core::{ErrorMessageFormat, GlobalCompilerPipeline};
2
3use crate::test_file::SourceFile;
4
5#[derive(Debug, Clone)]
6pub struct CheckResult {
7 pub display_path: String,
8 pub passed: bool,
9 pub error_count: usize,
10 pub errors: Vec<String>,
11}
12
13impl CheckResult {
14 pub fn success(display_path: String) -> Self {
15 Self {
16 display_path,
17 passed: true,
18 error_count: 0,
19 errors: Vec::new(),
20 }
21 }
22
23 pub fn failure(display_path: String, errors: Vec<String>) -> Self {
24 let error_count = errors.len();
25 Self {
26 display_path,
27 passed: false,
28 error_count,
29 errors,
30 }
31 }
32}
33
34pub fn check_files_from_sources(
35 source_files: Vec<SourceFile>,
36 error_format: ErrorMessageFormat,
37) -> Vec<CheckResult> {
38 let file_paths: Vec<_> = source_files.iter().map(|sf| sf.file_path.clone()).collect();
41
42 let mut pipeline = GlobalCompilerPipeline::new();
43 let mut allocator = qanglang_core::HeapAllocator::new();
44
45 match pipeline.process_files(file_paths, &mut allocator) {
47 Ok(_) => {
48 source_files
50 .into_iter()
51 .map(|source_file| CheckResult::success(source_file.display_path))
52 .collect()
53 }
54 Err(_) => {
55 source_files
57 .into_iter()
58 .map(|source_file| check_single_file(source_file, error_format))
59 .collect()
60 }
61 }
62}
63
64pub fn check_single_file(
65 source_file: SourceFile,
66 error_message_format: ErrorMessageFormat,
67) -> CheckResult {
68 let mut pipeline = GlobalCompilerPipeline::new();
70 let mut allocator = qanglang_core::HeapAllocator::new();
71
72 match pipeline.process_files(vec![source_file.file_path], &mut allocator) {
74 Ok(_) => CheckResult::success(source_file.display_path),
75 Err(analysis_error) => {
76 let error_messages: Vec<String> = analysis_error
77 .into_errors()
78 .into_iter()
79 .map(|e| match error_message_format {
80 ErrorMessageFormat::Compact => e.message,
81 ErrorMessageFormat::Minimal => e.into_short_formatted().message,
82 ErrorMessageFormat::Verbose => e.into_formatted().message,
83 })
84 .collect();
85 CheckResult::failure(source_file.display_path, error_messages)
86 }
87 }
88}
89
90pub fn format_check_results(results: &[CheckResult]) -> String {
91 let mut output = String::new();
92
93 for result in results {
95 if result.passed {
96 output.push_str(&format!("✓ {}\n", result.display_path));
97 } else {
98 output.push_str(&format!(
99 "✗ {} ({} error{})\n",
100 result.display_path,
101 result.error_count,
102 if result.error_count == 1 { "" } else { "s" }
103 ));
104
105 for error in &result.errors {
107 output.push_str(&format!(" {}\n", error));
108 }
109 }
110 }
111
112 let total_files = results.len();
114 let failed_files = results.iter().filter(|r| !r.passed).count();
115 let passed_files = total_files - failed_files;
116 let total_errors: usize = results.iter().map(|r| r.error_count).sum();
117
118 output.push('\n');
119 if failed_files > 0 {
120 output.push_str(&format!(
121 "Failed: {} file{}\n",
122 failed_files,
123 if failed_files == 1 { "" } else { "s" }
124 ));
125 output.push_str(&format!("Total errors: {}\n", total_errors));
126 }
127 output.push_str(&format!(
128 "Passed: {} file{}\n",
129 passed_files,
130 if passed_files == 1 { "" } else { "s" }
131 ));
132 output.push_str(&format!(
133 "Total: {} file{}\n",
134 total_files,
135 if total_files == 1 { "" } else { "s" }
136 ));
137
138 output
139}