| Crates.io | py-rs |
| lib.rs | py-rs |
| version | 0.1.1 |
| created_at | 2025-02-23 23:15:20.07856+00 |
| updated_at | 2025-02-24 00:10:16.442505+00 |
| description | generate python bindings from rust types |
| homepage | https://github.com/Blue-Chestnut/py-rs |
| repository | https://github.com/Blue-Chestnut/py-rs |
| max_upload_size | |
| id | 1566708 |
| size | 241,962 |
Generate python type declarations from rust types
When building an api in rust, data structures have to be shared between backend and client. Using this library, you can easily generate python bindings to your rust structs & enums so that you can keep your types in one place.
Note: This is a work in progress. There are still some features of
ts-rsthat are not tested or converted.
py-rs exposes a single trait, PY. Using a derive macro, you can implement this interface for your types.
Then, you can use this trait to obtain the Python bindings.
We recommend doing this in your tests.
See the example and the docs.
[dependencies]
py-rs = "0.1.0"
use py_rs::PY;
#[derive(PY)]
#[py(export)]
struct User {
user_id: i32,
first_name: String,
last_name: String,
}
When running cargo test or cargo test export_bindings, the Python bindings will be exported to the file bindings/User.py
and will contain the following code:
from pydanic import BaseModel
class User(BaseModel):
user_id: int
first_name: str
last_name: str
Note: not all the features are tested for Python.
| Feature | Description |
|---|---|
| serde-compat | Enabled by default See the "serde compatibility" section below for more information. |
| format | Enables formatting of the generated Python bindings. Currently, this unfortunately adds quite a few dependencies. |
| no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. Enabling this feature silences these warnings. |
| serde-json-impl | Implement PY for types from serde_json |
| chrono-impl | Implement PY for types from chrono |
| bigdecimal-impl | Implement PY for types from bigdecimal |
| url-impl | Implement PY for types from url |
| uuid-impl | Implement PY for types from uuid |
| bson-uuid-impl | Implement PY for bson::oid::ObjectId and bson::uuid |
| bytes-impl | Implement PY for types from bytes |
| indexmap-impl | Implement PY for types from indexmap |
| ordered-float-impl | Implement PY for types from ordered_float |
| heapless-impl | Implement PY for types from heapless |
| semver-impl | Implement PY for types from semver |
| smol_str-impl | Implement PY for types from smol_str |
| tokio-impl | Implement PY for types from tokio |
If there's a type you're dealing with which doesn't implement PY, use either
#[py(as = "..")] or #[py(type = "..")], or open a PR.
serde compatabilityWith the serde-compat feature (enabled by default), serde attributes can be parsed for enums and structs.
Supported serde attributes:
renamerename-allrename-all-fieldstagcontentuntaggedskipflattendefaultNote: skip_serializing and skip_deserializing are ignored. If you wish to exclude a field
from the generated type, but cannot use #[serde(skip)], use #[py(skip)] instead.
When py-rs encounters an unsupported serde attribute, a warning is emitted, unless the feature no-serde-warnings is enabled.
Contributions are always welcome! Feel free to open an issue, discuss using GitHub discussions or open a PR. See CONTRIBUTING.md
The Minimum Supported Rust Version for this crate is 1.63.0
License: MIT