Crate py_rs

Source
Expand description

logo
py-rs

Generate python type declarations from rust types

§Why?

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-rs that are not tested or converted.

§How?

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.

§Get started

[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

§Features

  • generate type declarations from rust structs
  • generate union declarations from rust enums
  • generate necessary imports when exporting to multiple files
  • serde compatibility
  • generic types
  • support for ESM imports

Note: not all the features are tested for Python.

§cargo features

FeatureDescription
serde-compatEnabled by default
See the “serde compatibility” section below for more information.
formatEnables formatting of the generated Python bindings.
Currently, this unfortunately adds quite a few dependencies.
no-serde-warningsBy default, warnings are printed during build if unsupported serde attributes are encountered.
Enabling this feature silences these warnings.
serde-json-implImplement PY for types from serde_json
chrono-implImplement PY for types from chrono
bigdecimal-implImplement PY for types from bigdecimal
url-implImplement PY for types from url
uuid-implImplement PY for types from uuid
bson-uuid-implImplement PY for bson::oid::ObjectId and bson::uuid
bytes-implImplement PY for types from bytes
indexmap-implImplement PY for types from indexmap
ordered-float-implImplement PY for types from ordered_float
heapless-implImplement PY for types from heapless
semver-implImplement PY for types from semver
smol_str-implImplement PY for types from smol_str
tokio-implImplement 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 compatability

With the serde-compat feature (enabled by default), serde attributes can be parsed for enums and structs. Supported serde attributes:

  • rename
  • rename-all
  • rename-all-fields
  • tag
  • content
  • untagged
  • skip
  • flatten
  • default

Note: 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.

§Contributing

Contributions are always welcome! Feel free to open an issue, discuss using GitHub discussions or open a PR. See CONTRIBUTING.md

§MSRV

The Minimum Supported Rust Version for this crate is 1.63.0

Structs§

Dependency
A python type which is depended upon by other types. This information is required for generating the correct import statements.

Enums§

ExportError
An error which may occur when exporting a type

Traits§

PY
A type which can be represented in Python.
Most of the time, you’d want to derive this trait instead of implementing it manually.
py-rs comes with implementations for all primitives, most collections, tuples, arrays and containers.
TypeVisitor
A visitor used to iterate over all dependencies or generics of a type. When an instance of TypeVisitor is passed to PY::visit_dependencies or PY::visit_generics, the TypeVisitor::visit method will be invoked for every dependency or generic parameter respectively.

Derive Macros§

PY
Derives PY for a struct or enum. Please take a look at PY for documentation.