pub mod parser;
pub mod rust;
pub mod fallback;
pub mod ruby;
pub mod golang;
pub mod javascript;
pub mod languages;
pub mod markdown;
pub mod python;
pub mod typescript;
pub mod yaml;
use anyhow::{Result};
use std::path::Path;
use self::parser::SyntaxParser;
use crate::syntax::fallback::FallbackParser;
use crate::syntax::rust::RustParser;
use crate::syntax::ruby::RubyParser;
use crate::syntax::golang::GolangParser;
use crate::syntax::python::PythonParser;
pub use self::parser::CodeChunk;
#[derive(Debug, Clone, PartialEq)]
pub struct AnalysisResult {
}
pub fn analyze_file(file_path: &Path) -> Result<AnalysisResult> {
let extension = file_path
.extension()
.and_then(|ext| ext.to_str())
.map(|s| s.to_lowercase()) .unwrap_or_default();
let _code = std::fs::read_to_string(file_path)?;
let _parser: Box<dyn SyntaxParser> = match extension.as_str() { "rs" => Box::new(RustParser::new()),
"rb" => Box::new(RubyParser::new()),
"go" => Box::new(GolangParser::new()),
"js" | "jsx" => Box::new(crate::syntax::javascript::JavaScriptParser::new()),
"ts" | "tsx" => Box::new(crate::syntax::typescript::TypeScriptParser::new()),
"yaml" | "yml" => Box::new(crate::syntax::yaml::YamlParser::new()),
"md" | "mdx" => Box::new(crate::syntax::markdown::MarkdownParser::new()),
"py" => Box::new(PythonParser::new()),
_ => Box::new(FallbackParser::new()),
};
Ok(AnalysisResult {})
}
pub fn get_chunks(file_path: &Path) -> Result<Vec<CodeChunk>> {
let extension = file_path
.extension()
.and_then(|ext| ext.to_str())
.map(|s| s.to_lowercase()) .unwrap_or_default();
let code = std::fs::read_to_string(file_path)?;
let mut parser: Box<dyn SyntaxParser> = match extension.as_str() { "rs" => Box::new(RustParser::new()),
"rb" => Box::new(RubyParser::new()),
"go" => Box::new(GolangParser::new()),
"js" | "jsx" => Box::new(crate::syntax::javascript::JavaScriptParser::new()),
"ts" | "tsx" => Box::new(crate::syntax::typescript::TypeScriptParser::new()),
"yaml" | "yml" => Box::new(crate::syntax::yaml::YamlParser::new()),
"md" | "mdx" => Box::new(crate::syntax::markdown::MarkdownParser::new()),
"py" => Box::new(PythonParser::new()),
_ => Box::new(FallbackParser::new()),
};
parser.parse(&code, file_path.to_str().unwrap_or(""))
}
#[cfg(test)]
mod rust_tests;
#[cfg(test)]
mod ruby_tests;
#[cfg(test)]
mod golang_tests;
#[cfg(test)]
mod javascript_tests;
#[cfg(test)]
mod typescript_tests;
#[cfg(test)]
mod yaml_tests;
#[cfg(test)]
mod markdown_tests;
#[cfg(test)]
mod python_tests; #[cfg(test)]
mod fallback_tests; #[cfg(test)]
mod languages_tests;
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn test_analyze_file_basic() -> Result<()> {
let dir = tempdir()?;
let file_path = dir.path().join("test.rs");
let mut file = File::create(&file_path)?;
writeln!(file, "fn main() {{}}")?;
let result = analyze_file(&file_path)?;
assert_eq!(result, AnalysisResult {});
Ok(())
}
#[test]
fn test_analyze_file_unknown_extension() -> Result<()> {
let dir = tempdir()?;
let file_path = dir.path().join("test.unknown");
let mut file = File::create(&file_path)?;
writeln!(file, "Some content")?;
let result = analyze_file(&file_path)?;
assert_eq!(result, AnalysisResult {});
Ok(())
}
#[test]
fn test_get_chunks_for_multiple_languages() -> Result<()> {
let dir = tempdir()?;
let rust_path = dir.path().join("test.rs");
let mut rust_file = File::create(&rust_path)?;
writeln!(rust_file, "fn main() {{\n println!(\"Hello\");\n}}")?;
let rust_chunks = get_chunks(&rust_path)?;
assert!(!rust_chunks.is_empty());
let py_path = dir.path().join("test.py");
let mut py_file = File::create(&py_path)?;
writeln!(py_file, "def main():\n print('Hello')")?;
let py_chunks = get_chunks(&py_path)?;
assert!(!py_chunks.is_empty());
let unknown_path = dir.path().join("test.xyz");
let mut unknown_file = File::create(&unknown_path)?;
writeln!(unknown_file, "Some random content")?;
let unknown_chunks = get_chunks(&unknown_path)?;
assert!(!unknown_chunks.is_empty());
Ok(())
}
}