Skip to content

Commit

Permalink
Merge pull request #141 from ruabmbua/cargo-fmt
Browse files Browse the repository at this point in the history
cargo fmt all code and add fmt CI check
  • Loading branch information
ruabmbua authored Jan 14, 2024
2 parents 8972305 + b3037ba commit 08d1f55
Show file tree
Hide file tree
Showing 19 changed files with 812 additions and 408 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,17 @@ jobs:
run: cargo build --no-default-features --verbose
- name: Run tests
run: cargo test --no-default-features --verbose

fmt-check:
runs-on: ubuntu-latest

env:
DEBIAN_FRONTEND: noninteractive

steps:
- name: checkout repository and submodules
uses: actions/checkout@v2
with:
submodules: recursive
- name: fmt check
run: cargo fmt --check
12 changes: 8 additions & 4 deletions examples/dump_descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ fn main() {
match HidApi::new() {
Ok(api) => {
for device in api.device_list() {
println!(" {} (Interface {}):",
println!(
" {} (Interface {}):",
match device.product_string() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
},
device.interface_number()
);
let mut descriptor = vec![0u8; 2048];
match device.open_device(&api).and_then(|dev| dev.get_report_descriptor(&mut descriptor)) {
match device
.open_device(&api)
.and_then(|dev| dev.get_report_descriptor(&mut descriptor))
{
Ok(length) => println!(" {:?}", &mut descriptor[..length]),
Err(err) => println!(" Failed to retrieve descriptor ({:?})", err)
Err(err) => println!(" Failed to retrieve descriptor ({:?})", err),
}
}
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
}
2 changes: 1 addition & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ pub mod macos {
// For documentation look at the corresponding C header file hidapi_winapi.h
#[cfg(target_os = "windows")]
pub mod windows {
use windows_sys::core::GUID;
use super::*;
use windows_sys::core::GUID;

extern "C" {
pub fn hid_winapi_get_container_id(
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ mod macos;
mod windows;

#[cfg(feature = "windows-native")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "windows-native", target_os = "windows"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "windows-native", target_os = "windows")))
)]
mod windows_native;

use libc::wchar_t;
Expand Down Expand Up @@ -214,7 +217,8 @@ impl HidApi {
/// Indexes devices that match the given VID and PID filters.
/// 0 indicates no filter.
pub fn add_devices(&mut self, vid: u16, pid: u16) -> HidResult<()> {
self.device_list.append(&mut HidApiBackend::get_hid_device_info_vector(vid, pid)?);
self.device_list
.append(&mut HidApiBackend::get_hid_device_info_vector(vid, pid)?);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use windows_sys::core::GUID;
use crate::{HidDevice, HidResult};
use windows_sys::core::GUID;

impl HidDevice {
/// Get the container ID for a HID device.
Expand Down
138 changes: 83 additions & 55 deletions src/windows_native/descriptor/encoder.rs

Large diffs are not rendered by default.

393 changes: 326 additions & 67 deletions src/windows_native/descriptor/mod.rs

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions src/windows_native/descriptor/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs::read_to_string;
use crate::windows_native::descriptor::get_descriptor_ptr;
use std::fs::read_to_string;

#[test]
fn test_01() {
Expand Down Expand Up @@ -98,7 +98,6 @@ fn test_24() {
execute_testcase("17CC_1130_0000_FF01");
}


fn execute_testcase(filename: &str) {
let source_path = format!("./tests/pp_data/{filename}.pp_data");
let expected_path = format!("./tests/pp_data/{filename}.expected");
Expand All @@ -110,13 +109,13 @@ fn execute_testcase(filename: &str) {
}

fn decode_hex(hex: &str) -> Vec<u8> {
hex
.lines()
.flat_map(|line| line
.split(',')
.map(|hex| hex.trim())
.filter(|hex| !hex.is_empty())
.map(|hex| hex.strip_prefix("0x").unwrap())
.map(|hex| u8::from_str_radix(hex, 16).unwrap()))
hex.lines()
.flat_map(|line| {
line.split(',')
.map(|hex| hex.trim())
.filter(|hex| !hex.is_empty())
.map(|hex| hex.strip_prefix("0x").unwrap())
.map(|hex| u8::from_str_radix(hex, 16).unwrap())
})
.collect()
}
37 changes: 19 additions & 18 deletions src/windows_native/descriptor/typedefs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::mem::size_of;
use crate::windows_native::descriptor::types::BitRange;
use std::mem::size_of;

// Reverse engineered typedefs for the internal structure of the preparsed data taken from
// https://github.com/libusb/hidapi/blob/master/windows/hidapi_descriptor_reconstruct.h
Expand All @@ -9,7 +9,10 @@ use crate::windows_native::descriptor::types::BitRange;
macro_rules! const_assert {
($x:expr $(,)?) => {
#[allow(unknown_lints)]
const _: [(); 0 - !{ const ASSERT: bool = $x; ASSERT } as usize] = [];
const _: [(); 0 - !{
const ASSERT: bool = $x;
ASSERT
} as usize] = [];
};
}

Expand All @@ -25,7 +28,7 @@ pub struct LinkCollectionNode {
pub number_of_children: u16,
pub next_sibling: u16,
pub first_child: u16,
pub bits: u32
pub bits: u32,
}

impl LinkCollectionNode {
Expand All @@ -44,7 +47,7 @@ pub struct CapsInfo {
pub first_cap: u16,
pub number_of_caps: u16,
pub last_cap: u16,
pub report_byte_length: u16
pub report_byte_length: u16,
}

const_assert!(size_of::<UnknownToken>() == 8);
Expand All @@ -53,14 +56,14 @@ const_assert!(size_of::<UnknownToken>() == 8);
pub struct UnknownToken {
pub token: u8,
_reserved: [u8; 3],
pub bit_field: u32
pub bit_field: u32,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct Button {
pub logical_min: i32,
pub logical_max: i32
pub logical_max: i32,
}

#[derive(Copy, Clone)]
Expand All @@ -71,14 +74,14 @@ pub struct NotButton {
pub logical_min: i32,
pub logical_max: i32,
pub physical_min: i32,
pub physical_max: i32
pub physical_max: i32,
}

#[derive(Copy, Clone)]
#[repr(C)]
union MaybeButton {
button: Button,
not_button: NotButton
not_button: NotButton,
}

#[derive(Copy, Clone)]
Expand All @@ -91,7 +94,7 @@ pub struct Range {
pub designator_min: u16,
pub designator_max: u16,
pub data_index_min: u16,
pub data_index_max: u16
pub data_index_max: u16,
}

#[derive(Copy, Clone)]
Expand All @@ -111,10 +114,9 @@ pub struct NotRange {
#[repr(C)]
union MaybeRange {
range: Range,
not_range: NotRange
not_range: NotRange,
}


const_assert!(size_of::<Caps>() == 104);
#[derive(Copy, Clone)]
#[repr(C)]
Expand All @@ -137,7 +139,7 @@ pub struct Caps {
maybe_range: MaybeRange,
maybe_button: MaybeButton,
pub units: u32,
pub units_exp: u32
pub units_exp: u32,
}

impl Caps {
Expand All @@ -159,22 +161,22 @@ impl Caps {

pub fn range(&self) -> Range {
//Both union elements have the same size and are valid for all bit patterns
unsafe {self.maybe_range.range }
unsafe { self.maybe_range.range }
}

pub fn not_range(&self) -> NotRange {
//Both union elements have the same size and are valid for all bit patterns
unsafe {self.maybe_range.not_range }
unsafe { self.maybe_range.not_range }
}

pub fn button(&self) -> Button {
//Both union elements have the same size and are valid for all bit patterns
unsafe {self.maybe_button.button }
unsafe { self.maybe_button.button }
}

pub fn not_button(&self) -> NotButton {
//Both union elements have the same size and are valid for all bit patterns
unsafe {self.maybe_button.not_button }
unsafe { self.maybe_button.not_button }
}

pub fn get_bit_range(&self) -> BitRange {
Expand All @@ -185,7 +187,6 @@ impl Caps {
last_bit,
}
}

}

#[derive(Copy, Clone)]
Expand All @@ -197,5 +198,5 @@ pub struct HidpPreparsedData {
_reserved: [u16; 2],
pub caps_info: [CapsInfo; 3],
pub first_byte_of_link_collection_array: u16,
pub number_link_collection_nodes: u16
pub number_link_collection_nodes: u16,
}
Loading

0 comments on commit 08d1f55

Please sign in to comment.