# utoipa_repr
[](https://crates.io/crates/utoipa_repr)
[](https://docs.rs/utoipa_repr)
[](https://github.com/inomata137/utoipa_repr)
A derive macro for [utoipa](https://github.com/juhaku/utoipa) that provides `ToSchema_repr` functionality, similar to how [serde_repr](https://github.com/dtolnay/serde-repr) works for serde.
This crate allows you to generate OpenAPI schema for enums with `#[repr(...)]` attributes, representing them as their underlying integer types in the schema instead of as enum variants.
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
utoipa_repr = "0.1"
utoipa = "5.0"
```
## Usage
### Basic Example
```rust
use utoipa_repr::ToSchema_repr;
#[derive(ToSchema_repr)]
#[repr(u8)]
enum Status {
Pending = 0,
InProgress = 1,
Completed = 2,
Failed = 3,
}
// The generated OpenAPI schema will represent this as:
// {
// "type": "integer",
// "format": "int32",
// "minimum": 0
// }
```
### With Signed Integer Types
```rust
use utoipa_repr::ToSchema_repr;
#[derive(ToSchema_repr)]
#[repr(i32)]
enum Priority {
Low = -1,
Normal = 0,
High = 1,
Critical = 2,
}
// The generated OpenAPI schema will be:
// {
// "type": "integer",
// "format": "int32"
// }
```
## Supported Types
The following `repr` types are supported:
- **Unsigned integers**: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`
- **Signed integers**: `i8`, `i16`, `i32`, `i64`, `i128`, `isize`
### Schema Generation
- **Unsigned types** generate schemas with `"minimum": 0` constraint
- **Signed types** generate schemas without minimum constraint
- All types currently use `"format": "int32"` in the generated schema
## Additional `repr` Attributes
The macro safely ignores other `repr` attributes like `align` and `packed`:
```rust
#[derive(ToSchema_repr)]
#[repr(align(8), u16)] // align(8) is ignored, u16 is used
enum AlignedEnum {
A,
B,
}
```
## Error Handling
The macro will produce compile-time errors in the following cases:
- Missing `#[repr(...)]` attribute
- Unsupported `repr` type
- Invalid `repr` syntax
## License
Licensed under either of [Apache License](./LICENSE-APACHE), Version 2.0, or [MIT License](./LICENSE-MIT) at your option.