use qanglang_core::{ErrorMessageFormat, GlobalCompilerPipeline};
use crate::test_file::SourceFile;
#[derive(Debug, Clone)]
pub struct CheckResult {
pub display_path: String,
pub passed: bool,
pub error_count: usize,
pub errors: Vec<String>,
}
impl CheckResult {
pub fn success(display_path: String) -> Self {
Self {
display_path,
passed: true,
error_count: 0,
errors: Vec::new(),
}
}
pub fn failure(display_path: String, errors: Vec<String>) -> Self {
let error_count = errors.len();
Self {
display_path,
passed: false,
error_count,
errors,
}
}
}
pub fn check_files_from_sources(
source_files: Vec<SourceFile>,
error_format: ErrorMessageFormat,
) -> Vec<CheckResult> {
let file_paths: Vec<_> = source_files.iter().map(|sf| sf.file_path.clone()).collect();
let mut pipeline = GlobalCompilerPipeline::new();
let mut allocator = qanglang_core::HeapAllocator::new();
match pipeline.process_files(file_paths, &mut allocator) {
Ok(_) => {
source_files
.into_iter()
.map(|source_file| CheckResult::success(source_file.display_path))
.collect()
}
Err(_) => {
source_files
.into_iter()
.map(|source_file| check_single_file(source_file, error_format))
.collect()
}
}
}
pub fn check_single_file(
source_file: SourceFile,
error_message_format: ErrorMessageFormat,
) -> CheckResult {
let mut pipeline = GlobalCompilerPipeline::new();
let mut allocator = qanglang_core::HeapAllocator::new();
match pipeline.process_files(vec![source_file.file_path], &mut allocator) {
Ok(_) => CheckResult::success(source_file.display_path),
Err(analysis_error) => {
let error_messages: Vec<String> = analysis_error
.into_errors()
.into_iter()
.map(|e| match error_message_format {
ErrorMessageFormat::Compact => e.message,
ErrorMessageFormat::Minimal => e.into_short_formatted().message,
ErrorMessageFormat::Verbose => e.into_formatted().message,
})
.collect();
CheckResult::failure(source_file.display_path, error_messages)
}
}
}
pub fn format_check_results(results: &[CheckResult]) -> String {
let mut output = String::new();
for result in results {
if result.passed {
output.push_str(&format!("✓ {}\n", result.display_path));
} else {
output.push_str(&format!(
"✗ {} ({} error{})\n",
result.display_path,
result.error_count,
if result.error_count == 1 { "" } else { "s" }
));
for error in &result.errors {
output.push_str(&format!(" {}\n", error));
}
}
}
let total_files = results.len();
let failed_files = results.iter().filter(|r| !r.passed).count();
let passed_files = total_files - failed_files;
let total_errors: usize = results.iter().map(|r| r.error_count).sum();
output.push('\n');
if failed_files > 0 {
output.push_str(&format!(
"Failed: {} file{}\n",
failed_files,
if failed_files == 1 { "" } else { "s" }
));
output.push_str(&format!("Total errors: {}\n", total_errors));
}
output.push_str(&format!(
"Passed: {} file{}\n",
passed_files,
if passed_files == 1 { "" } else { "s" }
));
output.push_str(&format!(
"Total: {} file{}\n",
total_files,
if total_files == 1 { "" } else { "s" }
));
output
}