libcst_native/
py.rs

1// Copyright (c) Meta Platforms, Inc. and affiliates.
2//
3// This source code is licensed under the MIT license found in the
4// LICENSE file in the root directory of this source tree
5
6use crate::nodes::traits::py::TryIntoPy;
7use pyo3::prelude::*;
8
9#[pymodule(gil_used = false)]
10#[pyo3(name = "native")]
11pub fn libcst_native(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
12    #[pyfn(m)]
13    #[pyo3(signature = (source, encoding=None))]
14    fn parse_module(source: String, encoding: Option<&str>) -> PyResult<Py<PyAny>> {
15        let m = crate::parse_module(source.as_str(), encoding)?;
16        Python::attach(|py| m.try_into_py(py))
17    }
18
19    #[pyfn(m)]
20    fn parse_expression(source: String) -> PyResult<Py<PyAny>> {
21        let expr = crate::parse_expression(source.as_str())?;
22        Python::attach(|py| expr.try_into_py(py))
23    }
24
25    #[pyfn(m)]
26    fn parse_statement(source: String) -> PyResult<Py<PyAny>> {
27        let stm = crate::parse_statement(source.as_str())?;
28        Python::attach(|py| stm.try_into_py(py))
29    }
30
31    Ok(())
32}