@@ -55,6 +55,12 @@ func writeRustBaseTypeDefinitions(componentdefinition ComponentDefinition, w Lan
5555 w .Writeln ("" )
5656 w .Writeln ("" )
5757
58+ w .Writeln ("/*************************************************************************************************************************" )
59+ w .Writeln (" Basic pointers definition for %s" , NameSpace )
60+ w .Writeln ("**************************************************************************************************************************/" )
61+ w .Writeln ("" )
62+ w .Writeln ("type Handle = std::ffi::c_void" )
63+
5864 if len (componentdefinition .Enums ) > 0 {
5965 w .Writeln ("/*************************************************************************************************************************" )
6066 w .Writeln (" Enum definitions for %s" , NameSpace )
@@ -101,6 +107,33 @@ func writeRustBaseTypeDefinitions(componentdefinition ComponentDefinition, w Lan
101107 }
102108 }
103109
110+ if len (componentdefinition .Functions ) > 0 {
111+ w .Writeln ("/*************************************************************************************************************************" )
112+ w .Writeln (" Function type definitions for %s" , NameSpace )
113+ w .Writeln ("**************************************************************************************************************************/" )
114+ w .Writeln ("" )
115+ for i := 0 ; i < len (componentdefinition .Functions ); i ++ {
116+ funcinfo := componentdefinition .Functions [i ]
117+ w .Writeln ("// %s" , funcinfo .FunctionDescription )
118+ w .Writeln ("//" )
119+ parameterString := ""
120+ for j := 0 ; j < len (funcinfo .Params ); j ++ {
121+ RustParameters , err := generatePlainRustParameters (funcinfo .Params [j ])
122+ RustParameter := RustParameters [0 ]
123+ if err != nil {
124+ return err
125+ }
126+ w .Writeln ("// %s" , RustParameter .ParamComment )
127+ if j == 0 {
128+ parameterString += fmt .Sprintf ("%s : %s" , RustParameter .ParamName , RustParameter .ParamType )
129+ } else {
130+ parameterString += fmt .Sprintf (", %s : %s" , RustParameter .ParamName , RustParameter .ParamType )
131+ }
132+ }
133+ w .Writeln ("type %s = unsafe extern \" C\" fn(%s);" , funcinfo .FunctionName , parameterString )
134+ }
135+ }
136+
104137 return nil
105138}
106139
@@ -151,3 +184,127 @@ func writeRustMemberLine(member ComponentDefinitionMember, w LanguageWriter, Str
151184 }
152185 return nil
153186}
187+
188+ // CParameter is a handy representation of a function parameter in C
189+ type RustParameter struct {
190+ ParamType string
191+ ParamName string
192+ ParamComment string
193+ }
194+
195+ func generatePlainRustParameters (param ComponentDefinitionParam ) ([]RustParameter , error ) {
196+
197+ }
198+
199+ func generateRustParameterType (param ComponentDefinitionParam , isPlain bool ) (string , error ) {
200+ RustParamTypeName := ""
201+ ParamTypeName := param .ParamType
202+ ParamClass := param .ParamClass
203+ switch ParamTypeName {
204+ case "uint8" :
205+ RustParamTypeName = "u8"
206+
207+ case "uint16" :
208+ RustParamTypeName = "u16"
209+
210+ case "uint32" :
211+ RustParamTypeName = "u32"
212+
213+ case "uint64" :
214+ RustParamTypeName = "u64"
215+
216+ case "int8" :
217+ RustParamTypeName = "i8"
218+
219+ case "int16" :
220+ RustParamTypeName = "i16"
221+
222+ case "int32" :
223+ RustParamTypeName = "i32"
224+
225+ case "int64" :
226+ RustParamTypeName = "i64"
227+
228+ case "bool" :
229+ if isPlain {
230+ RustParamTypeName = "u8"
231+ } else {
232+ RustParamTypeName = "bool"
233+ }
234+
235+ case "single" :
236+ RustParamTypeName = "f32"
237+
238+ case "double" :
239+ RustParamTypeName = "f64"
240+
241+ case "pointer" :
242+ RustParamTypeName = "c_void"
243+
244+ case "string" :
245+ if isPlain {
246+ RustParamTypeName = "*mut char"
247+ } else {
248+ // TODO
249+ return "" , fmt .Errorf ("Not yet handled" )
250+ }
251+
252+ case "enum" :
253+ if isPlain {
254+ RustParamTypeName = fmt .Sprintf ("u16" )
255+ } else {
256+ // TODO
257+ return "" , fmt .Errorf ("Not yet handled" )
258+ }
259+
260+ case "functiontype" :
261+ if isPlain {
262+ RustParamTypeName = fmt .Sprintf ("%s" , ParamClass )
263+ } else {
264+ // TODO
265+ return "" , fmt .Errorf ("Not yet handled" )
266+ }
267+
268+ case "struct" :
269+ if isPlain {
270+ RustParamTypeName = fmt .Sprintf ("*mut %s" , ParamClass )
271+ } else {
272+ // TODO
273+ return "" , fmt .Errorf ("Not yet handled" )
274+ }
275+
276+ case "basicarray" :
277+ basicTypeName , err := generateRustParameterType (param , isPlain )
278+ if err != nil {
279+ return "" , err
280+ }
281+
282+ if isPlain {
283+ RustParamTypeName = fmt .Sprintf ("*mut %s" , basicTypeName )
284+ } else {
285+ // TODO
286+ return "" , fmt .Errorf ("Not yet handled" )
287+ }
288+
289+ case "structarray" :
290+ if isPlain {
291+ RustParamTypeName = fmt .Sprintf ("*mut %s" , ParamClass )
292+ } else {
293+ // TODO
294+ return "" , fmt .Errorf ("Not yet handled" )
295+ }
296+
297+ case "class" , "optionalclass" :
298+ if isPlain {
299+ RustParamTypeName = fmt .Sprintf ("Handle" )
300+ } else {
301+ // TODO
302+ return "" , fmt .Errorf ("Not yet handled" )
303+ }
304+
305+ default :
306+ return "" , fmt .Errorf ("invalid parameter type \" %s\" for Pascal parameter" , ParamTypeName )
307+ }
308+
309+ return RustParamTypeName , nil
310+ }
0 commit comments