rust_mc_proto_tokio/convert_async.py
MeexReay a8299d75d1
Some checks failed
Rust / Build (push) Waiting to run
Rust / Test Suite (push) Waiting to run
Rust / Check (push) Has been cancelled
fix warnings
2025-06-06 19:59:32 +03:00

47 lines
1.4 KiB
Python

import sys
import re
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <filename>")
sys.exit(1)
filename = sys.argv[1]
with open(filename, "r", encoding="utf-8") as f:
content = f.read()
pattern = re.compile(
r"""async\s+fn\s+ # async fn
(\w+)\s* # имя функции
\(([^)]*)\)\s* # аргументы
->\s*([^{\n]+)\s* # возвращаемый тип
\{\s*\n # открывающая фигурная скобка + новая строка
(.*?) # тело функции (ленивый захват)
^\} # закрывающая фигурная скобка на отдельной строке
""",
re.DOTALL | re.MULTILINE | re.VERBOSE,
)
def replacer(match):
name = match.group(1)
args = match.group(2)
ret = match.group(3).strip()
body = match.group(4)
# Добавляем 4 пробела в начале каждой строки тела
indented_body = "\n".join(" " + line if line.strip() else "" for line in body.splitlines())
return (
f"fn {name}({args}) -> impl std::future::Future<Output = {ret}> {{\n"
f" async {{\n"
f"{indented_body}\n"
f" }}\n"
f"}}"
)
new_content = pattern.sub(replacer, content)
with open(filename, "w", encoding="utf-8") as f:
f.write(new_content)