Skip to content

Commit 063b98e

Browse files
committed
Fix zero-scancode formatting and improve test
1 parent e5320a1 commit 063b98e

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

src/keymap_entry.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{event::Key, raw::input::input_keymap_entry};
66
///
77
/// **Note**: This is not the same as the *localized* keymap you use in applications (like QWERTZ or
88
/// AZERTY), which is handled by the X server or Wayland compositor.
9-
/// Instead, this keymap exists to translate raw scancodes from the keyboard to a USB-HID keycode,
10-
/// which is defined as a US layout.
9+
/// Instead, this keymap exists to translate raw scancodes from the keyboard to USB-HID keycodes,
10+
/// which are defined as a US layout.
1111
///
1212
/// Returned by [`Evdev::keymap_entry`] and [`Evdev::keymap_entry_by_index`].
1313
///
@@ -143,13 +143,20 @@ impl From<u32> for Scancode {
143143

144144
impl fmt::Debug for Scancode {
145145
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146+
let mut any_nonzero = false;
146147
for (i, byte) in self.iter_be_bytes().skip_while(|b| *b == 0).enumerate() {
147148
if i == 0 {
148149
write!(f, "{byte:x}")?;
149150
} else {
150151
write!(f, "{byte:02x}")?;
151152
}
153+
any_nonzero = true;
152154
}
155+
156+
if !any_nonzero {
157+
f.write_str("0")?;
158+
}
159+
153160
Ok(())
154161
}
155162
}
@@ -164,12 +171,23 @@ mod tests {
164171
use super::*;
165172

166173
#[test]
167-
#[cfg_attr(target_endian = "big", ignore = "little-endian test")]
168-
fn scancode_debug() {
169-
let code = Scancode::from_ne_slice(&[0xe0, 0x00, 0x07]);
170-
assert_eq!(format!("{code:?}"), "700e0");
174+
fn smoke() {
175+
let code = Scancode::from(0u8);
176+
assert_eq!(format!("{code}"), "0");
177+
178+
let code = Scancode::from(1u8);
179+
assert_eq!(format!("{code}"), "1");
180+
181+
let code = Scancode::from(10u8);
182+
assert_eq!(format!("{code}"), "a");
183+
184+
let code = Scancode::from(0x10u8);
185+
assert_eq!(format!("{code}"), "10");
186+
187+
let code = Scancode::from(0x100u16);
188+
assert_eq!(format!("{code}"), "100");
171189

172-
let code = Scancode::from_ne_slice(&[0xe0, 0x00, 0x07, 0x00]);
173-
assert_eq!(format!("{code:?}"), "700e0");
190+
let code = Scancode::from(0x1000u16);
191+
assert_eq!(format!("{code}"), "1000");
174192
}
175193
}

0 commit comments

Comments
 (0)