use anyhow::Result;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use crate::skip_dirs::is_non_source_dir;
#[derive(Debug, Clone)]
pub struct ImportStatement {
pub file_path: String,
pub line_number: usize,
pub line_content: String,
pub imported_path: String,
}
#[derive(Debug, Clone)]
enum ImportPattern {
Rust,
JavaScript,
Python,
Go,
JavaLike,
CStyle,
Ruby,
Php,
Shell,
Css,
}
fn get_import_pattern(extension: &str) -> Option<ImportPattern> {
match extension {
"rs" => Some(ImportPattern::Rust),
"js" | "jsx" | "ts" | "tsx" | "mjs" | "cjs" => Some(ImportPattern::JavaScript),
"py" => Some(ImportPattern::Python),
"go" => Some(ImportPattern::Go),
"java" | "kt" | "scala" => Some(ImportPattern::JavaLike),
"cs" => Some(ImportPattern::JavaLike),
"c" | "cpp" | "cc" | "cxx" | "h" | "hpp" => Some(ImportPattern::CStyle),
"rb" => Some(ImportPattern::Ruby),
"php" => Some(ImportPattern::Php),
"sh" | "bash" => Some(ImportPattern::Shell),
"css" | "scss" | "less" => Some(ImportPattern::Css),
_ => None,
}
}
fn extract_rust_import(line: &str) -> Option<String> {
let trimmed = line.trim();
let import_part = trimmed.strip_prefix("use ")?.trim_end_matches(';').trim();
if import_part.starts_with("crate::")
|| import_part.starts_with("super::")
|| import_part.starts_with("self::")
{
let path = if let Some(brace_pos) = import_part.find('{') {
import_part[..brace_pos].trim()
} else if let Some(as_pos) = import_part.find(" as ") {
import_part[..as_pos].trim()
} else {
import_part
};
return Some(path.to_string());
}
None
}
fn extract_javascript_import(line: &str) -> Option<String> {
let trimmed = line.trim();
if trimmed.starts_with("import ") {
if let Some(from_pos) = trimmed.find(" from ") {
let after_from = &trimmed[from_pos + 6..].trim();
return extract_quoted_string(after_from);
}
if let Some(quote_pos) = trimmed.find(['"', '\'']) {
return extract_quoted_string(&trimmed[quote_pos..]);
}
}
if trimmed.starts_with("export ")
&& trimmed.contains(" from ")
&& let Some(from_pos) = trimmed.find(" from ")
{
let after_from = &trimmed[from_pos + 6..].trim();
return extract_quoted_string(after_from);
}
if trimmed.contains("require(")
&& let Some(paren_pos) = trimmed.find("require(")
{
let after_paren = &trimmed[paren_pos + 8..];
return extract_quoted_string(after_paren);
}
None
}
fn extract_python_import(line: &str) -> Option<String> {
let trimmed = line.trim();
if trimmed.starts_with("from ")
&& let Some(import_pos) = trimmed.find(" import ")
{
let module_path = trimmed[5..import_pos].trim();
if module_path.starts_with('.') {
return Some(module_path.to_string());
}
}
if let Some(import_part) = trimmed.strip_prefix("import ") {
let import_part = import_part.trim();
let module_path = if let Some(as_pos) = import_part.find(" as ") {
&import_part[..as_pos]
} else {
import_part
};
if module_path.starts_with('.') {
return Some(module_path.trim().to_string());
}
}
None
}
fn extract_go_import(line: &str) -> Option<String> {
let trimmed = line.trim();
let after_import = trimmed.strip_prefix("import ")?.trim();
extract_quoted_string(after_import)
}
fn extract_javalike_import(line: &str) -> Option<String> {
let trimmed = line.trim();
if let Some(import_part) = trimmed.strip_prefix("import ") {
let import_part = import_part.trim().trim_end_matches(';');
if import_part.starts_with("static ") {
return None;
}
return Some(import_part.to_string());
}
if !trimmed.contains('=')
&& let Some(import_part) = trimmed.strip_prefix("using ")
{
let import_part = import_part.trim().trim_end_matches(';');
return Some(import_part.to_string());
}
None
}
fn extract_c_include(line: &str) -> Option<String> {
let trimmed = line.trim();
let after_include = trimmed.strip_prefix("#include ")?.trim();
if let Some(path) = extract_quoted_string(after_include) {
return Some(path);
}
if after_include.starts_with('<')
&& after_include.contains('/')
&& let Some(end) = after_include.find('>')
{
return Some(after_include[1..end].to_string());
}
None
}
fn extract_ruby_require(line: &str) -> Option<String> {
let trimmed = line.trim();
if let Some(after_require) = trimmed.strip_prefix("require_relative ") {
return extract_quoted_string(after_require.trim());
}
if let Some(after_require) = trimmed.strip_prefix("require ")
&& let Some(path) = extract_quoted_string(after_require.trim())
&& path.starts_with('.')
{
return Some(path);
}
None
}
fn extract_php_include(line: &str) -> Option<String> {
let trimmed = line.trim();
for keyword in ["require", "require_once", "include", "include_once"] {
if let Some(after_keyword) = trimmed.strip_prefix(keyword) {
let after_keyword = after_keyword.trim();
if let Some(path) = extract_quoted_string(after_keyword) {
return Some(path);
}
}
}
None
}
fn extract_shell_source(line: &str) -> Option<String> {
let trimmed = line.trim();
if let Some(path) = trimmed.strip_prefix("source ") {
let path = path.trim();
return extract_quoted_string(path).or_else(|| Some(path.to_string()));
}
if let Some(path) = trimmed.strip_prefix(". ")
&& !trimmed.starts_with("..")
{
let path = path.trim();
return extract_quoted_string(path).or_else(|| Some(path.to_string()));
}
None
}
fn extract_css_import(line: &str) -> Option<String> {
let trimmed = line.trim();
let after_import = trimmed.strip_prefix("@import ")?.trim();
extract_quoted_string(after_import)
}
fn extract_quoted_string(s: &str) -> Option<String> {
let trimmed = s.trim();
let first_char = trimmed.chars().next()?;
if ['"', '\'', '`'].contains(&first_char)
&& let Some(end) = trimmed[1..].find(first_char)
{
return Some(trimmed[1..end + 1].to_string());
}
None
}
fn extract_import(line: &str, pattern: &ImportPattern) -> Option<String> {
match pattern {
ImportPattern::Rust => extract_rust_import(line),
ImportPattern::JavaScript => extract_javascript_import(line),
ImportPattern::Python => extract_python_import(line),
ImportPattern::Go => extract_go_import(line),
ImportPattern::JavaLike => extract_javalike_import(line),
ImportPattern::CStyle => extract_c_include(line),
ImportPattern::Ruby => extract_ruby_require(line),
ImportPattern::Php => extract_php_include(line),
ImportPattern::Shell => extract_shell_source(line),
ImportPattern::Css => extract_css_import(line),
}
}
pub fn scan_file_for_imports(file_path: &Path) -> Result<Vec<ImportStatement>> {
let mut imports = Vec::new();
let extension = file_path.extension().and_then(|e| e.to_str()).unwrap_or("");
let pattern = match get_import_pattern(extension) {
Some(p) => p,
None => return Ok(imports), };
let content = fs::read_to_string(file_path)?;
for (line_number, line) in content.lines().enumerate() {
if let Some(imported_path) = extract_import(line, &pattern) {
imports.push(ImportStatement {
file_path: file_path.to_string_lossy().to_string(),
line_number: line_number + 1, line_content: line.trim().to_string(),
imported_path,
});
}
}
Ok(imports)
}
pub fn build_file_map(base_path: &Path) -> HashMap<String, PathBuf> {
let mut file_map = HashMap::new();
for entry in WalkDir::new(base_path)
.into_iter()
.filter_entry(|e| {
if e.file_type().is_dir() {
let dir_name = e.file_name().to_string_lossy();
!is_non_source_dir(&dir_name)
} else {
true
}
})
.filter_map(|e| e.ok())
{
if entry.file_type().is_file() {
let path = entry.path();
if let Ok(relative_path) = path.strip_prefix(base_path) {
let key = relative_path.to_string_lossy().to_string();
file_map.insert(key, path.to_path_buf());
}
}
}
file_map
}
pub fn resolve_import_path(
import_path: &str,
source_file: &Path,
base_path: &Path,
file_map: &HashMap<String, PathBuf>,
) -> Option<PathBuf> {
let source_dir = source_file.parent()?;
if import_path.starts_with('.') {
resolve_relative_import(import_path, source_dir, base_path)
} else if import_path.contains("::") {
resolve_rust_module_path(import_path, source_file, base_path, file_map)
} else if import_path.contains('/') {
resolve_path_import(import_path, base_path, file_map)
} else {
None
}
}
fn resolve_relative_import(
import_path: &str,
source_dir: &Path,
base_path: &Path,
) -> Option<PathBuf> {
let _import_path_clean = import_path
.trim_start_matches("./")
.trim_start_matches("../");
let candidate = source_dir.join(import_path);
if candidate.is_dir() {
for index_name in ["index", "mod", "__init__"] {
for idx_ext in ["ts", "tsx", "js", "jsx", "rs", "py"] {
let index_path = candidate.join(format!("{}.{}", index_name, idx_ext));
if index_path.exists() && index_path.starts_with(base_path) {
if let Ok(canonical) = index_path.canonicalize() {
return Some(canonical);
}
return Some(index_path);
}
}
}
}
let extensions = [
"", ".ts", ".tsx", ".js", ".jsx", ".rs", ".py", ".go", ".java", ".rb", ".php",
];
for ext in extensions {
let path_with_ext = if ext.is_empty() {
candidate.clone()
} else {
let mut path_str = candidate.to_string_lossy().to_string();
path_str.push_str(ext);
PathBuf::from(path_str)
};
if path_with_ext.exists() && path_with_ext.starts_with(base_path) {
if let Ok(canonical) = path_with_ext.canonicalize() {
return Some(canonical);
}
return Some(path_with_ext);
}
if path_with_ext.is_dir() {
for index_name in ["index", "mod", "__init__"] {
for idx_ext in ["ts", "tsx", "js", "jsx", "rs", "py"] {
let index_path = path_with_ext.join(format!("{}.{}", index_name, idx_ext));
if index_path.exists() && index_path.starts_with(base_path) {
return Some(index_path);
}
}
}
}
}
None
}
fn resolve_rust_module_path(
import_path: &str,
source_file: &Path,
base_path: &Path,
_file_map: &HashMap<String, PathBuf>,
) -> Option<PathBuf> {
let path_str = if let Some(stripped) = import_path.strip_prefix("crate::") {
stripped
} else if let Some(stripped) = import_path.strip_prefix("super::") {
return resolve_super_path(stripped, source_file, base_path);
} else {
import_path.strip_prefix("self::")?
};
let path_parts: Vec<&str> = path_str.split("::").collect();
let src_dir = find_src_directory(base_path)?;
let mut module_path = src_dir.clone();
for part in &path_parts {
module_path = module_path.join(part);
}
if module_path.with_extension("rs").exists() {
return Some(module_path.with_extension("rs"));
}
let mod_path = module_path.join("mod.rs");
if mod_path.exists() {
return Some(mod_path);
}
None
}
fn resolve_super_path(
remaining_path: &str,
source_file: &Path,
base_path: &Path,
) -> Option<PathBuf> {
let source_dir = source_file.parent()?;
let parent_dir = source_dir.parent()?;
if !parent_dir.starts_with(base_path) {
return None;
}
let path_parts: Vec<&str> = remaining_path.split("::").collect();
let mut module_path = parent_dir.to_path_buf();
for part in &path_parts {
module_path = module_path.join(part);
}
if module_path.with_extension("rs").exists() {
return Some(module_path.with_extension("rs"));
}
let mod_path = module_path.join("mod.rs");
if mod_path.exists() {
return Some(mod_path);
}
None
}
fn find_src_directory(base_path: &Path) -> Option<PathBuf> {
let src_dir = base_path.join("src");
if src_dir.is_dir() {
return Some(src_dir);
}
for entry in (fs::read_dir(base_path).ok()?).flatten() {
let path = entry.path();
if path.is_dir() {
let nested_src = path.join("src");
if nested_src.is_dir() {
return Some(nested_src);
}
}
}
None
}
fn resolve_path_import(
import_path: &str,
_base_path: &Path,
file_map: &HashMap<String, PathBuf>,
) -> Option<PathBuf> {
if let Some(path) = file_map.get(import_path) {
return Some(path.clone());
}
for ext in [
"ts", "tsx", "js", "jsx", "rs", "py", "go", "java", "rb", "php",
] {
let with_ext = format!("{}.{}", import_path, ext);
if let Some(path) = file_map.get(&with_ext) {
return Some(path.clone());
}
}
for index_name in ["index", "mod", "__init__"] {
for ext in ["ts", "tsx", "js", "jsx", "rs", "py"] {
let index_path = format!("{}/{}.{}", import_path, index_name, ext);
if let Some(path) = file_map.get(&index_path) {
return Some(path.clone());
}
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_rust_import() {
assert_eq!(
extract_rust_import("use crate::models::Feature;"),
Some("crate::models::Feature".to_string())
);
assert_eq!(
extract_rust_import("use super::helper;"),
Some("super::helper".to_string())
);
assert_eq!(
extract_rust_import("use self::utils;"),
Some("self::utils".to_string())
);
}
#[test]
fn test_extract_javascript_import() {
assert_eq!(
extract_javascript_import("import { Feature } from './models';"),
Some("./models".to_string())
);
assert_eq!(
extract_javascript_import("const x = require('../utils');"),
Some("../utils".to_string())
);
assert_eq!(
extract_javascript_import("export { Feature } from './models';"),
Some("./models".to_string())
);
}
#[test]
fn test_extract_python_import() {
assert_eq!(
extract_python_import("from .models import Feature"),
Some(".models".to_string())
);
assert_eq!(
extract_python_import("from ..utils import helper"),
Some("..utils".to_string())
);
}
#[test]
fn test_extract_quoted_string() {
assert_eq!(
extract_quoted_string("\"./path/to/file\""),
Some("./path/to/file".to_string())
);
assert_eq!(
extract_quoted_string("'./path/to/file'"),
Some("./path/to/file".to_string())
);
}
}