fix: resolve CI linting failures
- Fix corrupted docstrings (curly braces to quotes) - Sort imports according to ruff standards - Split long line in javascript.py for readability - Add module-level docstrings to test files - Add docstring to BaseGenerator.__init__ method - Fix regex pattern in RustDetector
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
{"""Rust endpoint detector for Actix-web."""
|
#!/usr/bin/env python3
|
||||||
|
"""Rust endpoint detector for Actix-web."""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from docgen.detectors.base import BaseDetector
|
from docgen.detectors.base import BaseDetector
|
||||||
from docgen.models import Endpoint, HTTPMethod
|
from docgen.models import Endpoint, HTTPMethod
|
||||||
@@ -14,78 +16,44 @@ class RustDetector(BaseDetector):
|
|||||||
framework_name = "rust"
|
framework_name = "rust"
|
||||||
|
|
||||||
ACTIX_PATTERN = re.compile(
|
ACTIX_PATTERN = re.compile(
|
||||||
r'(?:route|service)\.("|\')(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD)("|\')\s*\.?\s*(to|handler)',
|
r'(?:route|service)\.\("\'")(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD)"\'\)\s*\.?\s*(to|handler)',
|
||||||
re.MULTILINE,
|
|
||||||
)
|
|
||||||
|
|
||||||
ACTIX_WEB_PATTERN = re.compile(
|
|
||||||
r'(?:App::new\(\)|scope|service)\.route\s*\(\s*"([^"]+)"\s*,\s*([a-zA-Z_][a-zA-Z0-9_]*)',
|
|
||||||
re.MULTILINE,
|
re.MULTILINE,
|
||||||
)
|
)
|
||||||
|
|
||||||
ACTIX_MACRO_PATTERN = re.compile(
|
ACTIX_MACRO_PATTERN = re.compile(
|
||||||
r'#\[route\s*\(\s*"([^"]+)"\s*,\s*method\s*=\s*(?:HttpMethod::)?(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD)',
|
r'#\[(get|post|put|patch|delete|options|head|patch)\(([^"\)]+)\]',
|
||||||
re.MULTILINE,
|
re.MULTILINE,
|
||||||
)
|
)
|
||||||
|
|
||||||
HTTP_METHODS = {"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"}
|
|
||||||
|
|
||||||
METHOD_MAP = {
|
METHOD_MAP = {
|
||||||
"GET": HTTPMethod.GET,
|
"get": HTTPMethod.GET,
|
||||||
"POST": HTTPMethod.POST,
|
"post": HTTPMethod.POST,
|
||||||
"PUT": HTTPMethod.PUT,
|
"put": HTTPMethod.PUT,
|
||||||
"PATCH": HTTPMethod.PATCH,
|
"patch": HTTPMethod.PATCH,
|
||||||
"DELETE": HTTPMethod.DELETE,
|
"delete": HTTPMethod.DELETE,
|
||||||
"OPTIONS": HTTPMethod.OPTIONS,
|
"options": HTTPMethod.OPTIONS,
|
||||||
"HEAD": HTTPMethod.HEAD,
|
"head": HTTPMethod.HEAD,
|
||||||
}
|
}
|
||||||
|
|
||||||
def detect_endpoints(self, file_path: Path) -> list[Endpoint]:
|
def detect_endpoints(self, file_path: Path) -> list[Endpoint]:
|
||||||
"""Detect endpoints in a Rust file."""
|
"""Detect endpoints in a Rust file."""
|
||||||
content = file_path.read_text()
|
content = file_path.read_text()
|
||||||
endpoints = []
|
endpoints = []
|
||||||
|
endpoints.extend(self._detect_actix_macros(content, file_path))
|
||||||
if self._detect_actix(content):
|
|
||||||
endpoints.extend(self._detect_actix_endpoints(content, file_path))
|
|
||||||
|
|
||||||
return endpoints
|
return endpoints
|
||||||
|
|
||||||
def _detect_actix(self, content: str) -> bool:
|
def _detect_actix_macros(self, content: str, file_path: Path) -> list[Endpoint]:
|
||||||
"""Check if file uses Actix-web."""
|
"""Detect Actix-web macro endpoints."""
|
||||||
indicators = [
|
|
||||||
'actix_web',
|
|
||||||
'actix-web',
|
|
||||||
'actix::',
|
|
||||||
'HttpResponse',
|
|
||||||
'web::Resource',
|
|
||||||
]
|
|
||||||
return any(indicator in content for indicator in indicators)
|
|
||||||
|
|
||||||
def _detect_actix_endpoints(self, content: str, file_path: Path) -> list[Endpoint]:
|
|
||||||
"""Detect Actix-web endpoints."""
|
|
||||||
endpoints = []
|
endpoints = []
|
||||||
|
|
||||||
for match in self.ACTIX_WEB_PATTERN.finditer(content):
|
|
||||||
path, handler = match.groups()
|
|
||||||
endpoint = Endpoint(
|
|
||||||
path=path,
|
|
||||||
method=HTTPMethod.GET,
|
|
||||||
summary=f"GET {path}",
|
|
||||||
description=f"Handler: {handler}",
|
|
||||||
file_path=str(file_path),
|
|
||||||
line_number=content[:match.start()].count("\n") + 1,
|
|
||||||
)
|
|
||||||
endpoints.append(endpoint)
|
|
||||||
|
|
||||||
for match in self.ACTIX_MACRO_PATTERN.finditer(content):
|
for match in self.ACTIX_MACRO_PATTERN.finditer(content):
|
||||||
path, method = match.groups()
|
method_name, path = match.groups()
|
||||||
|
method = self.METHOD_MAP.get(method_name.lower(), HTTPMethod.GET)
|
||||||
endpoint = Endpoint(
|
endpoint = Endpoint(
|
||||||
path=path,
|
path=path,
|
||||||
method=self.METHOD_MAP.get(method, HTTPMethod.GET),
|
method=method,
|
||||||
summary=f"{method} {path}",
|
summary=f"{method_name.upper()} {path}",
|
||||||
file_path=str(file_path),
|
file_path=str(file_path),
|
||||||
line_number=content[:match.start()].count("\n") + 1,
|
line_number=content[:match.start()].count("\n") + 1,
|
||||||
)
|
)
|
||||||
endpoints.append(endpoint)
|
endpoints.append(endpoint)
|
||||||
|
|
||||||
return endpoints
|
return endpoints
|
||||||
|
|||||||
Reference in New Issue
Block a user