From aa3fda8b3565ecfd5b8810e168a6472bc51f48c2 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Sat, 3 Jun 2023 18:20:59 +0300 Subject: [PATCH] Add github actions CI Also cleanup the code, use rustfmt, and migrate to once_cell. --- .github/workflows/ci.yml | 45 +++++++++++++++++++++ Cargo.toml | 7 ++-- src/lib.rs | 86 ++++++++++++++++++++++------------------ src/x11.rs | 28 ++++++------- 4 files changed, 110 insertions(+), 56 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..bbb22d4c9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +name: CI + +on: + pull_request: + push: + branches: [master] + +env: + RUST_BACKTRACE: 1 + CARGO_INCREMENTAL: 0 + RUSTFLAGS: "-Cdebuginfo=0 --deny=warnings" + RUSTDOCFLAGS: "--deny=warnings" + +jobs: + fmt: + name: Check Formatting + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: hecrj/setup-rust-action@v1 + with: + rust-version: nightly + components: rustfmt + - name: Check Formatting + run: cargo +nightly fmt --all -- --check + + tests: + name: Tests + runs-on: ubuntu-latest + strategy: + matrix: + rust_version: ["1.64", stable, nightly] + + steps: + - uses: actions/checkout@v3 + + - uses: hecrj/setup-rust-action@v1 + with: + rust-version: ${{ matrix.rust_version }} + + - name: Check documentation + run: cargo doc --no-deps --document-private-items + + - name: Run tests + run: cargo test --verbose diff --git a/Cargo.toml b/Cargo.toml index 402bc8ec2..d0f6e8d79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,8 @@ repository = "https://github.com/rust-windowing/xkbcommon-dl" authors = ["Francesca Frangipane "] license = "MIT" description = "Dynamically loaded xkbcommon and xkbcommon-x11 Rust bindings." -edition = "2018" +edition = "2021" +rust-version = "1.64" [badges] @@ -14,7 +15,7 @@ edition = "2018" x11 = [] [dependencies] -dlib = "0.5" -lazy_static = "1.0" bitflags = "2.3.1" +dlib = "0.5" log = "0.4" +once_cell = "1.17" diff --git a/src/lib.rs b/src/lib.rs index c81c5ec84..f9ee7bf45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,29 +1,27 @@ #![allow(dead_code, non_camel_case_types)] -#![cfg_attr(rustfmt, rustfmt_skip)] -use dlib::dlopen_external_library; -use lazy_static::lazy_static; +use std::os::raw::{c_char, c_int, c_uint, c_void}; + use bitflags::bitflags; +use dlib::dlopen_external_library; +use log::info; +use once_cell::sync::Lazy; pub mod keysyms; #[cfg(feature = "x11")] pub mod x11; -use log::info; - -use std::os::raw::{c_char, c_int, c_uint, c_void}; +pub const XKB_MOD_NAME_SHIFT: &[u8] = b"Shift\0"; +pub const XKB_MOD_NAME_CAPS: &[u8] = b"Lock\0"; +pub const XKB_MOD_NAME_CTRL: &[u8] = b"Control\0"; +pub const XKB_MOD_NAME_ALT: &[u8] = b"Mod1\0"; +pub const XKB_MOD_NAME_NUM: &[u8] = b"Mod2\0"; +pub const XKB_MOD_NAME_LOGO: &[u8] = b"Mod4\0"; -pub const XKB_MOD_NAME_SHIFT : &'static [u8] = b"Shift\0"; -pub const XKB_MOD_NAME_CAPS : &'static [u8] = b"Lock\0"; -pub const XKB_MOD_NAME_CTRL : &'static [u8] = b"Control\0"; -pub const XKB_MOD_NAME_ALT : &'static [u8] = b"Mod1\0"; -pub const XKB_MOD_NAME_NUM : &'static [u8] = b"Mod2\0"; -pub const XKB_MOD_NAME_LOGO : &'static [u8] = b"Mod4\0"; - -pub const XKB_LED_NAME_CAPS : &'static [u8] = b"Caps Lock\0"; -pub const XKB_LED_NAME_NUM : &'static [u8] = b"Num Lock\0"; -pub const XKB_LED_NAME_SCROLL : &'static [u8] = b"Scroll Lock\0"; +pub const XKB_LED_NAME_CAPS: &[u8] = b"Caps Lock\0"; +pub const XKB_LED_NAME_NUM: &[u8] = b"Num Lock\0"; +pub const XKB_LED_NAME_SCROLL: &[u8] = b"Scroll Lock\0"; pub struct xkb_context; pub struct xkb_keymap; @@ -42,18 +40,18 @@ pub type xkb_led_index_t = u32; pub type xkb_led_mask_t = u32; pub type xkb_keymap_key_iter_t = Option; -pub const XKB_KEYCODE_INVALID :u32 = 0xffffffff; -pub const XKB_LAYOUT_INVALID :u32 = 0xffffffff; -pub const XKB_LEVEL_INVALID :u32 = 0xffffffff; -pub const XKB_MOD_INVALID :u32 = 0xffffffff; -pub const XKB_LED_INVALID :u32 = 0xffffffff; -pub const XKB_KEYCODE_MAX :u32 = 0xffffffff - 1; +pub const XKB_KEYCODE_INVALID: u32 = 0xffffffff; +pub const XKB_LAYOUT_INVALID: u32 = 0xffffffff; +pub const XKB_LEVEL_INVALID: u32 = 0xffffffff; +pub const XKB_MOD_INVALID: u32 = 0xffffffff; +pub const XKB_LED_INVALID: u32 = 0xffffffff; +pub const XKB_KEYCODE_MAX: u32 = 0xffffffff - 1; #[repr(C)] pub struct xkb_rule_names { - pub rules: *const c_char, - pub model: *const c_char, - pub layout: *const c_char, + pub rules: *const c_char, + pub model: *const c_char, + pub layout: *const c_char, pub variant: *const c_char, pub options: *const c_char, } @@ -284,20 +282,30 @@ functions: fn xkb_compose_state_get_one_sym(*mut xkb_compose_state) -> xkb_keysym_t, ); -lazy_static!( - pub static ref XKBCOMMON_OPTION: Option = { - open_with_sonames(&["libxkbcommon.so", "libxkbcommon.so.0"], None, |name| unsafe { XkbCommon::open(name) }) - }; - pub static ref XKBCOMMON_HANDLE: &'static XkbCommon = { - XKBCOMMON_OPTION.as_ref().expect("Library libxkbcommon.so could not be loaded.") - }; - pub static ref XKBCOMMON_COMPOSE_OPTION: Option = { - open_with_sonames(&["libxkbcommon.so", "libxkbcommon.so.0"], Some("compose"), |name| unsafe { XkbCommonCompose::open(name) }) - }; - pub static ref XKBCOMMON_COMPOSE_HANDLE: &'static XkbCommonCompose = { - XKBCOMMON_COMPOSE_OPTION.as_ref().expect("Could not load compose module from libxkbcommon.so.") - }; -); +pub static XKBCOMMON_OPTION: Lazy> = Lazy::new(|| { + open_with_sonames( + &["libxkbcommon.so", "libxkbcommon.so.0"], + None, + |name| unsafe { XkbCommon::open(name) }, + ) +}); +pub static XKBCOMMON_HANDLE: Lazy<&'static XkbCommon> = Lazy::new(|| { + XKBCOMMON_OPTION + .as_ref() + .expect("Library libxkbcommon.so could not be loaded.") +}); +pub static XKBCOMMON_COMPOSE_OPTION: Lazy> = Lazy::new(|| { + open_with_sonames( + &["libxkbcommon.so", "libxkbcommon.so.0"], + Some("compose"), + |name| unsafe { XkbCommonCompose::open(name) }, + ) +}); +pub static XKBCOMMON_COMPOSE_HANDLE: Lazy<&'static XkbCommonCompose> = Lazy::new(|| { + XKBCOMMON_COMPOSE_OPTION + .as_ref() + .expect("Could not load compose module from libxkbcommon.so.") +}); fn open_with_sonames(names: &[&str], module: Option<&str>, open: F) -> Option where diff --git a/src/x11.rs b/src/x11.rs index 0826ad247..97a1a8c50 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -1,5 +1,7 @@ use std::os::raw::c_int; +use once_cell::sync::Lazy; + pub type xcb_connection_t = c_void; use super::*; @@ -40,17 +42,15 @@ functions: ) -> *mut xkb_state, ); -lazy_static!( - pub static ref XKBCOMMON_X11_OPTION: Option = { - open_with_sonames( - &["libxkbcommon-x11.so", "libxkbcommon-x11.so.0"], - None, - |name| unsafe { XkbCommonX11::open(name) }, - ) - }; - pub static ref XKBCOMMON_X11_HANDLE: &'static XkbCommonX11 = { - XKBCOMMON_X11_OPTION - .as_ref() - .expect("Library libxkbcommon-x11.so could not be loaded.") - }; -); +pub static XKBCOMMON_X11_OPTION: Lazy> = Lazy::new(|| { + open_with_sonames( + &["libxkbcommon-x11.so", "libxkbcommon-x11.so.0"], + None, + |name| unsafe { XkbCommonX11::open(name) }, + ) +}); +pub static XKBCOMMON_X11_HANDLE: Lazy<&'static XkbCommonX11> = Lazy::new(|| { + XKBCOMMON_X11_OPTION + .as_ref() + .expect("Library libxkbcommon-x11.so could not be loaded.") +});