openapi_model_generator/
models.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub enum ModelType {
5    Struct(Model),
6    Union(UnionModel),             // oneOf/anyOf -> enum
7    Composition(CompositionModel), // allOf
8    Enum(EnumModel),               // enum values -> enum
9    TypeAlias(TypeAliasModel),     // x-rust-type -> type alias
10}
11
12impl ModelType {
13    pub fn name(&self) -> &str {
14        match self {
15            ModelType::Struct(m) => &m.name,
16            ModelType::Enum(e) => &e.name,
17            ModelType::Union(u) => &u.name,
18            ModelType::Composition(c) => &c.name,
19            ModelType::TypeAlias(t) => &t.name,
20        }
21    }
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct Model {
26    pub name: String,
27    pub fields: Vec<Field>,
28    pub custom_attrs: Option<Vec<String>>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Field {
33    pub name: String,
34    pub field_type: String,
35    pub format: String,
36    pub is_required: bool,
37    pub is_nullable: bool,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct UnionModel {
42    pub name: String,
43    pub variants: Vec<UnionVariant>,
44    pub union_type: UnionType,
45    pub custom_attrs: Option<Vec<String>>,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub enum UnionType {
50    OneOf,
51    AnyOf,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct UnionVariant {
56    pub name: String,
57    pub fields: Vec<Field>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct CompositionModel {
62    pub name: String,
63    pub all_fields: Vec<Field>,
64    pub custom_attrs: Option<Vec<String>>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct RequestModel {
69    pub name: String,
70    pub content_type: String,
71    pub schema: String,
72    pub is_required: bool,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct ResponseModel {
77    pub name: String,
78    pub status_code: String,
79    pub content_type: String,
80    pub schema: String,
81    pub description: Option<String>,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct EnumModel {
86    pub name: String,
87    pub variants: Vec<String>,
88    pub description: Option<String>,
89    pub custom_attrs: Option<Vec<String>>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct TypeAliasModel {
94    pub name: String,
95    pub target_type: String,
96    pub description: Option<String>,
97    pub custom_attrs: Option<Vec<String>>,
98}