import { join } from "node:path";
import { existsSync } from "node:fs";
class RustDetector {
  name = "rust";
  language = "rust";
  filePattern = /\.rs$/;
  async detect(projectRoot, files) {
    if (files.some((f) => f === "Cargo.toml" || f.endsWith("/Cargo.toml"))) {
      return true;
    }
    const cargoPath = join(projectRoot, "Cargo.toml");
    return existsSync(cargoPath);
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    const axumRegex = /\.route\s*\(\s*['"]([^'"]+)['"]\s*,\s*([a-z0-9_]+)\s*\(\s*([a-zA-Z0-9_:]+)\s*\)\s*\)/g;
    let match;
    while ((match = axumRegex.exec(content)) !== null) {
      const pathVal = match[1];
      const verb = match[2].toUpperCase();
      const handlerSymbol = match[3];
      let resolvedFile = filePath;
      const resolvedPath = ctx.resolveSymbolToFile(handlerSymbol);
      if (resolvedPath) {
        resolvedFile = resolvedPath;
      }
      routes.push({
        framework: this.name,
        method: verb,
        path: pathVal,
        handlerFile: resolvedFile,
        handlerSymbol,
        metadata: {
          confidence: "inferred",
          routeType: "server"
        }
      });
    }
    const attrRegex = /#\[(get|post|put|delete|patch)\s*\(\s*['"]([^'"]+)['"]\s*\)\]\s*(?:pub\s+)?(?:async\s+)?fn\s+([a-zA-Z0-9_]+)\b/g;
    while ((match = attrRegex.exec(content)) !== null) {
      const verb = match[1].toUpperCase();
      let pathVal = match[2];
      const handlerSymbol = match[3];
      pathVal = pathVal.replace(/<([a-zA-Z0-9_]+)>/g, "{$1}");
      routes.push({
        framework: this.name,
        method: verb,
        path: pathVal,
        handlerFile: filePath,
        handlerSymbol,
        metadata: {
          confidence: "inferred",
          routeType: "server"
        }
      });
    }
    return routes;
  }
}
export {
  RustDetector
};
