rust-x-bindings / xkbcommon-rs

bindings and safe wrappers for libxkbcommon
MIT License
20 stars 24 forks source link

Introduce new variant of compose `Table::new_from_locale` that takes an OsString #17

Closed i509VCB closed 2 years ago

i509VCB commented 2 years ago

In order to construct a table from the locale, I need to query the env of course. However I need to convert the OsString from the environment to a rust string and then the bindings convert the rust string to a c string.

It would be nice if there were a version of the function that took a OsString so I wouldn't need to possibly fail because the file path is not UTF-8:

if let Some(locale) = env::var_os("LC_ALL")
    .and_then(|v| if v.is_empty() { None } else { Some(v) })
    .or_else(|| env::var_os("LC_CTYPE"))
    .and_then(|v| if v.is_empty() { None } else { Some(v) })
    .or_else(|| env::var_os("LANG"))
    .and_then(|v| if v.is_empty() { None } else { Some(v) })
    .unwrap_or_else(|| "C".into())
    .to_str()
{
    // TODO: Would be nice to not need to convert to UTF-8 which can fail.
    if let Ok(table) = xkb::compose::Table::new_from_locale(
        &xkb_context,
        locale,
        xkb::compose::COMPILE_NO_FLAGS,
    ) {
        let compose_state =
            xkb::compose::State::new(&table, xkb::compose::COMPILE_NO_FLAGS);
        *self.xkb_compose.lock().unwrap() = Some(compose_state);
    }
}