src/commands/ignore.rs
74 lines · 1839 bytes
8 symbols in this file
1use std::borrow::Cow;
2
3use weechat::{
4 buffer::Buffer,
5 hooks::{CommandRun, CommandRunCallback},
6 Prefix, ReturnCode, Weechat,
7};
8
9use crate::PLUGIN_NAME;
10
11pub struct IgnoreCommand;
12
13#[derive(Debug, Eq, PartialEq)]
14enum IgnoreDisposition {
15 EatWithUnsupportedMessage,
16 PassThrough,
17}
18
19impl IgnoreCommand {
20 pub fn create() -> Result<[CommandRun; 2], ()> {
21 Ok([
22 CommandRun::new("2000|/ignore", IgnoreCommand)?,
23 CommandRun::new("2000|/ignore *", IgnoreCommand)?,
24 ])
25 }
26}
27
28fn disposition(plugin_name: &str) -> IgnoreDisposition {
29 if plugin_name == PLUGIN_NAME {
30 IgnoreDisposition::EatWithUnsupportedMessage
31 } else {
32 IgnoreDisposition::PassThrough
33 }
34}
35
36impl CommandRunCallback for IgnoreCommand {
37 fn callback(
38 &mut self,
39 _: &Weechat,
40 buffer: &Buffer,
41 _: Cow<str>,
42 ) -> ReturnCode {
43 match disposition(&buffer.plugin_name()) {
44 IgnoreDisposition::EatWithUnsupportedMessage => {
45 buffer.print(&format!(
46 "{}{}: /ignore is not supported in Matrix buffers.",
47 Weechat::prefix(Prefix::Error),
48 PLUGIN_NAME,
49 ));
50 ReturnCode::OkEat
51 }
52 IgnoreDisposition::PassThrough => ReturnCode::Ok,
53 }
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::{disposition, IgnoreDisposition};
60
61 #[test]
62 fn eats_ignore_in_matrix_buffers() {
63 assert_eq!(
64 IgnoreDisposition::EatWithUnsupportedMessage,
65 disposition("matrix")
66 );
67 }
68
69 #[test]
70 fn passes_ignore_through_in_other_buffers() {
71 assert_eq!(IgnoreDisposition::PassThrough, disposition("irc"));
72 assert_eq!(IgnoreDisposition::PassThrough, disposition("core"));
73 }
74}