@@ -10,13 +10,13 @@ import (
1010 "text/template"
1111)
1212
13- type Data struct {
14- Name string
15- ExtSuffix string
13+ type Generator struct {
14+ ExtSuffix string
15+ HeaderName string
1616 * Yml
1717}
1818
19- func (d * Data ) GenCHeader (dst io.Writer ) error {
19+ func (g * Generator ) Gen (dst io.Writer ) error {
2020 t := template .
2121 New ("" ).
2222 Funcs (template.FuncMap {
@@ -27,43 +27,43 @@ func (d *Data) GenCHeader(dst io.Writer) error {
2727 "ConstantCase" : ConstantCase ,
2828 "PascalCase" : PascalCase ,
2929 "CamelCase" : CamelCase ,
30- "CType" : d .CType ,
31- "CValue" : d .CValue ,
32- "EnumValue" : d .EnumValue ,
33- "BitflagValue" : d .BitflagValue ,
30+ "CType" : g .CType ,
31+ "CValue" : g .CValue ,
32+ "EnumValue" : g .EnumValue ,
33+ "BitflagValue" : g .BitflagValue ,
3434 "IsArray" : func (typ string ) bool {
3535 return arrayTypeRegexp .Match ([]byte (typ ))
3636 },
3737 "ArrayType" : func (typ string , pointer PointerType ) string {
3838 matches := arrayTypeRegexp .FindStringSubmatch (typ )
3939 if len (matches ) == 2 {
40- return d .CType (matches [1 ], pointer , "" )
40+ return g .CType (matches [1 ], pointer , "" )
4141 }
4242 return ""
4343 },
4444 "Singularize" : Singularize ,
4545 "IsLast" : func (i int , s any ) bool { return i == reflect .ValueOf (s ).Len ()- 1 },
4646 "FunctionReturns" : func (f Function ) string {
4747 if f .Returns != nil {
48- return d .CType (f .Returns .Type , f .Returns .Pointer , "" )
48+ return g .CType (f .Returns .Type , f .Returns .Pointer , "" )
4949 }
5050 return "void"
5151 },
52- "FunctionArgs" : d .FunctionArgs ,
53- "CallbackArgs" : d .CallbackArgs ,
54- "StructMember" : d .StructMember ,
52+ "FunctionArgs" : g .FunctionArgs ,
53+ "CallbackArgs" : g .CallbackArgs ,
54+ "StructMember" : g .StructMember ,
5555 })
5656 t , err := t .Parse (tmpl )
5757 if err != nil {
5858 return fmt .Errorf ("GenCHeader: failed to parse template: %w" , err )
5959 }
60- if err := t .Execute (dst , d ); err != nil {
60+ if err := t .Execute (dst , g ); err != nil {
6161 return fmt .Errorf ("GenCHeader: failed to execute template: %w" , err )
6262 }
6363 return nil
6464}
6565
66- func (d * Data ) CValue (s string ) (string , error ) {
66+ func (g * Generator ) CValue (s string ) (string , error ) {
6767 switch s {
6868 case "usize_max" :
6969 return "SIZE_MAX" , nil
@@ -95,7 +95,7 @@ func (d *Data) CValue(s string) (string, error) {
9595 }
9696}
9797
98- func (d * Data ) CType (typ string , pointerType PointerType , suffix string ) string {
98+ func (g * Generator ) CType (typ string , pointerType PointerType , suffix string ) string {
9999 appendModifiers := func (s string , pointerType PointerType ) string {
100100 var sb strings.Builder
101101 sb .WriteString (s )
@@ -153,12 +153,12 @@ func (d *Data) CType(typ string, pointerType PointerType, suffix string) string
153153 }
154154}
155155
156- func (d * Data ) FunctionArgs (f Function , o * Object ) string {
156+ func (g * Generator ) FunctionArgs (f Function , o * Object ) string {
157157 sb := & strings.Builder {}
158158 if o != nil {
159159 var typeSuffix string
160160 if o .Namespace == "" {
161- typeSuffix = ConstantCase (d .ExtSuffix )
161+ typeSuffix = ConstantCase (g .ExtSuffix )
162162 } else if o .Namespace != "webgpu" {
163163 typeSuffix = ConstantCase (o .Namespace )
164164 }
@@ -177,16 +177,16 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
177177 }
178178 var typeSuffix string
179179 if arg .Namespace == "" {
180- typeSuffix = ConstantCase (d .ExtSuffix )
180+ typeSuffix = ConstantCase (g .ExtSuffix )
181181 } else if arg .Namespace != "webgpu" {
182182 typeSuffix = ConstantCase (arg .Namespace )
183183 }
184184 matches := arrayTypeRegexp .FindStringSubmatch (arg .Type )
185185 if len (matches ) == 2 {
186186 fmt .Fprintf (sb , "size_t %sCount, " , CamelCase (Singularize (arg .Name )))
187- fmt .Fprintf (sb , "%s %s" , d .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
187+ fmt .Fprintf (sb , "%s %s" , g .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
188188 } else {
189- fmt .Fprintf (sb , "%s %s" , d .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
189+ fmt .Fprintf (sb , "%s %s" , g .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
190190 }
191191 if i != len (f .Args )- 1 {
192192 sb .WriteString (", " )
@@ -204,15 +204,15 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
204204 return sb .String ()
205205}
206206
207- func (d * Data ) CallbackArgs (f Function ) string {
207+ func (g * Generator ) CallbackArgs (f Function ) string {
208208 sb := & strings.Builder {}
209209 for _ , arg := range f .ReturnsAsync {
210210 if arg .Optional {
211211 sb .WriteString ("WGPU_NULLABLE " )
212212 }
213213 var typeSuffix string
214214 if arg .Namespace == "" {
215- typeSuffix = ConstantCase (d .ExtSuffix )
215+ typeSuffix = ConstantCase (g .ExtSuffix )
216216 } else if arg .Namespace != "webgpu" {
217217 typeSuffix = ConstantCase (arg .Namespace )
218218 }
@@ -223,16 +223,16 @@ func (d *Data) CallbackArgs(f Function) string {
223223 matches := arrayTypeRegexp .FindStringSubmatch (arg .Type )
224224 if len (matches ) == 2 {
225225 fmt .Fprintf (sb , "size_t %sCount, " , CamelCase (Singularize (arg .Name )))
226- fmt .Fprintf (sb , "%s%s %s, " , structPrefix , d .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
226+ fmt .Fprintf (sb , "%s%s %s, " , structPrefix , g .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
227227 } else {
228- fmt .Fprintf (sb , "%s%s %s, " , structPrefix , d .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
228+ fmt .Fprintf (sb , "%s%s %s, " , structPrefix , g .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
229229 }
230230 }
231231 sb .WriteString ("WGPU_NULLABLE void * userdata" )
232232 return sb .String ()
233233}
234234
235- func (d * Data ) EnumValue (prefix string , e Enum , entryIndex int ) (string , error ) {
235+ func (g * Generator ) EnumValue (prefix string , e Enum , entryIndex int ) (string , error ) {
236236 var entryValue uint16
237237 entry := e .Entries [entryIndex ]
238238 if entry .Value == "" {
@@ -256,14 +256,14 @@ func (d *Data) EnumValue(prefix string, e Enum, entryIndex int) (string, error)
256256 return fmt .Sprintf ("%s%.4X" , prefix , entryValue ), nil
257257}
258258
259- func (d * Data ) BitflagValue (b Bitflag , entryIndex int ) (string , error ) {
259+ func (g * Generator ) BitflagValue (b Bitflag , entryIndex int ) (string , error ) {
260260 entry := b .Entries [entryIndex ]
261261 var entryValue string
262262 if len (entry .ValueCombination ) > 0 {
263263 for valueIndex , v := range entry .ValueCombination {
264264 entryValue += "WGPU" + PascalCase (b .Name ) + "_" + PascalCase (v )
265- if d .ExtSuffix != "" {
266- entryValue += "_" + d .ExtSuffix
265+ if g .ExtSuffix != "" {
266+ entryValue += "_" + g .ExtSuffix
267267 }
268268 if valueIndex != len (entry .ValueCombination )- 1 {
269269 entryValue += " | "
@@ -293,24 +293,24 @@ func (d *Data) BitflagValue(b Bitflag, entryIndex int) (string, error) {
293293 return entryValue , nil
294294}
295295
296- func (d * Data ) StructMember (s Struct , memberIndex int ) (string , error ) {
296+ func (g * Generator ) StructMember (s Struct , memberIndex int ) (string , error ) {
297297 member := s .Members [memberIndex ]
298298 sb := & strings.Builder {}
299299 if member .Optional {
300300 sb .WriteString ("WGPU_NULLABLE " )
301301 }
302302 var typeSuffix string
303303 if member .Namespace == "" {
304- typeSuffix = ConstantCase (d .ExtSuffix )
304+ typeSuffix = ConstantCase (g .ExtSuffix )
305305 } else if member .Namespace != "webgpu" {
306306 typeSuffix = ConstantCase (member .Namespace )
307307 }
308308 matches := arrayTypeRegexp .FindStringSubmatch (member .Type )
309309 if len (matches ) == 2 {
310310 fmt .Fprintf (sb , "size_t %sCount;\n " , CamelCase (Singularize (member .Name )))
311- fmt .Fprintf (sb , " %s %s;" , d .CType (matches [1 ], member .Pointer , typeSuffix ), CamelCase (member .Name ))
311+ fmt .Fprintf (sb , " %s %s;" , g .CType (matches [1 ], member .Pointer , typeSuffix ), CamelCase (member .Name ))
312312 } else {
313- fmt .Fprintf (sb , "%s %s;" , d .CType (member .Type , member .Pointer , typeSuffix ), CamelCase (member .Name ))
313+ fmt .Fprintf (sb , "%s %s;" , g .CType (member .Type , member .Pointer , typeSuffix ), CamelCase (member .Name ))
314314 }
315315 return sb .String (), nil
316316}
0 commit comments