Add custom Result impl

This commit is contained in:
Maik Klein 2018-07-07 10:43:05 +02:00
parent 9f14e404d7
commit 4584a8af2a

View file

@ -765,6 +765,10 @@ pub fn generate_enum(_enum: &vkxml::Enumeration) -> EnumType {
};
EnumType::Bitflags(q)
} else {
let q = match _name.as_str() {
"Result" => generate_result(&_name, _enum),
_ => {
let ident = Ident::from(_name.as_str());
let variants = _enum.elements.iter().filter_map(|elem| {
let (variant_name, value) = match *elem {
vkxml::EnumerationElement::Enum(ref constant) => {
@ -782,17 +786,74 @@ pub fn generate_enum(_enum: &vkxml::Enumeration) -> EnumType {
#variant_ident = #value
})
});
let ident = Ident::from(_name.as_str());
let q = quote!{
#[derive(Copy, Clone, Debug)]
quote!{
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum #ident {
#(#variants,)*
}
}
}
};
EnumType::Enum(q)
}
}
pub fn generate_result(name: &str, _enum: &vkxml::Enumeration) -> Tokens {
let ident = Ident::from(name);
let variants = _enum.elements.iter().filter_map(|elem| {
let (variant_name, value) = match *elem {
vkxml::EnumerationElement::Enum(ref constant) => {
let c = Constant::from_constant(constant);
//println!("value {:?}", c.value());
(constant.name.as_str(), c.to_tokens())
}
_ => {
return None;
}
};
let variant_ident = to_variant_ident(&name, variant_name);
Some(quote!{
#variant_ident = #value
})
});
let notation = _enum.elements.iter().filter_map(|elem| {
let (variant_name, notation) = match *elem {
vkxml::EnumerationElement::Enum(ref constant) => (
constant.name.as_str(),
constant.notation.as_ref().map(|s| s.as_str()).unwrap_or(""),
),
_ => {
return None;
}
};
let variant_ident = to_variant_ident(&name, variant_name);
Some(quote!{
#ident::#variant_ident => write!(fmt, #notation)
})
});
quote!{
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum #ident {
#(#variants,)*
}
impl ::std::error::Error for #ident {
fn description(&self) -> &str {
"vk::Result"
}
}
impl ::std::fmt::Display for #ident {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
writeln!(fmt, "vk::Result::{:?}", self)?;
match self {
#(#notation),*
}
}
}
}
}
pub trait StructExt {}
pub fn generate_struct(_struct: &vkxml::Struct) -> Tokens {
let name = to_type_tokens(&_struct.name, None);
@ -809,7 +870,7 @@ pub fn generate_struct(_struct: &vkxml::Struct) -> Tokens {
quote!{pub #param_ident: #param_ty_tokens}
});
quote!{
//#[derive(Clone, Debug)]
#[derive(Copy, Clone)]
#[repr(C)]
pub struct #name {
#(#params,)*