"""Rust dependency parser for Cargo.toml.""" from pathlib import Path import tomllib from . import BaseParser, Dependency class RustDependencyParser(BaseParser): """Parser for Rust Cargo.toml files.""" def can_parse(self, path: Path) -> bool: """Check if the file is a Cargo.toml.""" return path.name.lower() == "cargo.toml" def parse(self, path: Path) -> list[Dependency]: """Parse dependencies from Cargo.toml.""" if not path.exists(): return [] dependencies = [] try: with open(path, "rb") as f: data = tomllib.load(f) except (tomllib.TOMLDecodeError, OSError): return dependencies self._parse_dependencies_section(data, "dependencies", dependencies, path) self._parse_dependencies_section(data, "dev-dependencies", dependencies, path, is_dev=True) self._parse_dependencies_section(data, "build-dependencies", dependencies, path, is_dev=True) return dependencies def _parse_dependencies_section( self, data: dict, section: str, dependencies: list, source_file: Path, is_dev: bool = False, ) -> None: """Parse a specific dependencies section.""" if section in data: section_data = data[section] if isinstance(section_data, dict): for name, value in section_data.items(): if isinstance(value, str): version = value elif isinstance(value, dict): version = value.get("version", None) features = value.get("features", []) if features: pass else: version = None dependencies.append( Dependency( name=name, version=self._normalize_version(version), is_dev=is_dev, source_file=source_file, ) )