Skip to content

Commit

Permalink
Use newtypes for enums instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Mar 25, 2022
1 parent 0d6394c commit 0ef5381
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
35 changes: 19 additions & 16 deletions build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ struct BindgenCallbacks;
impl ParseCallbacks for BindgenCallbacks {
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
// Make sure the ESP_ERR_*, ESP_OK and ESP_FAIL macros are all i32.
const PREFIX: &str = "ESP_";
const SUFFIX: &str = "ERR_";
const SUFFIX_SPECIAL: [&str; 2] = ["OK", "FAIL"];
if let Some(name) = name.strip_prefix("ESP_") {
if name == "OK" || name == "FAIL" || name.starts_with("ERR_") {
return Some(IntKind::I32);
}
}

let name = name.strip_prefix(PREFIX)?;
if name.starts_with(SUFFIX) || SUFFIX_SPECIAL.iter().any(|&s| name == s) {
Some(IntKind::I32)
} else {
None
None
}

fn add_derives(&self, name: &str) -> Vec<String> {
let mut derives = vec![];

// Make sure log levels can be compared.
if name == "esp_log_level_t" {
derives.push("PartialOrd".into());
}

derives
}
}

Expand Down Expand Up @@ -98,15 +106,10 @@ fn main() -> anyhow::Result<()> {
.parse_callbacks(Box::new(BindgenCallbacks))
.ctypes_prefix("crate::c_types")
.header(header_file.try_to_str()?)
.default_enum_style(EnumVariation::Rust {
non_exhaustive: false,
})
.default_enum_style(EnumVariation::NewType { is_bitfield: false })
.constified_enum_module("flags")
.constified_enum_module("http_errno")
.bitfield_enum(r"esp_netif_flags(_t)?")
.constified_enum(r"adc\d?_channel_t")
.constified_enum_module("gpio_num_t")
.constified_enum_module("ip_event_t")
.constified_enum_module("wifi_event_t")
.constified_enum("touch_pad_t")
.no_default("wifi_init_config_t")
.blocklist_function("strtold")
.blocklist_function("_strtold_r")
Expand Down
12 changes: 7 additions & 5 deletions src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ impl Default for wifi_init_config_t {

impl Default for esp_log_level_t {
fn default() -> Self {
if CONFIG_LOG_DEFAULT_LEVEL >= esp_log_level_t::ESP_LOG_VERBOSE as _ {
let default_log_level = esp_log_level_t(CONFIG_LOG_DEFAULT_LEVEL);

if default_log_level >= esp_log_level_t::ESP_LOG_VERBOSE {
esp_log_level_t::ESP_LOG_VERBOSE
} else if CONFIG_LOG_DEFAULT_LEVEL >= esp_log_level_t::ESP_LOG_DEBUG as _ {
} else if default_log_level >= esp_log_level_t::ESP_LOG_DEBUG {
esp_log_level_t::ESP_LOG_DEBUG
} else if CONFIG_LOG_DEFAULT_LEVEL >= esp_log_level_t::ESP_LOG_INFO as _ {
} else if default_log_level >= esp_log_level_t::ESP_LOG_INFO {
esp_log_level_t::ESP_LOG_INFO
} else if CONFIG_LOG_DEFAULT_LEVEL >= esp_log_level_t::ESP_LOG_WARN as _ {
} else if default_log_level >= esp_log_level_t::ESP_LOG_WARN {
esp_log_level_t::ESP_LOG_WARN
} else if CONFIG_LOG_DEFAULT_LEVEL >= esp_log_level_t::ESP_LOG_ERROR as _ {
} else if default_log_level >= esp_log_level_t::ESP_LOG_ERROR {
esp_log_level_t::ESP_LOG_ERROR
} else {
esp_log_level_t::ESP_LOG_NONE
Expand Down

0 comments on commit 0ef5381

Please sign in to comment.