use super::languages::get_language_analyzer;
use super::types::{FunctionInfo, StructureInfo};
use crate::utils::errors::Result;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::Path;
#[derive(Debug, Clone, Default)]
pub struct FileAnalysis {
pub functions: Vec<FunctionInfo>,
pub structures: Vec<StructureInfo>,
}
pub struct CodeAnalyzer;
impl CodeAnalyzer {
pub fn new() -> Self {
Self
}
pub fn analyze_file(&self, file_path: &str) -> Result<FileAnalysis> {
let extension = Path::new(file_path)
.extension()
.and_then(|ext| ext.to_str())
.unwrap_or("unknown")
.to_lowercase();
let Some(analyzer) = get_language_analyzer(&extension) else {
return Ok(FileAnalysis::default());
};
let reader = BufReader::new(fs::File::open(file_path)?);
let lines: Vec<String> = reader
.lines()
.collect::<std::io::Result<Vec<_>>>()
.unwrap_or_default();
Ok(FileAnalysis {
functions: analyzer.analyze_functions(&lines)?,
structures: analyzer.analyze_structures(&lines)?,
})
}
}
impl Default for CodeAnalyzer {
fn default() -> Self {
Self::new()
}
}