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,34 +765,95 @@ pub fn generate_enum(_enum: &vkxml::Enumeration) -> EnumType {
}; };
EnumType::Bitflags(q) EnumType::Bitflags(q)
} else { } else {
let variants = _enum.elements.iter().filter_map(|elem| { let q = match _name.as_str() {
let (variant_name, value) = match *elem { "Result" => generate_result(&_name, _enum),
vkxml::EnumerationElement::Enum(ref constant) => { _ => {
let c = Constant::from_constant(constant); let ident = Ident::from(_name.as_str());
//println!("value {:?}", c.value()); let variants = _enum.elements.iter().filter_map(|elem| {
(constant.name.as_str(), c.to_tokens()) let (variant_name, value) = match *elem {
} vkxml::EnumerationElement::Enum(ref constant) => {
_ => { let c = Constant::from_constant(constant);
return None; //println!("value {:?}", c.value());
} (constant.name.as_str(), c.to_tokens())
}; }
_ => {
return None;
}
};
let variant_ident = to_variant_ident(&_name, variant_name); let variant_ident = to_variant_ident(&_name, variant_name);
Some(quote!{ Some(quote!{
#variant_ident = #value #variant_ident = #value
}) })
}); });
let ident = Ident::from(_name.as_str()); quote!{
let q = quote!{ #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, Debug)] #[repr(C)]
#[repr(C)] pub enum #ident {
pub enum #ident { #(#variants,)*
#(#variants,)* }
}
} }
}; };
EnumType::Enum(q) 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 trait StructExt {}
pub fn generate_struct(_struct: &vkxml::Struct) -> Tokens { pub fn generate_struct(_struct: &vkxml::Struct) -> Tokens {
let name = to_type_tokens(&_struct.name, None); 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!{pub #param_ident: #param_ty_tokens}
}); });
quote!{ quote!{
//#[derive(Clone, Debug)] #[derive(Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct #name { pub struct #name {
#(#params,)* #(#params,)*