Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically assume that win32_ and unix_ should use the related cfg #1538

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/config/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,7 @@ impl Parse for Function {
.lookup("version")
.and_then(Value::as_str)
.and_then(|s| s.parse().ok());
let cfg_condition = toml
.lookup("cfg_condition")
.and_then(Value::as_str)
.map(ToOwned::to_owned);
let cfg_condition = super::get_cfg_condition(toml, object_name);
let parameters = Parameters::parse(toml.lookup("parameter"), object_name);
let ret = Return::parse(toml.lookup("return"), object_name);
let doc_hidden = toml
Expand Down
5 changes: 1 addition & 4 deletions src/config/gobjects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,7 @@ fn parse_object(
.lookup("version")
.and_then(Value::as_str)
.and_then(|s| s.parse().ok());
let cfg_condition = toml_object
.lookup("cfg_condition")
.and_then(Value::as_str)
.map(ToOwned::to_owned);
let cfg_condition = super::get_object_cfg_condition(toml_object, &name);
let generate_trait = toml_object.lookup("trait").and_then(Value::as_bool);
let final_type = toml_object
.lookup("final_type")
Expand Down
5 changes: 1 addition & 4 deletions src/config/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ impl Parse for Member {
.lookup("deprecated_version")
.and_then(Value::as_str)
.and_then(|s| s.parse().ok());
let cfg_condition = toml
.lookup("cfg_condition")
.and_then(Value::as_str)
.map(ToOwned::to_owned);
let cfg_condition = super::get_cfg_condition(toml, object_name);

let status = {
if toml
Expand Down
59 changes: 59 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,62 @@ pub use self::{
string_type::StringType,
work_mode::WorkMode,
};

use self::error::TomlHelper;
use log::warn;

fn get_cfg_condition(toml: &toml::Value, object_name: &str) -> Option<String> {
let cfg_condition = toml.lookup("cfg_condition").and_then(toml::Value::as_str);
let Some(sub_object_name) = object_name.split('.').nth(1) else {
return cfg_condition.map(ToString::to_string);
};
if sub_object_name.starts_with("win32_") {
match cfg_condition {
Some("windows") => {
warn!("\"object {object_name}\": No need to set `cfg_condition` to `windows` if name starts with `win32_`");
Some("window".to_string())
}
None => Some("window".to_string()),
Some(cfg) => Some(format!("{cfg},windows")),
}
} else if sub_object_name.starts_with("unix_") {
match cfg_condition {
Some("unix") => {
warn!("\"object {object_name}\": No need to set `cfg_condition` to `unix` if name starts with `unix_`");
Some("unix".to_string())
}
None => Some("unix".to_string()),
Some(cfg) => Some(format!("{cfg},unix")),
}
} else {
cfg_condition.map(ToString::to_string)
}
}

fn get_object_cfg_condition(toml: &toml::Value, object_name: &str) -> Option<String> {
let cfg_condition = toml.lookup("cfg_condition").and_then(toml::Value::as_str);
let Some(sub_object_name) = object_name.split('.').nth(1) else {
return cfg_condition.map(ToString::to_string);
};
if sub_object_name.starts_with("Win32") || sub_object_name.starts_with("GWin32") {
match cfg_condition {
Some("windows") => {
warn!("\"object {object_name}\": No need to set `cfg_condition` to `windows` if object name starts with `Win32`");
Some("windows".to_string())
}
None => Some("windows".to_string()),
Some(cfg) => Some(format!("{cfg},windows")),
}
} else if sub_object_name.starts_with("Unix") || sub_object_name.starts_with("GUnix") {
match cfg_condition {
Some("unix") => {
warn!("\"object {object_name}\": No need to set `cfg_condition` to `unix` if object name starts with `Unix`");
Some("unix".to_string())
}
None => Some("unix".to_string()),
Some(cfg) => Some(format!("{cfg},unix")),
}
} else {
cfg_condition.map(ToString::to_string)
}
}
5 changes: 1 addition & 4 deletions src/config/virtual_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ impl Parse for VirtualMethod {
.lookup("version")
.and_then(Value::as_str)
.and_then(|s| s.parse().ok());
let cfg_condition = toml
.lookup("cfg_condition")
.and_then(Value::as_str)
.map(ToOwned::to_owned);
let cfg_condition = super::get_cfg_condition(toml, object_name);
let parameters = Parameters::parse(toml.lookup("parameter"), object_name);
let ret = Return::parse(toml.lookup("return"), object_name);
let doc_hidden = toml
Expand Down