diff --git a/rust/Cargo.lock b/rust/Cargo.lock index eef5ef90d..17dd00f95 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1815,7 +1815,7 @@ dependencies = [ [[package]] name = "kcl-bumper" -version = "0.1.70" +version = "0.1.71" dependencies = [ "anyhow", "clap", @@ -1826,7 +1826,7 @@ dependencies = [ [[package]] name = "kcl-derive-docs" -version = "0.1.70" +version = "0.1.71" dependencies = [ "Inflector", "anyhow", @@ -1845,7 +1845,7 @@ dependencies = [ [[package]] name = "kcl-directory-test-macro" -version = "0.1.70" +version = "0.1.71" dependencies = [ "proc-macro2", "quote", @@ -1854,7 +1854,7 @@ dependencies = [ [[package]] name = "kcl-language-server" -version = "0.2.70" +version = "0.2.71" dependencies = [ "anyhow", "clap", @@ -1875,7 +1875,7 @@ dependencies = [ [[package]] name = "kcl-language-server-release" -version = "0.1.70" +version = "0.1.71" dependencies = [ "anyhow", "clap", @@ -1895,7 +1895,7 @@ dependencies = [ [[package]] name = "kcl-lib" -version = "0.2.70" +version = "0.2.71" dependencies = [ "anyhow", "approx 0.5.1", @@ -1971,7 +1971,7 @@ dependencies = [ [[package]] name = "kcl-python-bindings" -version = "0.3.70" +version = "0.3.71" dependencies = [ "anyhow", "kcl-lib", @@ -1986,7 +1986,7 @@ dependencies = [ [[package]] name = "kcl-test-server" -version = "0.1.70" +version = "0.1.71" dependencies = [ "anyhow", "hyper 0.14.32", @@ -1999,7 +1999,7 @@ dependencies = [ [[package]] name = "kcl-to-core" -version = "0.1.70" +version = "0.1.71" dependencies = [ "anyhow", "async-trait", @@ -2013,7 +2013,7 @@ dependencies = [ [[package]] name = "kcl-wasm-lib" -version = "0.1.70" +version = "0.1.71" dependencies = [ "anyhow", "bson", diff --git a/rust/kcl-bumper/Cargo.toml b/rust/kcl-bumper/Cargo.toml index 683e6826a..aeaafc7a5 100644 --- a/rust/kcl-bumper/Cargo.toml +++ b/rust/kcl-bumper/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-bumper" -version = "0.1.70" +version = "0.1.71" edition = "2021" repository = "https://github.com/KittyCAD/modeling-api" rust-version = "1.76" diff --git a/rust/kcl-derive-docs/Cargo.toml b/rust/kcl-derive-docs/Cargo.toml index 3ea79c59b..6388f414e 100644 --- a/rust/kcl-derive-docs/Cargo.toml +++ b/rust/kcl-derive-docs/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-derive-docs" description = "A tool for generating documentation from Rust derive macros" -version = "0.1.70" +version = "0.1.71" edition = "2021" license = "MIT" repository = "https://github.com/KittyCAD/modeling-app" diff --git a/rust/kcl-directory-test-macro/Cargo.toml b/rust/kcl-directory-test-macro/Cargo.toml index bb03bed21..c67bb9292 100644 --- a/rust/kcl-directory-test-macro/Cargo.toml +++ b/rust/kcl-directory-test-macro/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-directory-test-macro" description = "A tool for generating tests from a directory of kcl files" -version = "0.1.70" +version = "0.1.71" edition = "2021" license = "MIT" repository = "https://github.com/KittyCAD/modeling-app" diff --git a/rust/kcl-language-server-release/Cargo.toml b/rust/kcl-language-server-release/Cargo.toml index 8d8bbacf6..0af30eb4f 100644 --- a/rust/kcl-language-server-release/Cargo.toml +++ b/rust/kcl-language-server-release/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kcl-language-server-release" -version = "0.1.70" +version = "0.1.71" edition = "2021" authors = ["KittyCAD Inc "] publish = false diff --git a/rust/kcl-language-server/Cargo.toml b/rust/kcl-language-server/Cargo.toml index 38d0da3c1..1f3e4822c 100644 --- a/rust/kcl-language-server/Cargo.toml +++ b/rust/kcl-language-server/Cargo.toml @@ -2,7 +2,7 @@ name = "kcl-language-server" description = "A language server for KCL." authors = ["KittyCAD Inc "] -version = "0.2.70" +version = "0.2.71" edition = "2021" license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/rust/kcl-lib/Cargo.toml b/rust/kcl-lib/Cargo.toml index 6eb39071e..72dc70387 100644 --- a/rust/kcl-lib/Cargo.toml +++ b/rust/kcl-lib/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-lib" description = "KittyCAD Language implementation and tools" -version = "0.2.70" +version = "0.2.71" edition = "2021" license = "MIT" repository = "https://github.com/KittyCAD/modeling-app" diff --git a/rust/kcl-python-bindings/Cargo.toml b/rust/kcl-python-bindings/Cargo.toml index 45a59302b..431b88519 100644 --- a/rust/kcl-python-bindings/Cargo.toml +++ b/rust/kcl-python-bindings/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kcl-python-bindings" -version = "0.3.70" +version = "0.3.71" edition = "2021" repository = "https://github.com/kittycad/modeling-app" exclude = ["tests/*", "files/*", "venv/*"] diff --git a/rust/kcl-python-bindings/src/lib.rs b/rust/kcl-python-bindings/src/lib.rs index 220a050d3..ba5ef0d96 100644 --- a/rust/kcl-python-bindings/src/lib.rs +++ b/rust/kcl-python-bindings/src/lib.rs @@ -227,6 +227,31 @@ async fn new_context_state(current_file: Option) -> Result<( Ok((ctx, state)) } +/// Parse the kcl code from a file path. +#[pyfunction] +async fn parse(path: String) -> PyResult<()> { + tokio() + .spawn(async move { + let (code, path) = get_code_and_file_path(&path) + .await + .map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string()))?; + let _program = kcl_lib::Program::parse_no_errs(&code) + .map_err(|err| into_miette_for_parse(&path.display().to_string(), &code, err))?; + + Ok(()) + }) + .await + .map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string()))? +} + +/// Parse the kcl code. +#[pyfunction] +fn parse_code(code: String) -> PyResult<()> { + let _program = kcl_lib::Program::parse_no_errs(&code).map_err(|err| into_miette_for_parse("", &code, err))?; + + Ok(()) +} + /// Execute the kcl code from a file path. #[pyfunction] async fn execute(path: String) -> PyResult<()> { @@ -534,6 +559,8 @@ fn kcl(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; // Add our functions to the module. + m.add_function(wrap_pyfunction!(parse, m)?)?; + m.add_function(wrap_pyfunction!(parse_code, m)?)?; m.add_function(wrap_pyfunction!(execute, m)?)?; m.add_function(wrap_pyfunction!(execute_code, m)?)?; m.add_function(wrap_pyfunction!(execute_and_snapshot, m)?)?; diff --git a/rust/kcl-python-bindings/tests/tests.py b/rust/kcl-python-bindings/tests/tests.py index ee08557bb..c31bb8249 100755 --- a/rust/kcl-python-bindings/tests/tests.py +++ b/rust/kcl-python-bindings/tests/tests.py @@ -39,6 +39,33 @@ async def test_kcl_execute(): await kcl.execute(lego_file) +@pytest.mark.asyncio +async def test_kcl_parse_with_exception(): + # Read from a file. + try: + await kcl.parse(os.path.join(files_dir, "parse_file_error")) + except Exception as e: + assert e is not None + assert len(str(e)) > 0 + assert "lksjndflsskjfnak;jfna##" in str(e) + + +@pytest.mark.asyncio +async def test_kcl_parse(): + # Read from a file. + await kcl.parse(lego_file) + + +@pytest.mark.asyncio +async def test_kcl_parse_code(): + # Read from a file. + with open(lego_file, "r") as f: + code = str(f.read()) + assert code is not None + assert len(code) > 0 + kcl.parse_code(code) + + @pytest.mark.asyncio async def test_kcl_execute_code(): # Read from a file. @@ -97,9 +124,7 @@ async def test_kcl_execute_and_snapshot(): @pytest.mark.asyncio async def test_kcl_execute_and_snapshot_dir(): # Read from a file. - image_bytes = await kcl.execute_and_snapshot( - car_wheel_dir, kcl.ImageFormat.Jpeg - ) + image_bytes = await kcl.execute_and_snapshot(car_wheel_dir, kcl.ImageFormat.Jpeg) assert image_bytes is not None assert len(image_bytes) > 0 @@ -129,10 +154,12 @@ def test_kcl_format(): assert formatted_code is not None assert len(formatted_code) > 0 + @pytest.mark.asyncio async def test_kcl_format_dir(): await kcl.format_dir(car_wheel_dir) + def test_kcl_lint(): # Read from a file. with open(os.path.join(files_dir, "box_with_linter_errors.kcl"), "r") as f: diff --git a/rust/kcl-test-server/Cargo.toml b/rust/kcl-test-server/Cargo.toml index 50dfb4f5a..48d2795bf 100644 --- a/rust/kcl-test-server/Cargo.toml +++ b/rust/kcl-test-server/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-test-server" description = "A test server for KCL" -version = "0.1.70" +version = "0.1.71" edition = "2021" license = "MIT" diff --git a/rust/kcl-to-core/Cargo.toml b/rust/kcl-to-core/Cargo.toml index 2d97b5868..73a7b54a9 100644 --- a/rust/kcl-to-core/Cargo.toml +++ b/rust/kcl-to-core/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-to-core" description = "Utility methods to convert kcl to engine core executable tests" -version = "0.1.70" +version = "0.1.71" edition = "2021" license = "MIT" repository = "https://github.com/KittyCAD/modeling-app" diff --git a/rust/kcl-wasm-lib/Cargo.toml b/rust/kcl-wasm-lib/Cargo.toml index 019743841..df387f9f6 100644 --- a/rust/kcl-wasm-lib/Cargo.toml +++ b/rust/kcl-wasm-lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kcl-wasm-lib" -version = "0.1.70" +version = "0.1.71" edition = "2021" repository = "https://github.com/KittyCAD/modeling-app" rust-version = "1.83"