From be36f328965b4bb01ed46538eafaa85844e8b351 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 5 Feb 2026 08:40:06 +0000 Subject: [PATCH] Initial upload: Auto README Generator CLI v0.1.0 --- src/auto_readme/parsers/rust_parser.py | 65 ++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/auto_readme/parsers/rust_parser.py diff --git a/src/auto_readme/parsers/rust_parser.py b/src/auto_readme/parsers/rust_parser.py new file mode 100644 index 0000000..79a3e46 --- /dev/null +++ b/src/auto_readme/parsers/rust_parser.py @@ -0,0 +1,65 @@ +"""Rust dependency parser for Cargo.toml.""" + +from pathlib import Path +from typing import Optional +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, + ) + )