1use crate::parser::{
2 BitmaskDef, CallbackDef, CallbackFunctionDef, CallbackInfoDef, ConstantDef, DawnApi, EnumDef,
3 FunctionDef, FunctionPointerDef, NativeType, ObjectDef, StructureDef, TypedefDef,
4};
5
6#[derive(Debug, Clone)]
7pub struct ApiModel {
8 pub c_prefix: String,
9 pub enums: Vec<EnumModel>,
10 pub bitmasks: Vec<BitmaskModel>,
11 pub structures: Vec<StructureModel>,
12 pub objects: Vec<ObjectModel>,
13 pub functions: Vec<FunctionModel>,
14 pub callbacks: Vec<CallbackModel>,
15 pub callback_functions: Vec<CallbackFunctionModel>,
16 pub callback_infos: Vec<CallbackInfoModel>,
17 pub constants: Vec<ConstantModel>,
18 pub natives: Vec<NativeModel>,
19 pub typedefs: Vec<TypedefModel>,
20 pub function_pointers: Vec<FunctionPointerModel>,
21}
22
23#[derive(Debug, Clone)]
24pub struct EnumModel {
25 pub name: String,
26 pub def: EnumDef,
27}
28
29#[derive(Debug, Clone)]
30pub struct BitmaskModel {
31 pub name: String,
32 pub def: BitmaskDef,
33}
34
35#[derive(Debug, Clone)]
36pub struct StructureModel {
37 pub name: String,
38 pub def: StructureDef,
39}
40
41#[derive(Debug, Clone)]
42pub struct ObjectModel {
43 pub name: String,
44 pub def: ObjectDef,
45}
46
47#[derive(Debug, Clone)]
48pub struct FunctionModel {
49 pub name: String,
50 pub def: FunctionDef,
51}
52
53#[derive(Debug, Clone)]
54pub struct CallbackModel {
55 pub name: String,
56 pub def: CallbackDef,
57}
58
59#[derive(Debug, Clone)]
60pub struct CallbackFunctionModel {
61 pub name: String,
62 pub def: CallbackFunctionDef,
63}
64
65#[derive(Debug, Clone)]
66pub struct CallbackInfoModel {
67 pub name: String,
68 pub def: CallbackInfoDef,
69}
70
71#[derive(Debug, Clone)]
72pub struct ConstantModel {
73 pub name: String,
74 pub def: ConstantDef,
75}
76
77#[derive(Debug, Clone)]
78pub struct NativeModel {
79 pub name: String,
80 pub def: NativeType,
81}
82
83#[derive(Debug, Clone)]
84pub struct TypedefModel {
85 pub name: String,
86 pub def: TypedefDef,
87}
88
89#[derive(Debug, Clone)]
90pub struct FunctionPointerModel {
91 pub name: String,
92 pub def: FunctionPointerDef,
93}
94
95impl ApiModel {
96 pub fn from_api(api: &DawnApi) -> Self {
97 let mut enums = Vec::new();
98 let mut bitmasks = Vec::new();
99 let mut structures = Vec::new();
100 let mut objects = Vec::new();
101 let mut functions = Vec::new();
102 let mut callbacks = Vec::new();
103 let mut callback_functions = Vec::new();
104 let mut callback_infos = Vec::new();
105 let mut constants = Vec::new();
106 let mut natives = Vec::new();
107 let mut typedefs = Vec::new();
108 let mut function_pointers = Vec::new();
109
110 for (name, def) in &api.definitions {
111 match def {
112 crate::parser::Definition::Enum(enum_def) => enums.push(EnumModel {
113 name: name.clone(),
114 def: enum_def.clone(),
115 }),
116 crate::parser::Definition::Bitmask(bitmask_def) => bitmasks.push(BitmaskModel {
117 name: name.clone(),
118 def: bitmask_def.clone(),
119 }),
120 crate::parser::Definition::Structure(struct_def) => {
121 structures.push(StructureModel {
122 name: name.clone(),
123 def: struct_def.clone(),
124 })
125 }
126 crate::parser::Definition::Object(object_def) => objects.push(ObjectModel {
127 name: name.clone(),
128 def: object_def.clone(),
129 }),
130 crate::parser::Definition::Function(func_def) => functions.push(FunctionModel {
131 name: name.clone(),
132 def: func_def.clone(),
133 }),
134 crate::parser::Definition::Callback(callback_def) => {
135 callbacks.push(CallbackModel {
136 name: name.clone(),
137 def: callback_def.clone(),
138 })
139 }
140 crate::parser::Definition::CallbackFunction(callback_def) => callback_functions
141 .push(CallbackFunctionModel {
142 name: name.clone(),
143 def: callback_def.clone(),
144 }),
145 crate::parser::Definition::CallbackInfo(callback_info_def) => {
146 callback_infos.push(CallbackInfoModel {
147 name: name.clone(),
148 def: callback_info_def.clone(),
149 })
150 }
151 crate::parser::Definition::Constant(const_def) => constants.push(ConstantModel {
152 name: name.clone(),
153 def: const_def.clone(),
154 }),
155 crate::parser::Definition::Native(native_def) => natives.push(NativeModel {
156 name: name.clone(),
157 def: native_def.clone(),
158 }),
159 crate::parser::Definition::Typedef(typedef_def) => typedefs.push(TypedefModel {
160 name: name.clone(),
161 def: typedef_def.clone(),
162 }),
163 crate::parser::Definition::FunctionPointer(fp_def) => {
164 function_pointers.push(FunctionPointerModel {
165 name: name.clone(),
166 def: fp_def.clone(),
167 })
168 }
169 }
170 }
171
172 enums.sort_by(|a, b| a.name.cmp(&b.name));
173 bitmasks.sort_by(|a, b| a.name.cmp(&b.name));
174 structures.sort_by(|a, b| a.name.cmp(&b.name));
175 objects.sort_by(|a, b| a.name.cmp(&b.name));
176 functions.sort_by(|a, b| a.name.cmp(&b.name));
177 callbacks.sort_by(|a, b| a.name.cmp(&b.name));
178 callback_functions.sort_by(|a, b| a.name.cmp(&b.name));
179 callback_infos.sort_by(|a, b| a.name.cmp(&b.name));
180 constants.sort_by(|a, b| a.name.cmp(&b.name));
181 natives.sort_by(|a, b| a.name.cmp(&b.name));
182 typedefs.sort_by(|a, b| a.name.cmp(&b.name));
183 function_pointers.sort_by(|a, b| a.name.cmp(&b.name));
184
185 let c_prefix = api
186 .metadata
187 .c_prefix
188 .clone()
189 .unwrap_or_else(|| api.metadata.namespace.to_uppercase());
190
191 Self {
192 c_prefix,
193 enums,
194 bitmasks,
195 structures,
196 objects,
197 functions,
198 callbacks,
199 callback_functions,
200 callback_infos,
201 constants,
202 natives,
203 typedefs,
204 function_pointers,
205 }
206 }
207}