zerodds_idl_python/lib.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3
4//! Crate `zerodds-idl-python`. Safety classification: **STANDARD**.
5//!
6//! IDL4 → Python code generator for ZeroDDS data types. Reads the IDL AST
7//! from `zerodds-idl` and emits `@idl_struct(...)` + `@dataclass`
8//! classes that meet the convention of the `zerodds-py` crate.
9//!
10//! Build-time tool — `forbid(unsafe_code)`, std-only.
11//!
12//! # Layer position
13//!
14//! Layer 3 (schema) — analogous to `zerodds-idl-cpp` / `-csharp` / `-java` /
15//! `-rust` / `-ts`, but emits Python code that runs against the
16//! `zerodds` Python library at runtime.
17//!
18//! # Public API
19//!
20//! - [`generate_python_module`] — AST + options → Python module source.
21//! - [`PythonGenOptions`] — codegen options.
22//! - [`error::IdlPythonError`] — error family.
23//!
24//! # What is emitted
25//!
26//! Per IDL construct:
27//!
28//! | IDL | Python |
29//! |-----|--------|
30//! | `struct` (any extensibility) | `@idl_struct(typename=...)` + `@dataclass class` |
31//! | `enum` | `class X(IntEnum)` |
32//! | `module M { ... }` | flat class name `M_Inner` (Python-PSM convention) |
33//! | `boolean` | `bool` |
34//! | `short`/`long`/`long long` | `Int16` / `Int32` / `Int64` (zerodds.idl brands) |
35//! | `unsigned short`/... | `UInt16` / `UInt32` / `UInt64` |
36//! | `octet` | `Octet` |
37//! | `float` / `double` / `long double` | `Float32` / `Float64` / `LongDouble` |
38//! | `char` / `wchar` | `Char` / `WChar` |
39//! | `string` / `wstring` | `String` / `WString` |
40//! | `sequence<T>` | `List[T]` |
41//! | `T[N]` | `List[T]` (multi-dim → nested) |
42//!
43//! # Phase-2 material (today `Unsupported`)
44//!
45//! - `union` with a discriminator
46//! - `bitset` / `bitmask`
47//! - `typedef T name` (for now without a comment; phase 2: `typing.TypeAlias`)
48//! - `valuetype`, `interface`, `exception`
49//! - `fixed`, `map`, `any`
50//!
51//! # Example
52//!
53//! ```rust
54//! use zerodds_idl::config::ParserConfig;
55//! use zerodds_idl_python::{generate_python_module, PythonGenOptions};
56//!
57//! let ast = zerodds_idl::parse(
58//! "struct Greeting { long id; string<128> text; };",
59//! &ParserConfig::default(),
60//! )
61//! .expect("parse");
62//! let py_src =
63//! generate_python_module(&ast, &PythonGenOptions::default()).expect("gen");
64//! assert!(py_src.contains("@dataclass"));
65//! assert!(py_src.contains("class Greeting:"));
66//! assert!(py_src.contains("id: Int32"));
67//! assert!(py_src.contains("text: String"));
68//! ```
69
70#![forbid(unsafe_code)]
71#![warn(missing_docs)]
72
73pub mod emitter;
74pub mod error;
75
76pub use emitter::{PythonGenOptions, generate_python_module};
77pub use error::{IdlPythonError, Result};