Skip to content

Commit a4d2ffe

Browse files
authored
refactor: use trim_start_matches to remove tilde and at symbols (#239)
* refactor: use `trim_start_matches` to remove tilde and at symbols * lint: cargo clippy
1 parent 994b74f commit a4d2ffe

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

swhkd/src/config.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -624,40 +624,22 @@ fn parse_keybind(
624624

625625
// Delete the @ and ~ in the last token
626626
fn strip_at(token: &str) -> &str {
627-
if token.starts_with('@') {
628-
let token = token.strip_prefix('@').unwrap();
629-
strip_tilde(token)
630-
} else if token.starts_with('~') {
631-
strip_tilde(token)
632-
} else {
633-
token
634-
}
635-
}
636-
637-
fn strip_tilde(token: &str) -> &str {
638-
if token.starts_with('~') {
639-
let token = token.strip_prefix('~').unwrap();
640-
strip_at(token)
641-
} else if token.starts_with('@') {
642-
strip_at(token)
643-
} else {
644-
token
645-
}
627+
token.trim_start_matches(['@', '~'])
646628
}
647629

648630
let last_token = strip_at(last_token);
631+
let tokens_no_at: Vec<_> = tokens_new.iter().map(|token| strip_at(token)).collect();
649632

650633
// Check if each token is valid
651-
for token in &tokens_new {
652-
let token = strip_at(token);
634+
for token in &tokens_no_at {
653635
if key_to_evdev_key.contains_key(token) {
654636
// Can't have a keysym that's like a modifier
655-
if token != last_token {
637+
if *token != last_token {
656638
return Err(Error::InvalidConfig(ParseError::InvalidModifier(path, line_nr)));
657639
}
658640
} else if mod_to_mod_enum.contains_key(token) {
659641
// Can't have a modifier that's like a keysym
660-
if token == last_token {
642+
if *token == last_token {
661643
return Err(Error::InvalidConfig(ParseError::InvalidKeysym(path, line_nr)));
662644
}
663645
} else {
@@ -668,9 +650,9 @@ fn parse_keybind(
668650
// Translate keypress into evdev key
669651
let keysym = key_to_evdev_key.get(last_token).unwrap();
670652

671-
let modifiers: Vec<Modifier> = tokens_new[0..(tokens_new.len() - 1)]
653+
let modifiers: Vec<Modifier> = tokens_no_at[0..(tokens_no_at.len() - 1)]
672654
.iter()
673-
.map(|token| *mod_to_mod_enum.get(strip_at(token.as_str())).unwrap())
655+
.map(|token| *mod_to_mod_enum.get(token).unwrap())
674656
.collect();
675657

676658
let mut keybinding = KeyBinding::new(*keysym, modifiers);

0 commit comments

Comments
 (0)