Fix rust parseing

This commit is contained in:
2025-11-14 00:23:45 +00:00
parent 20923f1555
commit e12d386b8f
+21 -5
View File
@@ -1537,6 +1537,9 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
"""Cached Rust parsing with enhanced metadata for graph relationships.""" """Cached Rust parsing with enhanced metadata for graph relationships."""
filepath = Path(filepath_str) filepath = Path(filepath_str)
chunks = [] chunks = []
logger.info(f"🔧 Parsing Rust file: {filepath}")
try: try:
code = filepath.read_text(encoding="utf-8") code = filepath.read_text(encoding="utf-8")
lines = code.splitlines(keepends=True) lines = code.splitlines(keepends=True)
@@ -1544,6 +1547,7 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
# Try using rust-analyzer AST if available # Try using rust-analyzer AST if available
rust_helper = Path("./tools/parse_rust_ast") rust_helper = Path("./tools/parse_rust_ast")
if rust_helper.exists(): if rust_helper.exists():
logger.info(f" Using Rust AST helper: {rust_helper}")
try: try:
proc = subprocess.run( proc = subprocess.run(
[str(rust_helper), str(filepath)], [str(rust_helper), str(filepath)],
@@ -1553,8 +1557,9 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
timeout=20 timeout=20
) )
decls = json.loads(proc.stdout) decls = json.loads(proc.stdout)
logger.info(f" AST helper found {len(decls)} declarations")
for d in decls: for i, d in enumerate(decls):
start_line = max(0, d.get("start_line", 1) - 1) start_line = max(0, d.get("start_line", 1) - 1)
end_line = d.get("end_line", start_line + 1) end_line = d.get("end_line", start_line + 1)
chunk_code = "".join(lines[start_line:end_line]) chunk_code = "".join(lines[start_line:end_line])
@@ -1563,7 +1568,7 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
metadata = { metadata = {
"file": str(filepath), "file": str(filepath),
"name": d.get("name", ""), "name": d.get("name", ""),
"type": d.get("type", ""), "type": d.get("type_", ""),
"line": start_line + 1, "line": start_line + 1,
"language": "rust", "language": "rust",
} }
@@ -1588,7 +1593,10 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
if d.get("parameters"): if d.get("parameters"):
metadata["parameters"] = d.get("parameters") metadata["parameters"] = d.get("parameters")
chunk_text = f"File: {filepath}\nType: {d.get('type')}\nName: {d.get('name')}\n" # Log what we found
logger.info(f" Declaration {i+1}: {metadata.get('type', 'unknown')} {metadata.get('name', 'unnamed')}")
chunk_text = f"File: {filepath}\nType: {d.get('type_')}\nName: {d.get('name')}\n"
# Add Rust-specific details to text # Add Rust-specific details to text
if d.get("visibility"): if d.get("visibility"):
@@ -1612,18 +1620,26 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
}) })
if chunks: if chunks:
logger.info(f"✅ Successfully parsed {len(chunks)} chunks from {filepath}")
return tuple((c["text"], tuple(c["metadata"].items())) for c in chunks) return tuple((c["text"], tuple(c["metadata"].items())) for c in chunks)
else:
logger.warning(f"❌ AST helper returned declarations but no chunks were created")
except Exception as e: except Exception as e:
logger.warning(f"Rust AST helper failed for {filepath}: {e}; falling back to regex") logger.warning(f"Rust AST helper failed for {filepath}: {e}; falling back to regex")
# Fallback: Regex-based Rust parsing # Fallback: Regex-based Rust parsing
chunks.extend(_parse_rust_with_regex(filepath, code, lines)) logger.info(f" Falling back to regex parsing for {filepath}")
regex_chunks = _parse_rust_with_regex(filepath, code, lines)
chunks.extend(regex_chunks)
logger.info(f" Regex found {len(regex_chunks)} chunks")
except Exception as e: except Exception as e:
logger.warning(f"parse_rust_file failed {filepath}: {e}") logger.warning(f"parse_rust_file failed {filepath}: {e}")
return tuple((c["text"], tuple(c["metadata"].items())) for c in chunks) final_result = tuple((c["text"], tuple(c["metadata"].items())) for c in chunks)
logger.info(f"📦 Final result: {len(final_result)} chunks with metadata")
return final_result
def _parse_rust_with_regex(filepath: Path, code: str, lines: list) -> list: def _parse_rust_with_regex(filepath: Path, code: str, lines: list) -> list:
"""Fallback regex-based Rust parser.""" """Fallback regex-based Rust parser."""