-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(router): optimize router * feat(router): fix clippy warnings
- Loading branch information
Showing
10 changed files
with
298 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aionbot-core": patch:feat | ||
--- | ||
|
||
Optimize router implementation for `AsRef<str>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aionbot-core": patch:feat | ||
--- | ||
|
||
Support alias command for `CommandRouter`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aionbot-core": patch:feat | ||
--- | ||
|
||
Support router to match error event. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::event::Event; | ||
|
||
use super::Router; | ||
|
||
pub struct CommandRouter { | ||
pub prefixes: Vec<String>, | ||
pub command: Vec<String>, | ||
} | ||
|
||
impl Default for CommandRouter { | ||
fn default() -> Self { | ||
Self { | ||
prefixes: vec!["/".into()], | ||
command: ["help".into()].to_vec(), | ||
} | ||
} | ||
} | ||
|
||
impl CommandRouter { | ||
pub fn new<S: Into<String>, C: IntoIterator<Item = S>>( | ||
prefixes: Vec<String>, | ||
command: C, | ||
) -> Self { | ||
Self { | ||
prefixes, | ||
command: command.into_iter().map(Into::into).collect(), | ||
} | ||
} | ||
|
||
pub fn command<S: Into<String>, C: IntoIterator<Item = S>>(command: C) -> Self { | ||
Self { | ||
command: command.into_iter().map(Into::into).collect(), | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl Router for CommandRouter { | ||
fn matches(&self, event: &dyn Event) -> bool { | ||
if let Ok(val) = event.content().downcast::<&str>() { | ||
for prefix in &self.prefixes { | ||
if val.starts_with(prefix) { | ||
let command = val.strip_prefix(prefix).unwrap(); | ||
if self.command.iter().any(|c| command.starts_with(c)) { | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_command_router() { | ||
let router = CommandRouter::default(); | ||
assert!(!router.matches(&"help".to_string())); | ||
assert!(router.matches(&"/help".to_string())); | ||
assert!(router.matches(&"/help@bot".to_string())); | ||
assert!(!router.matches(&"/not help".to_string())); | ||
|
||
let router = CommandRouter::command(["cmd", "command"]); | ||
assert!(!router.matches(&"help".to_string())); | ||
assert!(router.matches(&"/cmd".to_string())); | ||
assert!(router.matches(&"/cmd@bot".to_string())); | ||
assert!(!router.matches(&"/not cmd".to_string())); | ||
assert!(router.matches(&"/command".to_string())); | ||
|
||
let router = CommandRouter::new(vec!["!".to_string()], ["cmd"]); | ||
assert!(!router.matches(&"help".to_string())); | ||
assert!(router.matches(&"!cmd".to_string())); | ||
assert!(router.matches(&"!cmd@bot".to_string())); | ||
assert!(!router.matches(&"!not cmd".to_string())); | ||
assert!(!router.matches(&"/cmd arg1 arg2".to_string())) | ||
} | ||
} |
Oops, something went wrong.