Skip to content

Commit

Permalink
clippy: fix all clippy warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Feb 25, 2024
1 parent d9280bc commit 3a9c26e
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 49 deletions.
24 changes: 12 additions & 12 deletions rmk/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub enum KeyAction {

impl KeyAction {
/// Convert a `KeyAction` to corresponding key action code.
pub(crate) fn to_key_action_code(&self) -> u16 {
pub(crate) fn to_key_action_code(self) -> u16 {
match self {
KeyAction::No => 0x0000,
KeyAction::Transparent => 0x0001,
Expand All @@ -63,19 +63,19 @@ impl KeyAction {
KeyAction::WithModifier(a, m) => {
let mut modifier_bits = [0];
// Ignore packing error
ModifierCombination::pack_to_slice(m, &mut modifier_bits).unwrap_or_default();
ModifierCombination::pack_to_slice(&m, &mut modifier_bits).unwrap_or_default();
0x4000 | ((modifier_bits[0] as u16) << 8) | a.to_basic_action_code()
}
KeyAction::ModifierTapHold(action, modifier) => {
let mut modifier_bits = [0];
// Ignore packing error
ModifierCombination::pack_to_slice(modifier, &mut modifier_bits)
ModifierCombination::pack_to_slice(&modifier, &mut modifier_bits)
.unwrap_or_default();
0x6000 | ((modifier_bits[0] as u16) << 8) | action.to_basic_action_code()
}
KeyAction::LayerTapHold(action, layer) => {
if *layer < 16 {
0x3000 | ((*layer as u16) << 15) | action.to_basic_action_code()
if layer < 16 {
0x3000 | ((layer as u16) << 15) | action.to_basic_action_code()
} else {
error!("LayerTapHold supports only layer 0~15, got {}", layer);
0x0000
Expand Down Expand Up @@ -146,13 +146,13 @@ pub enum Action {

impl Action {
/// Convert an `Action` to 12-bit action code
pub(crate) fn to_action_code(&self) -> u16 {
pub(crate) fn to_action_code(self) -> u16 {
match self {
Action::Key(k) => *k as u16,
Action::Key(k) => k as u16,
Action::Modifier(m) => 0xE00 | (m.to_bits() as u16),
Action::LayerOn(layer) => 0xE20 | (*layer as u16),
Action::LayerOff(layer) => 0xE40 | (*layer as u16),
Action::LayerToggle(layer) => 0xE60 | (*layer as u16),
Action::LayerOn(layer) => 0xE20 | (layer as u16),
Action::LayerOff(layer) => 0xE40 | (layer as u16),
Action::LayerToggle(layer) => 0xE60 | (layer as u16),
}
}

Expand Down Expand Up @@ -184,11 +184,11 @@ impl Action {
}

/// Convert an `Action` to 8-bit basic action code, only applicable for `Key(BasicKeyCode)`
pub(crate) fn to_basic_action_code(&self) -> u16 {
pub(crate) fn to_basic_action_code(self) -> u16 {
match self {
Action::Key(kc) => {
if kc.is_basic() {
*kc as u16
kc as u16
} else {
0
}
Expand Down
11 changes: 5 additions & 6 deletions rmk/src/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

pub(crate) fn write_byte(&mut self, mut address: u16, data: &[u8]) {
if data.len() == 0 {
if data.is_empty() {
warn!("No data to write to eeprom, skip");
return;
}
Expand All @@ -175,13 +175,12 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {

for i in (0..data_len).step_by(2) {
let data_idx = address as usize + i;
let data;
if i + 1 == data_len {
let data = if i + 1 == data_len {
// Last byte, append 0xFF
data = ((self.cache[data_idx] as u16) << 8) | (0xFF << 8);
((self.cache[data_idx] as u16) << 8) | (0xFF << 8)
} else {
data = ((self.cache[data_idx] as u16) << 8) | (self.cache[data_idx + 1] as u16);
}
((self.cache[data_idx] as u16) << 8) | (self.cache[data_idx + 1] as u16)
};
let record = EepromRecord { address, data };

// If the storage is full, do consolidation
Expand Down
20 changes: 10 additions & 10 deletions rmk/src/eeprom/eeconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
// Write eeprom
let mut buf = [0xFF; 2];
BigEndian::write_u16(&mut buf, magic);
self.write_byte(0, &mut buf);
self.write_byte(0, &buf);
}

/// Returns eeprom magic value stored in EEPROM
Expand Down Expand Up @@ -105,14 +105,14 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {

/// Set keymap config
pub(crate) fn set_keymap_config(&mut self, config: EeKeymapConfig) {
let mut buf = match config.pack() {
let buf = match config.pack() {
Ok(b) => b,
Err(_) => {
error!("Pack keymap config error");
[0xFF; 2]
}
};
self.write_byte(KEYMAP_CONFIG_ADDR, &mut buf);
self.write_byte(KEYMAP_CONFIG_ADDR, &buf);
}

/// Returns keymap config as `EeKeymapConfig`
Expand All @@ -130,14 +130,14 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {

/// Set backlight config
pub(crate) fn set_backlight_config(&mut self, config: EeBacklightConfig) {
let mut buf = match config.pack() {
let buf = match config.pack() {
Ok(b) => b,
Err(_) => {
error!("Pack backlight config error");
[0xFF; 1]
}
};
self.write_byte(BACKLIGHT_CONFIG_ADDR, &mut buf);
self.write_byte(BACKLIGHT_CONFIG_ADDR, &buf);
}

/// Returns backlight config as `EeBacklightConfig`
Expand All @@ -155,14 +155,14 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {

/// Set audio config
pub(crate) fn set_audio_config(&mut self, config: EeAudioConfig) {
let mut buf = match config.pack() {
let buf = match config.pack() {
Ok(b) => b,
Err(_) => {
error!("Pack audio config error");
[0xFF; 1]
}
};
self.write_byte(AUDIO_CONFIG_ADDR, &mut buf);
self.write_byte(AUDIO_CONFIG_ADDR, &buf);
}

/// Returns audio config as `EeAudioConfig`
Expand All @@ -179,14 +179,14 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {

/// Set rgb light config
pub(crate) fn set_rgb_light_config(&mut self, config: EeRgbLightConfig) {
let mut buf = match config.pack() {
let buf = match config.pack() {
Ok(b) => b,
Err(_) => {
error!("Pack rgb light config error");
[0xFF; 5]
}
};
self.write_byte(RGB_CONFIG_ADDR, &mut buf);
self.write_byte(RGB_CONFIG_ADDR, &buf);
}

/// Returns rgb light config as `EeRgbLightConfig`
Expand All @@ -205,7 +205,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
pub(crate) fn set_layout_option(&mut self, option: u32) {
let mut buf = [0xFF; 4];
BigEndian::write_u32(&mut buf, option);
self.write_byte(LAYOUT_OPTION_ADDR, &mut buf);
self.write_byte(LAYOUT_OPTION_ADDR, &buf);
}

/// Returns layout option
Expand Down
2 changes: 1 addition & 1 deletion rmk/src/eeprom/eekeymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
// 2-byte value, relative addr should be i*2
let addr = DYNAMIC_KEYMAP_ADDR + (i * 2) as u16;
let mut buf: [u8; 2] = [0xFF; 2];
BigEndian::write_u16(&mut buf, to_via_keycode(action.clone()));
BigEndian::write_u16(&mut buf, to_via_keycode(*action));
self.write_byte(addr, &buf);
});
}
Expand Down
6 changes: 4 additions & 2 deletions rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ impl<
) {
// Process modifier first
let (keycodes, n) = modifier.to_modifier_keycodes();
for i in 0..n {
self.process_action_keycode(keycodes[i], key_state);
for kc in keycodes.iter().take(n) {
self.process_action_keycode(*kc, key_state);
}
for _i in 0..n {
}
self.process_key_action_normal(action, key_state);
}
Expand Down
13 changes: 8 additions & 5 deletions rmk/src/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ModifierCombination {

/// Convert modifier combination to a list of modifier keycodes.
/// Returns a list of modifiers keycodes, and the length of the list.
pub(crate) fn to_modifier_keycodes(&self) -> ([KeyCode; 8], usize) {
pub(crate) fn to_modifier_keycodes(self) -> ([KeyCode; 8], usize) {
let mut keycodes = [KeyCode::No; 8];
let mut i = 0;
if self.right {
Expand Down Expand Up @@ -80,18 +80,21 @@ impl ModifierCombination {
}

/// Get modifier hid report bits from modifier combination
pub(crate) fn to_hid_modifier_bits(&self) -> u8 {
pub(crate) fn to_hid_modifier_bits(self) -> u8 {
let (keycodes, n) = self.to_modifier_keycodes();
let mut hid_modifier_bits = 0;
for i in 0..n {
hid_modifier_bits |= keycodes[i].as_modifier_bit();
for item in keycodes.iter().take(n) {
hid_modifier_bits |= item.as_modifier_bit();
}
// for i in 0..n {
// hid_modifier_bits |= keycodes[i].as_modifier_bit();
// }

hid_modifier_bits
}

/// Convert modifier combination to bits
pub(crate) fn to_bits(&self) -> u8 {
pub(crate) fn to_bits(self) -> u8 {
ModifierCombination::pack(&self).unwrap_or_default()[0]
}

Expand Down
2 changes: 1 addition & 1 deletion rmk/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<
// Initialize eeprom, if success, re-load keymap from it
let eeprom = match storage {
Some(s) => {
let e = Eeprom::new(s, eeconfig, &mut action_map);
let e = Eeprom::new(s, eeconfig, &action_map);
// If eeprom is initialized, read keymap from it.
match e {
Some(e) => {
Expand Down
7 changes: 5 additions & 2 deletions rmk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(non_snake_case, non_upper_case_globals)]
// Enable std in test
#![cfg_attr(not(test), no_std)]
#![allow(clippy::if_same_then_else)]

use crate::light::LightService;
use config::{RmkConfig, VialConfig};
Expand Down Expand Up @@ -113,8 +114,10 @@ pub async fn initialize_keyboard_and_run<
vial_keyboard_id: &'static [u8],
vial_keyboard_def: &'static [u8],
) -> ! {
let mut keyboard_config = RmkConfig::default();
keyboard_config.vial_config = VialConfig::new(vial_keyboard_id, vial_keyboard_def);
let keyboard_config = RmkConfig {
vial_config: VialConfig::new(vial_keyboard_id, vial_keyboard_def),
..Default::default()
};

initialize_keyboard_with_config_and_run(
driver,
Expand Down
4 changes: 2 additions & 2 deletions rmk/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ impl<D: Driver<'static>> KeyboardUsbDevice<'static, D> {
// Build usb device
let usb = builder.build();
let (reader, writer) = keyboard_hid.split();
return Self {
Self {
device: usb,
keyboard_hid_reader: reader,
keyboard_hid_writer: writer,
other_hid_writer: other_hid,
via_hid,
};
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions rmk/src/usb/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl CompositeReport {

pub(crate) fn serialize(
&self,
mut data: &mut [u8],
data: &mut [u8],
report_type: CompositeReportType,
) -> Result<usize, ssmarshal::Error> {
// TODO: Optimize it
Expand All @@ -122,19 +122,19 @@ impl CompositeReport {
wheel: self.wheel,
pan: self.pan,
};
Ok(serialize(&mut data, &mouse_report)?)
Ok(serialize(data, &mouse_report)?)
}
CompositeReportType::Media => {
let consumer_report = MediaKeyboardReport {
usage_id: self.media_usage_id,
};
Ok(serialize(&mut data, &consumer_report)?)
Ok(serialize(data, &consumer_report)?)
}
CompositeReportType::System => {
let system_report = SystemControlReport {
usage_id: self.system_usage_id as u8,
usage_id: self.system_usage_id,
};
Ok(serialize(&mut data, &system_report)?)
Ok(serialize(data, &system_report)?)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions rmk/src/via/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<
self.process_via_packet(&mut via_report, &mut self.keymap.borrow_mut());

// Send via report back after processing
match hid_interface.write_serialize(&mut via_report).await {
match hid_interface.write_serialize(&via_report).await {
Ok(_) => Ok(()),
Err(e) => {
error!("Send via report error: {}", e);
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<
"Setting keycode: 0x{:02X} at ({},{}), layer {} as {}",
keycode, row, col, layer, action
);
keymap.set_action_at(row, col, layer, action.clone());
keymap.set_action_at(row, col, layer, action);
match &mut keymap.eeprom {
Some(e) => e.set_keymap_action(row, col, layer, action),
None => (),
Expand Down
2 changes: 1 addition & 1 deletion rmk/src/via/vial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) fn process_vial(
debug!("Received Vial - GetKeyboardId");
// Returns vial protocol version + vial keyboard id
LittleEndian::write_u32(&mut report.input_data[0..4], VIAL_PROTOCOL_VERSION);
report.input_data[4..12].clone_from_slice(&vial_keyboard_Id);
report.input_data[4..12].clone_from_slice(vial_keyboard_Id);
}
VialCommand::GetSize => {
debug!("Received Vial - GetSize");
Expand Down

0 comments on commit 3a9c26e

Please sign in to comment.