1 | // Copyright 2015 Brendan Zabarauskas and the gl-rs developers |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | use registry::{Cmd, Enum, Registry}; |
16 | use std::io; |
17 | use Api; |
18 | |
19 | pub mod debug_struct_gen; |
20 | pub mod global_gen; |
21 | pub mod static_gen; |
22 | pub mod static_struct_gen; |
23 | pub mod struct_gen; |
24 | |
25 | /// Trait for a bindings generator. |
26 | /// |
27 | /// See https://github.com/brendanzab/gl-rs/tree/master/gl_generator#generator-types |
28 | pub trait Generator { |
29 | /// Builds the GL bindings. |
30 | fn write<W>(&self, registry: &Registry, dest: &mut W) -> io::Result<()> |
31 | where |
32 | W: io::Write; |
33 | } |
34 | |
35 | pub fn gen_struct_name(api: Api) -> &'static str { |
36 | match api { |
37 | Api::Gl => "Gl" , |
38 | Api::Glx => "Glx" , |
39 | Api::Wgl => "Wgl" , |
40 | Api::Egl => "Egl" , |
41 | Api::GlCore => "GlCore" , |
42 | Api::Gles1 => "Gles1" , |
43 | Api::Gles2 => "Gles2" , |
44 | Api::Glsc2 => "Glsc2" , |
45 | } |
46 | } |
47 | |
48 | /// This function generates a `const name: type = value;` item. |
49 | pub fn gen_enum_item<W>(enm: &Enum, types_prefix: &str, dest: &mut W) -> io::Result<()> |
50 | where |
51 | W: io::Write, |
52 | { |
53 | writeln!(dest, |
54 | "#[allow(dead_code, non_upper_case_globals)] pub const {ident}: {types_prefix}{ty} = {value}{cast_suffix};" , |
55 | ident = enm.ident, |
56 | types_prefix = if enm.ty == "&'static str" { "" } else { types_prefix }, |
57 | ty = enm.ty, |
58 | value = enm.value, |
59 | cast_suffix = match enm.cast { |
60 | true => format!(" as {}{}" , types_prefix, enm.ty), |
61 | false => String::new(), |
62 | }, |
63 | ) |
64 | } |
65 | |
66 | /// Generates all the type aliases for a namespace. |
67 | /// |
68 | /// Aliases are either `pub type = ...` or `#[repr(C)] pub struct ... { ... }` and contain all the |
69 | /// things that we can't obtain from the XML files. |
70 | pub fn gen_types<W>(api: Api, dest: &mut W) -> io::Result<()> |
71 | where |
72 | W: io::Write, |
73 | { |
74 | if let Api::Egl = api { |
75 | try!(writeln!(dest, " {}" , include_str!("templates/types/egl.rs" ))); |
76 | return Ok(()); |
77 | } |
78 | |
79 | try!(writeln!(dest, " {}" , include_str!("templates/types/gl.rs" ))); |
80 | |
81 | match api { |
82 | Api::Glx => try!(writeln!(dest, " {}" , include_str!("templates/types/glx.rs" ))), |
83 | Api::Wgl => try!(writeln!(dest, " {}" , include_str!("templates/types/wgl.rs" ))), |
84 | _ => {}, |
85 | } |
86 | |
87 | Ok(()) |
88 | } |
89 | |
90 | /// Generates the list of Rust `Arg`s that a `Cmd` requires. |
91 | pub fn gen_parameters(cmd: &Cmd, with_idents: bool, with_types: bool) -> Vec<String> { |
92 | cmdimpl Iterator .params |
93 | .iter() |
94 | .map(|binding: &Binding| { |
95 | // returning |
96 | if with_idents && with_types { |
97 | format!(" {}: {}" , binding.ident, binding.ty) |
98 | } else if with_types { |
99 | format!(" {}" , binding.ty) |
100 | } else if with_idents { |
101 | format!(" {}" , binding.ident) |
102 | } else { |
103 | panic!() |
104 | } |
105 | }) |
106 | .collect() |
107 | } |
108 | |
109 | /// Generates the native symbol name of a `Cmd`. |
110 | /// |
111 | /// Example results: `"glClear"`, `"wglCreateContext"`, etc. |
112 | pub fn gen_symbol_name(api: Api, cmd: &str) -> String { |
113 | match api { |
114 | Api::Gl | Api::GlCore | Api::Gles1 | Api::Gles2 | Api::Glsc2 => format!("gl {}" , cmd), |
115 | Api::Glx => format!("glX {}" , cmd), |
116 | Api::Wgl => format!("wgl {}" , cmd), |
117 | Api::Egl => format!("egl {}" , cmd), |
118 | } |
119 | } |
120 | |