1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{
    irc::{IrcMessage, MessageError, TagIndices},
    MaybeOwnedIndex,
};

/// This trait is provided as an easy way of defining your own custom events.

pub trait Validator {
    /// Pre-compute the tag indices

    fn parse_tags(&self) -> TagIndices;
    /// You expect a command matching 'cmd'. See the constants in `IrcMessage`

    fn expect_command(&self, cmd: &str) -> Result<(), MessageError>;
    /// You expect a user nickname to be attached. This returns the `MaybeOwnedIndex` of it

    fn expect_nick(&self) -> Result<MaybeOwnedIndex, MessageError>;
    /// You expect an argumnet at position 'nth' (0-based). This returns the `&str` of it.

    fn expect_arg(&self, nth: usize) -> Result<&str, MessageError>;
    /// You expect an argumnet at position 'nth' (0-based). This returns the `MaybeOwnedIndex` of it.

    fn expect_arg_index(&self, nth: usize) -> Result<MaybeOwnedIndex, MessageError>;
    /// You expect data to be attached to the message. This returns the `&str` of it.

    fn expect_data(&self) -> Result<&str, MessageError>;
    /// You expect data to be attached to the message. This returns the `MaybeOwnedIndex` of it.

    fn expect_data_index(&self) -> Result<MaybeOwnedIndex, MessageError>;
}

impl<'a> Validator for IrcMessage<'a> {
    fn parse_tags(&self) -> TagIndices {
        self.tags
            .map(|index| TagIndices::build_indices(&self.raw[index]))
            .unwrap_or_default()
    }

    fn expect_command(&self, cmd: &str) -> Result<(), MessageError> {
        if self.get_command() != cmd {
            return Err(MessageError::InvalidCommand {
                expected: cmd.to_string(),
                got: self.get_command().to_string(),
            });
        }
        Ok(())
    }

    fn expect_nick(&self) -> Result<MaybeOwnedIndex, MessageError> {
        self.prefix
            .and_then(|p| p.nick_index())
            .ok_or_else(|| MessageError::ExpectedNick)
    }

    fn expect_arg(&self, nth: usize) -> Result<&str, MessageError> {
        self.nth_arg(nth)
            .ok_or_else(|| MessageError::ExpectedArg { pos: nth })
    }

    fn expect_arg_index(&self, nth: usize) -> Result<MaybeOwnedIndex, MessageError> {
        self.nth_arg_index(nth)
            .ok_or_else(|| MessageError::ExpectedArg { pos: nth })
    }

    fn expect_data(&self) -> Result<&str, MessageError> {
        self.expect_data_index().map(|index| &self.raw[index])
    }

    fn expect_data_index(&self) -> Result<MaybeOwnedIndex, MessageError> {
        self.data.ok_or_else(|| MessageError::ExpectedData)
    }
}