Skip to content

Commit

Permalink
Merge pull request #992 from MarijnS95/simplification-refactor
Browse files Browse the repository at this point in the history
Simplify `match` blocks into `?` operator using SSR
  • Loading branch information
sdroege authored Nov 16, 2020
2 parents a169b44 + cf16130 commit 9f4a439
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 195 deletions.
126 changes: 65 additions & 61 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions src/analysis/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,13 @@ pub fn class(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info>
info!("Analyzing class {}", obj.name);
let full_name = obj.name.clone();

let class_tid = match env.library.find_type(0, &full_name) {
Some(tid) => tid,
None => return None,
};
let class_tid = env.library.find_type(0, &full_name)?;

let type_ = env.type_(class_tid);

let name: String = split_namespace_name(&full_name).1.into();

let klass: &library::Class = match type_.maybe_ref() {
Some(klass) => klass,
None => return None,
};
let klass: &library::Class = type_.maybe_ref()?;

let mut imports = Imports::with_defined(&env.library, &name);
imports.add("glib::translate::*");
Expand Down Expand Up @@ -226,19 +220,13 @@ pub fn interface(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<I
info!("Analyzing interface {}", obj.name);
let full_name = obj.name.clone();

let iface_tid = match env.library.find_type(0, &full_name) {
Some(tid) => tid,
None => return None,
};
let iface_tid = env.library.find_type(0, &full_name)?;

let type_ = env.type_(iface_tid);

let name: String = split_namespace_name(&full_name).1.into();

let iface: &library::Interface = match type_.maybe_ref() {
Some(iface) => iface,
None => return None,
};
let iface: &library::Interface = type_.maybe_ref()?;

let mut imports = Imports::with_defined(&env.library, &name);
imports.add("glib::translate::*");
Expand Down
10 changes: 2 additions & 8 deletions src/analysis/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@ pub fn new(env: &Env, obj: &GObject) -> Option<Info> {
info!("Analyzing record {}", obj.name);
let full_name = obj.name.clone();

let record_tid = match env.library.find_type(0, &full_name) {
Some(tid) => tid,
None => return None,
};
let record_tid = env.library.find_type(0, &full_name)?;

let type_ = env.type_(record_tid);

let name: String = split_namespace_name(&full_name).1.into();

let record: &library::Record = match type_.maybe_ref() {
Some(record) => record,
None => return None,
};
let record: &library::Record = type_.maybe_ref()?;

let is_boxed = obj.use_boxed_functions || RecordType::of(&record) == RecordType::AutoBoxed;

Expand Down
32 changes: 16 additions & 16 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ fn generate_enum(
}
writeln!(
w,
"{}",
" #[doc(hidden)]
"\
#[doc(hidden)]
__Unknown(i32),
}
}}
"
)?;

Expand Down Expand Up @@ -208,13 +208,13 @@ impl ToGlib for {name} {{
enum_.name, member.name, sys_crate_name, member.c_name
)?;
}
writeln!(w, "\t\t\t{}::__Unknown(value) => value", enum_.name)?;
writeln!(w, "\t\t\t{}::__Unknown(value) => value,", enum_.name)?;
writeln!(
w,
"{}",
" }
}
}
"\
}}
}}
}}
"
)?;

Expand Down Expand Up @@ -248,10 +248,10 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
writeln!(w, "\t\t\tvalue => {}::__Unknown(value),", enum_.name)?;
writeln!(
w,
"{}",
" }
}
}
"\
}}
}}
}}
"
)?;

Expand Down Expand Up @@ -319,10 +319,10 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{

writeln!(
w,
"{}",
" }
}
}
"\
}}
}}
}}
"
)?;
}
Expand Down
6 changes: 2 additions & 4 deletions src/codegen/sys/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ fn prepare_ctype(env: &Env, ns: &Namespace, t: &Type) -> Option<CType> {
if env.type_status_sys(&full_name).ignored() {
return None;
}
let name = match t.get_glib_name() {
None => return None,
Some(name) => name,
};
let name = t.get_glib_name()?;

if is_name_made_up(name) {
return None;
}
Expand Down
Loading

0 comments on commit 9f4a439

Please sign in to comment.