py-rs 0.1.1

generate python bindings from rust types
Documentation
#![allow(dead_code)]

use py_rs::PY;

#[derive(PY)]
#[py(export_to = "imports/ts_rs_test_type_a.ts")]
pub struct TestTypeA<T> {
    value: T,
}

#[derive(PY)]
#[py(export_to = "imports/ts_rs_test_type_b.ts")]
pub struct TestTypeB<T> {
    value: T,
}

#[derive(PY)]
#[py(export_to = "imports/")]
pub enum TestEnum {
    C { value: TestTypeB<i8> },
    A1 { value: TestTypeA<i32> },
    A2 { value: TestTypeA<i8> },
}

#[test]
fn test_def() {
    // The only way to get access to how the imports look is to export the type and load the exported file
    TestEnum::export_all().unwrap();
    let text = std::fs::read_to_string(TestEnum::default_output_path().unwrap()).unwrap();

    let expected = match (cfg!(feature = "format"), cfg!(feature = "import-esm")) {
        (true, true) => concat!(
            "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
            "import type { TestTypeA } from \"./ts_rs_test_type_a.js\";\n",
            "import type { TestTypeB } from \"./ts_rs_test_type_b.js\";\n",
            "\n",
            "export type TestEnum = { \"C\": { value: TestTypeB<number> } } | {\n",
            "  \"A1\": { value: TestTypeA<number> };\n",
            "} | { \"A2\": { value: TestTypeA<number> } };\n",
        ),
        (true, false) => concat!(
            "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
            "import type { TestTypeA } from \"./ts_rs_test_type_a\";\n",
            "import type { TestTypeB } from \"./ts_rs_test_type_b\";\n",
            "\n",
            "export type TestEnum = { \"C\": { value: TestTypeB<number> } } | {\n",
            "  \"A1\": { value: TestTypeA<number> };\n",
            "} | { \"A2\": { value: TestTypeA<number> } };\n",
        ),
        (false, true) => concat!(
            "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
            "import type { TestTypeA } from \"./ts_rs_test_type_a.js\";\n",
            "import type { TestTypeB } from \"./ts_rs_test_type_b.js\";\n",
            "\n",
            "export type TestEnum = { \"C\": { value: TestTypeB<number>, } } | { \"A1\": { value: TestTypeA<number>, } } | { \"A2\": { value: TestTypeA<number>, } };",
            "\n",
        ),
        (false, false) => concat!(
            "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
            "import type { TestTypeA } from \"./ts_rs_test_type_a\";\n",
            "import type { TestTypeB } from \"./ts_rs_test_type_b\";\n",
            "\n",
            "export type TestEnum = { \"C\": { value: TestTypeB<number>, } } | { \"A1\": { value: TestTypeA<number>, } } | { \"A2\": { value: TestTypeA<number>, } };",
            "\n",
        ),
    };

    assert_eq!(text, expected);
}