Skip to content

Commit

Permalink
Merge pull request gtk-rs#745 from EPashkin/fix_webkit2_sys_crate_name
Browse files Browse the repository at this point in the history
Add crate name fix for webkit2xx
  • Loading branch information
EPashkin committed Mar 27, 2019
2 parents 1afe7c7 + 3248028 commit 16d8c2d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,16 @@ unsafe { println!("{}", myUnion.test };
```

This is required as the rust compiler can not guarantee the safety of the union, or that the member being addressed exists. The union RFC is [here](https://github.com/tbu-/rust-rfcs/blob/master/text/1444-union.md) and the tracking issue is [here](https://github.com/rust-lang/rust/issues/32836).

### Crate name overrides

`gir` uses simple rule to convert a namespace to a crate name and it sometimes goes wrong. For example, "WebKit2WebExtension" namespace will be converted to "web_kit2_web_extension", which looks bad.

To fix it, the `crate_name_overrides` option can be used.

It also replaces FFI crates' name.

```toml
[crate_name_overrides]
"web_kit2_web_extension" = "webkit2_webextension"
```
19 changes: 19 additions & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
Expand All @@ -8,6 +9,7 @@ use toml;
use config::error::TomlHelper;
use git::repo_hash;
use library::{self, Library};
use nameutil::set_crate_name_overrides;
use super::external_libraries::{read_external_libraries, ExternalLibrary};
use super::WorkMode;
use super::gobjects;
Expand Down Expand Up @@ -66,6 +68,11 @@ impl Config {
config_file.display(), e)),
};

let overrides = read_crate_name_overrides(&toml);
if !overrides.is_empty() {
set_crate_name_overrides(overrides);
}

let work_mode = match work_mode.into() {
Some(w) => w,
None => {
Expand Down Expand Up @@ -244,6 +251,18 @@ fn make_single_version_file(configured: Option<&str>, target_path: &Path) -> Pat
}
}

fn read_crate_name_overrides(toml: &toml::Value) -> HashMap<String, String> {
let mut overrides = HashMap::new();
if let Some(a) = toml.lookup("crate_name_overrides").and_then(toml::Value::as_table) {
for (key, value) in a {
if let Some(s) = value.as_str() {
overrides.insert(key.clone(), s.to_string());
}
}
};
overrides
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
30 changes: 29 additions & 1 deletion src/nameutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ use std::string::String;

use case::*;

static mut CRATE_NAME_OVERRIDES: Option<HashMap<String, String>> = None;

pub(crate) fn set_crate_name_overrides(overrides: HashMap<String, String>) {
unsafe {
if CRATE_NAME_OVERRIDES.is_some() {
panic!("Crate name overrides already set;");
}
CRATE_NAME_OVERRIDES = Some(overrides);
}
}

fn get_crate_name_override(crate_name: &str) -> Option<String> {
unsafe {
if let Some(ref overrides) = CRATE_NAME_OVERRIDES {
if let Some(crate_name) = overrides.get(crate_name) {
return Some(crate_name.clone());
}
}
None
}
}

pub fn split_namespace_name(name: &str) -> (Option<&str>, &str) {
let mut parts = name.split('.');
let name = parts.next_back().unwrap();
Expand Down Expand Up @@ -34,10 +56,15 @@ pub fn file_name_sys(name: &str) -> String {
/// Crate name with undescores for `use` statement
pub fn crate_name(name: &str) -> String {
let name = name.to_snake();
if name.starts_with("g_") {
let crate_name = if name.starts_with("g_") {
format!("g{}", &name[2..])
} else {
name
};
if let Some(crate_name) = get_crate_name_override(&crate_name) {
crate_name
} else {
crate_name
}
}

Expand Down Expand Up @@ -136,6 +163,7 @@ mod tests {
fn crate_name_works() {
assert_eq!(crate_name("GdkPixbuf"), "gdk_pixbuf");
assert_eq!(crate_name("GLib"), "glib");
assert_eq!(crate_name("GObject"), "gobject");
assert_eq!(crate_name("Gtk"), "gtk");
}

Expand Down

0 comments on commit 16d8c2d

Please sign in to comment.