Skip to content

Commit 8f02258

Browse files
committed
refactor: do not use macro
1 parent b57a03d commit 8f02258

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

src/config/key.rs

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -188,49 +188,57 @@ mod test {
188188
use super::*;
189189
use pretty_assertions::assert_eq;
190190

191+
fn ch(c: &str) -> IcedKey {
192+
IcedKey::Character(SmolStr::new(c))
193+
}
194+
195+
#[track_caller]
196+
fn parse(input: &str, expected: Result<KeySequence, String>) {
197+
assert_eq!(
198+
input.parse::<KeySequence>(),
199+
expected,
200+
"Failed to parse {:?}",
201+
input
202+
);
203+
}
204+
191205
#[test]
192206
fn parse_key_sequence() {
193-
macro_rules! assert_parsed_key_sequences {
194-
( $( $seq:literal -> $( $outcome:tt ),+ )* ) => {{
195-
$(
196-
assert_eq!(
197-
$seq.parse::<KeySequence>(),
198-
assert_parsed_key_sequences!(@seq $($outcome),*),
199-
concat!("Failed to parse ", $seq)
200-
);
201-
)*
202-
}};
203-
(@seq Err, $message:literal ) => { Err($message.to_string()) };
204-
(@seq $first:expr) => { Ok(KeySequence((assert_parsed_key_sequences!(@key $first), None))) };
205-
(@seq $first:tt, $second:tt) => {{
206-
Ok(KeySequence((
207-
assert_parsed_key_sequences!(@key $first),
208-
Some(assert_parsed_key_sequences!(@key $second))
209-
)))
210-
}};
211-
(@key $key:ident) => { IcedKey::Named(key::Named::$key) };
212-
(@key $key:literal) => { IcedKey::Character(SmolStr::new($key)) };
213-
}
214-
215-
assert_parsed_key_sequences! {
216-
"gh" -> "g", "h"
217-
"ge" -> "g", "e"
218-
"x" -> "x"
219-
"Lx" -> "L", "x"
220-
"" -> Err, "Expected at least 1 key."
221-
"<space>x" -> Space, "x"
222-
"x<space>" -> "x", Space
223-
"<space><space>" -> Space, Space
224-
"<<" -> "<", "<"
225-
"<>" -> "<", ">"
226-
"<" -> "<"
227-
">>" -> ">", ">"
228-
"<<space>" -> "<", Space
229-
"<f32><f31>" -> F32, F31
230-
"><f32>" -> ">", F32
231-
"abc" -> Err, "At the moment, only up to 2 keys in a sequence are supported."
232-
"<f32>b<f16>" -> Err, "At the moment, only up to 2 keys in a sequence are supported."
233-
"<@>" -> Err, "Invalid key: <@>. Matching variant not found"
234-
}
207+
use IcedKey::Named;
208+
use key::Named::*;
209+
210+
parse("gh", Ok(KeySequence((ch("g"), Some(ch("h"))))));
211+
parse("ge", Ok(KeySequence((ch("g"), Some(ch("e"))))));
212+
parse("x", Ok(KeySequence((ch("x"), None))));
213+
parse("Lx", Ok(KeySequence((ch("L"), Some(ch("x"))))));
214+
parse("", Err("Expected at least 1 key.".to_string()));
215+
parse("<space>x", Ok(KeySequence((Named(Space), Some(ch("x"))))));
216+
parse("x<space>", Ok(KeySequence((ch("x"), Some(Named(Space))))));
217+
parse(
218+
"<space><space>",
219+
Ok(KeySequence((Named(Space), Some(Named(Space))))),
220+
);
221+
parse("<<", Ok(KeySequence((ch("<"), Some(ch("<"))))));
222+
parse("<>", Ok(KeySequence((ch("<"), Some(ch(">"))))));
223+
parse("<", Ok(KeySequence((ch("<"), None))));
224+
parse(">>", Ok(KeySequence((ch(">"), Some(ch(">"))))));
225+
parse("<<space>", Ok(KeySequence((ch("<"), Some(Named(Space))))));
226+
parse(
227+
"<f32><f31>",
228+
Ok(KeySequence((Named(F32), Some(Named(F31))))),
229+
);
230+
parse("><f32>", Ok(KeySequence((ch(">"), Some(Named(F32))))));
231+
parse(
232+
"abc",
233+
Err("At the moment, only up to 2 keys in a sequence are supported.".to_string()),
234+
);
235+
parse(
236+
"<f32>b<f16>",
237+
Err("At the moment, only up to 2 keys in a sequence are supported.".to_string()),
238+
);
239+
parse(
240+
"<@>",
241+
Err("Invalid key: <@>. Matching variant not found".to_string()),
242+
);
235243
}
236244
}

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
//! The ferrishot app
2-
#![cfg_attr(test, allow(clippy::unwrap_used, reason = "ok to unwrap in test"))]
2+
#![cfg_attr(
3+
test,
4+
allow(
5+
clippy::unwrap_used,
6+
clippy::needless_pass_by_value,
7+
reason = "relaxed lints in tests"
8+
)
9+
)]
310

11+
/// See [`CommandHandler`](crate::config::commands::CommandHandler) for more info
412
mod command {
513
pub use super::config::commands::CommandHandler as Handler;
614
}

0 commit comments

Comments
 (0)