Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use trim_start_matches to remove tilde and at symbols #239

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions swhkd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,40 +624,22 @@ fn parse_keybind(

// Delete the @ and ~ in the last token
fn strip_at(token: &str) -> &str {
if token.starts_with('@') {
let token = token.strip_prefix('@').unwrap();
strip_tilde(token)
} else if token.starts_with('~') {
strip_tilde(token)
} else {
token
}
}

fn strip_tilde(token: &str) -> &str {
if token.starts_with('~') {
let token = token.strip_prefix('~').unwrap();
strip_at(token)
} else if token.starts_with('@') {
strip_at(token)
} else {
token
}
token.trim_start_matches(['@', '~'])
}

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

// Check if each token is valid
for token in &tokens_new {
let token = strip_at(token);
for token in &tokens_no_at {
if key_to_evdev_key.contains_key(token) {
// Can't have a keysym that's like a modifier
if token != last_token {
if *token != last_token {
return Err(Error::InvalidConfig(ParseError::InvalidModifier(path, line_nr)));
}
} else if mod_to_mod_enum.contains_key(token) {
// Can't have a modifier that's like a keysym
if token == last_token {
if *token == last_token {
return Err(Error::InvalidConfig(ParseError::InvalidKeysym(path, line_nr)));
}
} else {
Expand All @@ -668,9 +650,9 @@ fn parse_keybind(
// Translate keypress into evdev key
let keysym = key_to_evdev_key.get(last_token).unwrap();

let modifiers: Vec<Modifier> = tokens_new[0..(tokens_new.len() - 1)]
let modifiers: Vec<Modifier> = tokens_no_at[0..(tokens_no_at.len() - 1)]
.iter()
.map(|token| *mod_to_mod_enum.get(strip_at(token.as_str())).unwrap())
.map(|token| *mod_to_mod_enum.get(token).unwrap())
.collect();

let mut keybinding = KeyBinding::new(*keysym, modifiers);
Expand Down
Loading