Usually it's the other way round, parsing Python with Rust, see here or here - in my case though I am looking for a way to parse Rust code with Python ideally into something like an AST that can be analyzed (ideally before any further compilation steps kick in). Specifically, I want to extract enum
and struct
definitions from an application written in Rust for unit-tests written in Python. Is there a standard way of doing this, perhaps through the Rust compiler?
Dependencies:
pip install tree-sitter tree-sitter-rust
Example usage:
import tree_sitter_rust as tsrust
from tree_sitter import Language, Parser
with open("some.rs", mode = "rb") as f:
raw = f.read()
parser = Parser(Language(tsrust.language()))
tree = parser.parse(raw)
enums = [
node
for node in tree.root_node.children
if node.type == 'enum_item'
]
print(enums)