src/commands/me.rs
69 lines · 1488 bytes
8 symbols in this file
1use std::borrow::Cow;
2
3use weechat::{
4 buffer::Buffer,
5 hooks::{CommandRun, CommandRunCallback},
6 ReturnCode, Weechat,
7};
8
9use crate::Servers;
10
11pub struct MeCommand {
12 servers: Servers,
13}
14
15const ME_COMMAND_PATTERN: &str = "/me *";
16
17fn action_body(command: &str) -> Option<&str> {
18 command.strip_prefix("/me ")
19}
20
21impl MeCommand {
22 pub fn create(servers: &Servers) -> Result<CommandRun, ()> {
23 CommandRun::new(
24 ME_COMMAND_PATTERN,
25 MeCommand {
26 servers: servers.clone(),
27 },
28 )
29 }
30}
31
32impl CommandRunCallback for MeCommand {
33 fn callback(
34 &mut self,
35 _: &Weechat,
36 buffer: &Buffer,
37 cmd: Cow<str>,
38 ) -> ReturnCode {
39 let Some(room) = self.servers.find_room(buffer) else {
40 return ReturnCode::Ok;
41 };
42
43 let Some(body) = action_body(&cmd) else {
44 return ReturnCode::Ok;
45 };
46
47 self.servers
48 .runtime()
49 .block_on(room.send_emote(buffer, body.to_owned()));
50
51 ReturnCode::OkEat
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::{action_body, ME_COMMAND_PATTERN};
58
59 #[test]
60 fn me_hook_matches_action_arguments_before_command_resolution() {
61 assert_eq!(ME_COMMAND_PATTERN, "/me *");
62 }
63
64 #[test]
65 fn me_action_body_excludes_other_commands() {
66 assert_eq!(action_body("/me waves"), Some("waves"));
67 assert_eq!(action_body("/message waves"), None);
68 }
69}