Skip to content

Commit

Permalink
Merge pull request #1505 from gtk-rs/bilelmoussaoui/random
Browse files Browse the repository at this point in the history
Make auto builders/traits/functions `pub(crate)`
  • Loading branch information
sdroege authored Aug 27, 2023
2 parents c9d6716 + 6b6fabf commit 0f2c059
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 119 deletions.
2 changes: 1 addition & 1 deletion book/src/tutorial/handling_errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ A little tip about reexporting manual traits: in `gtk3-rs`, we create a `src/pre
The `src/prelude.rs` file looks like this:

```rust
pub use auto::traits::*;
pub use crate::auto::traits::*;
pub use region::RegionExtManual;
```

Expand Down
4 changes: 3 additions & 1 deletion book/src/tutorial/high_level_rust_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ These are the objects we will add to the `manual` array in the following steps.
These are global functions that were not generated.
To fix it, we just add `"NameOfYourLibrary.*"` to the `generate` array in the Git.toml and add the following line to your src/lib.rs file:
```rust
pub use auto::functions::*;
pub mod functions {
pub use super::auto::functions::*;
}
```

## Generating the code
Expand Down
20 changes: 8 additions & 12 deletions src/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ pub fn run(env: &mut Env) {
if obj.status.ignored() {
continue;
}
let tid = match env.library.find_type(0, &obj.name) {
Some(x) => x,
None => continue,
let Some(tid) = env.library.find_type(0, &obj.name) else {
continue;
};
let deps = supertypes::dependencies(env, tid);
to_analyze.push((tid, deps));
Expand Down Expand Up @@ -224,9 +223,8 @@ fn analyze_enums(env: &mut Env) {
if obj.status.ignored() {
continue;
}
let tid = match env.library.find_type(0, &obj.name) {
Some(x) => x,
None => continue,
let Some(tid) = env.library.find_type(0, &obj.name) else {
continue;
};

if let Type::Enumeration(_) = env.library.type_(tid) {
Expand All @@ -246,9 +244,8 @@ fn analyze_flags(env: &mut Env) {
if obj.status.ignored() {
continue;
}
let tid = match env.library.find_type(0, &obj.name) {
Some(x) => x,
None => continue,
let Some(tid) = env.library.find_type(0, &obj.name) else {
continue;
};

if let Type::Bitfield(_) = env.library.type_(tid) {
Expand Down Expand Up @@ -325,9 +322,8 @@ fn analyze_constants(env: &mut Env) {

fn analyze(env: &mut Env, tid: TypeId, deps: &[TypeId]) {
let full_name = tid.full_name(&env.library);
let obj = match env.config.objects.get(&*full_name) {
Some(obj) => obj,
None => return,
let Some(obj) = env.config.objects.get(&*full_name) else {
return;
};
match env.library.type_(tid) {
Type::Class(_) => {
Expand Down
7 changes: 3 additions & 4 deletions src/codegen/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use crate::{
pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
info!("Generate global functions");

let functions = match env.analysis.global_functions {
Some(ref functions) => functions,
None => return,
let Some(ref functions) = env.analysis.global_functions else {
return;
};
// Don't generate anything if we have no functions
if functions.functions.is_empty() {
Expand All @@ -27,7 +26,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {

writeln!(w)?;

mod_rs.push("\npub mod functions;".into());
mod_rs.push("\npub(crate) mod functions;".into());

for func_analysis in &functions.functions {
function::generate(w, env, None, func_analysis, None, None, false, false, 0)?;
Expand Down
6 changes: 2 additions & 4 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ pub fn generate_mod_rs(
general::write_vec(w, mod_rs)?;
writeln!(w)?;
if !traits.is_empty() {
writeln!(w, "#[doc(hidden)]")?;
writeln!(w, "pub mod traits {{")?;
writeln!(w, "pub(crate) mod traits {{")?;
general::write_vec(w, traits)?;
writeln!(w, "}}")?;
}

if !builders.is_empty() {
writeln!(w, "#[doc(hidden)]")?;
writeln!(w, "pub mod builders {{")?;
writeln!(w, "pub(crate) mod builders {{")?;
general::write_vec(w, builders)?;
writeln!(w, "}}")?;
}
Expand Down
5 changes: 2 additions & 3 deletions src/codegen/sys/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ pub fn generate_unions_funcs(
) -> Result<()> {
let intern_str = INTERN.to_string();
for union in unions {
let c_type = match union.c_type {
Some(ref x) => x,
None => return Ok(()),
let Some(ref c_type) = union.c_type else {
return Ok(());
};
let name = format!("{}.{}", env.config.library_name, union.name);
let obj = env.config.objects.get(&name).unwrap_or(&DEFAULT_OBJ);
Expand Down
5 changes: 2 additions & 3 deletions src/codegen/sys/lib_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ fn find_modules(env: &Env) -> Result<Vec<String>> {
let mut vec = Vec::<String>::new();
for entry in fs::read_dir(&env.config.auto_path)? {
let path = entry?.path();
let ext = match path.extension() {
Some(ext) => ext,
None => continue,
let Some(ext) = path.extension() else {
continue;
};
if ext != "rs" {
continue;
Expand Down
15 changes: 6 additions & 9 deletions src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ pub struct Constant {

impl Parse for Constant {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "constant") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for constant for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "constant") else {
error!(
"No 'name' or 'pattern' given for constant for object {}",
object_name
);
return None;
};
toml.check_unwanted(
&[
Expand Down
5 changes: 1 addition & 4 deletions src/config/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ where
impl TomlHelper for toml::Value {
fn check_unwanted(&self, options: &[&str], err_msg: &str) {
let mut ret = Vec::new();
let table = match self.as_table() {
Some(table) => table,
None => return,
};
let Some(table) = self.as_table() else { return };
for (key, _) in table.iter() {
if !options.contains(&key.as_str()) {
ret.push(key.clone());
Expand Down
45 changes: 18 additions & 27 deletions src/config/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ pub type CallbackParameters = Vec<CallbackParameter>;

impl Parse for CallbackParameter {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "callback parameter") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for parameter for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "callback parameter") else {
error!(
"No 'name' or 'pattern' given for parameter for object {}",
object_name
);
return None;
};
toml.check_unwanted(&["nullable"], &format!("callback parameter {object_name}"));

Expand Down Expand Up @@ -72,15 +69,12 @@ pub struct Parameter {

impl Parse for Parameter {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "function parameter") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for parameter for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "function parameter") else {
error!(
"No 'name' or 'pattern' given for parameter for object {}",
object_name
);
return None;
};
toml.check_unwanted(
&[
Expand Down Expand Up @@ -295,15 +289,12 @@ pub struct Function {

impl Parse for Function {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "function") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for function for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "function") else {
error!(
"No 'name' or 'pattern' given for function for object {}",
object_name
);
return None;
};
toml.check_unwanted(
&[
Expand Down
15 changes: 6 additions & 9 deletions src/config/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ pub struct Member {

impl Parse for Member {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "member") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for member for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "member") else {
error!(
"No 'name' or 'pattern' given for member for object {}",
object_name
);
return None;
};

toml.check_unwanted(
Expand Down
15 changes: 6 additions & 9 deletions src/config/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ pub struct Property {

impl Parse for Property {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "property") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for property for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "property") else {
error!(
"No 'name' or 'pattern' given for property for object {}",
object_name
);
return None;
};

toml.check_unwanted(
Expand Down
30 changes: 12 additions & 18 deletions src/config/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ pub struct Parameter {

impl Parse for Parameter {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "signal parameter") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for parameter for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "signal parameter") else {
error!(
"No 'name' or 'pattern' given for parameter for object {}",
object_name
);
return None;
};
toml.check_unwanted(
&["nullable", "transformation", "new_name", "name", "pattern"],
Expand Down Expand Up @@ -118,15 +115,12 @@ impl Signal {
object_name: &str,
concurrency: library::Concurrency,
) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "signal") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for signal for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "signal") else {
error!(
"No 'name' or 'pattern' given for signal for object {}",
object_name
);
return None;
};
toml.check_unwanted(
&[
Expand Down
15 changes: 6 additions & 9 deletions src/config/virtual_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ pub struct VirtualMethod {

impl Parse for VirtualMethod {
fn parse(toml: &Value, object_name: &str) -> Option<Self> {
let ident = match Ident::parse(toml, object_name, "virtual_method") {
Some(ident) => ident,
None => {
error!(
"No 'name' or 'pattern' given for virtual_method for object {}",
object_name
);
return None;
}
let Some(ident) = Ident::parse(toml, object_name, "virtual_method") else {
error!(
"No 'name' or 'pattern' given for virtual_method for object {}",
object_name
);
return None;
};
toml.check_unwanted(
&[
Expand Down
5 changes: 2 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ impl Library {
for dir in dirs {
let dir: &Path = dir.as_ref();
let file_name = make_file_name(dir, &libs[libs.len() - 1]);
let mut parser = match XmlParser::from_path(&file_name) {
Ok(p) => p,
_ => continue,
let Ok(mut parser) = XmlParser::from_path(&file_name) else {
continue;
};
return parser.document(|p, _| {
p.element_with_name("repository", |sub_parser, _elem| {
Expand Down
5 changes: 2 additions & 3 deletions src/update_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ fn fix_versions_by_config(library: &mut Library, cfg: &Config) {
}
let version = obj.version;

let tid = match library.find_type(0, &obj.name) {
Some(x) => x,
None => continue,
let Some(tid) = library.find_type(0, &obj.name) else {
continue;
};
match library.type_mut(tid) {
Class(class) => class.version = version,
Expand Down

0 comments on commit 0f2c059

Please sign in to comment.