use Api;
use registry::{Enum, Registry, Cmd};
use std::io;
pub mod debug_struct_gen;
pub mod global_gen;
pub mod static_gen;
pub mod struct_gen;
pub mod static_struct_gen;
pub trait Generator {
fn write<W>(&self, registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write;
}
pub fn gen_struct_name(api: Api) -> &'static str {
match api {
Api::Gl => "Gl",
Api::Glx => "Glx",
Api::Wgl => "Wgl",
Api::Egl => "Egl",
Api::GlCore => "GlCore",
Api::Gles1 => "Gles1",
Api::Gles2 => "Gles2",
}
}
pub fn gen_enum_item<W>(enm: &Enum, types_prefix: &str, dest: &mut W) -> io::Result<()>
where W: io::Write
{
writeln!(dest,
"#[allow(dead_code, non_upper_case_globals)] pub const {ident}: {types_prefix}{ty} = {value}{cast_suffix};",
ident = enm.ident,
types_prefix = if enm.ty == "&'static str" { "" } else { types_prefix },
ty = enm.ty,
value = enm.value,
cast_suffix = match enm.cast {
true => format!(" as {}{}", types_prefix, enm.ty),
false => String::new(),
},
)
}
pub fn gen_types<W>(api: Api, dest: &mut W) -> io::Result<()>
where W: io::Write
{
try!(writeln!(dest, "{}", include_str!("templates/types/gl.rs")));
match api {
Api::Glx => try!(writeln!(dest, "{}", include_str!("templates/types/glx.rs"))),
Api::Wgl => try!(writeln!(dest, "{}", include_str!("templates/types/wgl.rs"))),
Api::Egl => try!(writeln!(dest, "{}", include_str!("templates/types/egl.rs"))),
_ => {}
}
Ok(())
}
pub fn gen_parameters(cmd: &Cmd, with_idents: bool, with_types: bool) -> Vec<String> {
cmd.params
.iter()
.map(|binding| {
if with_idents && with_types {
format!("{}: {}", binding.ident, binding.ty)
} else if with_types {
format!("{}", binding.ty)
} else if with_idents {
format!("{}", binding.ident)
} else {
panic!()
}
})
.collect()
}
pub fn gen_symbol_name(api: Api, cmd: &str) -> String {
match api {
Api::Gl | Api::GlCore | Api::Gles1 | Api::Gles2 => format!("gl{}", cmd),
Api::Glx => format!("glX{}", cmd),
Api::Wgl => format!("wgl{}", cmd),
Api::Egl => format!("egl{}", cmd),
}
}