Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Dec 11, 2024
1 parent f8124e8 commit d7f2862
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 70 deletions.
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/libraries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn combine_libraries(

// Windows libs are always produced with lower case module names.
let library = ty.method.module_name().to_lowercase();
let impl_map = ty.method.impl_map().expect("ImplMap not found");
let impl_map = ty.method.impl_map().unwrap();
let flags = impl_map.flags();
let name = impl_map.import_name().to_string();

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/tables/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl std::fmt::Debug for Constant {

impl Constant {
pub fn ty(&self) -> Type {
Type::from_element_type(self.usize(0)).unwrap_or_else(|| panic!("`Constant` type invalid"))
Type::from_element_type(self.usize(0)).unwrap()
}

pub fn value(&self) -> Value {
Expand Down
8 changes: 3 additions & 5 deletions crates/libs/bindgen/src/tables/method_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ impl MethodDef {
if !param_is_output {
if let Some(attribute) = param.find_attribute("AssociatedEnumAttribute") {
if let Some((_, Value::Str(name))) = attribute.args().first() {
if let Some(overload) =
param.reader().with_full_name(namespace, name).next()
{
ty = Type::PrimitiveOrEnum(Box::new(ty), Box::new(overload));
}
let overload = param.reader().unwrap_full_name(namespace, name);

ty = Type::PrimitiveOrEnum(Box::new(ty), Box::new(overload));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/tables/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl TypeDef {
}

pub fn underlying_type(&self) -> Type {
let field = self.fields().next().expect("field not found");
let field = self.fields().next().unwrap();
if let Some(constant) = field.constant() {
constant.ty()
} else {
Expand Down
14 changes: 5 additions & 9 deletions crates/libs/bindgen/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,9 @@ impl Class {
break;
}

let Some(Type::Class(base)) = reader
.with_full_name(extends.namespace(), extends.name())
.next()
let Type::Class(base) = reader.unwrap_full_name(extends.namespace(), extends.name())
else {
panic!("Type not found");
panic!("type not found: {extends}");
};

def = base.def;
Expand Down Expand Up @@ -319,17 +317,15 @@ impl Class {

for (_, arg) in attribute.args() {
if let Value::TypeName(tn) = arg {
let Some(Type::Interface(mut interface)) = self
let Type::Interface(mut interface) = self
.def
.reader()
.with_full_name(tn.namespace(), tn.name())
.next()
.unwrap_full_name(tn.namespace(), tn.name())
else {
panic!("Type not found");
panic!("type not found: {tn}");
};

interface.kind = kind;

set.push(interface);
break;
}
Expand Down
10 changes: 3 additions & 7 deletions crates/libs/bindgen/src/types/cpp_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,10 @@ impl CppEnum {
pub fn dependencies(&self, dependencies: &mut TypeMap) {
if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
if let Some(ty) = self
.def
self.def
.reader()
.with_full_name(self.def.namespace(), type_name)
.next()
{
ty.dependencies(dependencies);
}
.unwrap_full_name(self.def.namespace(), type_name)
.dependencies(dependencies);
}
}
}
Expand Down
35 changes: 13 additions & 22 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CppFn {
pub fn write_link(&self, writer: &Writer, underlying_types: bool) -> TokenStream {
let name = self.method.name();
let library = self.method.module_name().to_lowercase();
let impl_map = self.method.impl_map().expect("ImplMap not found");
let impl_map = self.method.impl_map().unwrap();
let mut symbol = Some(impl_map.import_name());

if symbol == Some(name) {
Expand Down Expand Up @@ -263,28 +263,19 @@ impl CppFn {
.signature(self.namespace, &[])
.dependencies(dependencies);

match self.method.name() {
"GetWindowLongPtrA" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "GetWindowLongA")
.dependencies(dependencies),
"GetWindowLongPtrW" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "GetWindowLongW")
.dependencies(dependencies),
"SetWindowLongPtrA" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "SetWindowLongA")
.dependencies(dependencies),
"SetWindowLongPtrW" => self
.method
let dependency = match self.method.name() {
"GetWindowLongPtrA" => Some("GetWindowLongA"),
"GetWindowLongPtrW" => Some("GetWindowLongW"),
"SetWindowLongPtrA" => Some("SetWindowLongA"),
"SetWindowLongPtrW" => Some("SetWindowLongW"),
_ => None,
};

if let Some(dependency) = dependency {
self.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "SetWindowLongW")
.dependencies(dependencies),
_ => {}
.unwrap_full_name(self.namespace, dependency)
.dependencies(dependencies);
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,10 @@ impl CppStruct {

if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
if let Some(ty) = self
.def
self.def
.reader()
.with_full_name(self.def.namespace(), type_name)
.next()
{
ty.dependencies(dependencies);
}
.unwrap_full_name(self.def.namespace(), type_name)
.dependencies(dependencies);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/winmd/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Blob {
match self.read_u8() {
0 => false,
1 => true,
_ => panic!("illegal bool value"),
_ => panic!(),
}
}

Expand Down Expand Up @@ -175,7 +175,7 @@ impl Blob {
Type::U32 => Value::U32(self.read_u32()),
Type::I64 => Value::I64(self.read_i64()),
Type::U64 => Value::U64(self.read_u64()),
_ => panic!("type is not an integer"),
_ => panic!(),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/winmd/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl Reader {
reader
}

#[track_caller]
pub fn unwrap_full_name(&self, namespace: &str, name: &str) -> Type {
if let Some(ty) = self.with_full_name(namespace, name).next() {
ty
Expand Down
24 changes: 10 additions & 14 deletions crates/libs/bindgen/src/writer/cpp_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,18 @@ impl Writer {

if let Some(attribute) = def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
if let Some(ty) = def
.reader()
.with_full_name(def.namespace(), type_name)
.next()
{
let ty = ty.write_name(self);
let ty = def.reader().unwrap_full_name(def.namespace(), type_name);

result.combine(quote! {
impl windows_core::imp::CanInto<#ty> for #name {}
impl From<#name> for #ty {
fn from(value: #name) -> Self {
Self(value.0)
}
let ty = ty.write_name(self);

result.combine(quote! {
impl windows_core::imp::CanInto<#ty> for #name {}
impl From<#name> for #ty {
fn from(value: #name) -> Self {
Self(value.0)
}
});
}
}
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tools/bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,6 @@ fn write_lib() {
cmd.arg("lib.rs");

if !cmd.status().unwrap().success() {
panic!("Failed to run rustfmt");
panic!("failed to run rustfmt");
}
}

0 comments on commit d7f2862

Please sign in to comment.