gesha_rust_types/
definition.rs

1use crate::{
2    DataType, Definitions, EnumVariant, ErrorDef, Imports, MediaTypeDef, ModuleName,
3    RequestBodyDef, StructField, TypeHeader,
4};
5
6#[derive(Clone, Debug, PartialEq)]
7pub enum Definition {
8    StructDef(StructDef),
9    NewTypeDef(NewTypeDef),
10    EnumDef(EnumDef),
11    PresetDef(PresetDef),
12    RequestBodyDef(RequestBodyDef),
13    ModDef(ModDef),
14}
15
16impl Definition {
17    pub fn any_type<F>(&self, f: F) -> bool
18    where
19        F: Fn(&DataType) -> bool,
20    {
21        match self {
22            Definition::StructDef(x) => x.fields.iter().any(|x| f(&x.data_type)),
23            Definition::NewTypeDef(x) => f(&x.data_type),
24            Definition::EnumDef(_) => false,
25            Definition::PresetDef(_) => false,
26            Definition::RequestBodyDef(_) => false,
27            Definition::ModDef(_) => false,
28        }
29    }
30}
31
32#[derive(Clone, Debug, PartialEq)]
33pub enum PresetDef {
34    Error(ErrorDef),
35    /// rf. https://stackoverflow.com/q/44331037
36    Patch,
37    MediaType(MediaTypeDef),
38    FromJson,
39}
40
41impl From<PresetDef> for Definition {
42    fn from(this: PresetDef) -> Self {
43        Definition::PresetDef(this)
44    }
45}
46
47#[derive(Clone, Debug, PartialEq)]
48pub struct StructDef {
49    pub header: TypeHeader,
50    pub fields: Vec<StructField>,
51    _hide_default_constructor: bool,
52}
53
54impl StructDef {
55    pub fn new(header: TypeHeader, fields: Vec<StructField>) -> Self {
56        Self {
57            header,
58            fields,
59            _hide_default_constructor: true,
60        }
61    }
62}
63
64impl From<StructDef> for Definition {
65    fn from(x: StructDef) -> Self {
66        Self::StructDef(x)
67    }
68}
69
70#[derive(Clone, Debug, PartialEq)]
71pub struct NewTypeDef {
72    pub header: TypeHeader,
73    pub data_type: DataType,
74    _hide_default_constructor: bool,
75}
76
77impl NewTypeDef {
78    pub fn new(header: TypeHeader, data_type: DataType) -> Self {
79        Self {
80            header,
81            data_type,
82            _hide_default_constructor: true,
83        }
84    }
85}
86
87impl From<NewTypeDef> for Definition {
88    fn from(x: NewTypeDef) -> Self {
89        Self::NewTypeDef(x)
90    }
91}
92
93#[derive(Clone, Debug, PartialEq)]
94pub struct EnumDef {
95    pub header: TypeHeader,
96    pub variants: Vec<EnumVariant>,
97    _hide_default_constructor: bool,
98}
99
100impl EnumDef {
101    pub fn new(header: TypeHeader, variants: Vec<EnumVariant>) -> Self {
102        Self {
103            header,
104            variants,
105            _hide_default_constructor: true,
106        }
107    }
108}
109
110impl From<EnumDef> for Definition {
111    fn from(this: EnumDef) -> Self {
112        Self::EnumDef(this)
113    }
114}
115
116#[derive(Clone, Debug, PartialEq)]
117pub struct ModDef {
118    pub name: ModuleName,
119    pub imports: Imports,
120    pub defs: Definitions,
121}
122
123impl From<ModDef> for Definition {
124    fn from(this: ModDef) -> Self {
125        Self::ModDef(this)
126    }
127}