ircparser/
line.rs

1// BSD 3-Clause License
2//
3// Copyright (c) 2022-present, Ethan Henderson
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this
10//    list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice,
13//    this list of conditions and the following disclaimer in the documentation
14//    and/or other materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its
17//    contributors may be used to endorse or promote products derived from
18//    this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31use std::collections::HashMap;
32
33/// A struct representing a parsed line.
34#[derive(Debug, Clone, Default)]
35pub struct Line {
36    /// This line's tags. This will be an empty hashmap if there are
37    /// none.
38    pub tags: HashMap<String, String>,
39
40    /// This line's source (including the nick, user, and host). This is
41    /// optional, and will be [`None`] if not provided.
42    pub source: Option<String>,
43
44    /// This line's command.
45    pub command: String,
46
47    /// Any parameters passed to the command. This will be an empty
48    /// vector if there are none.
49    pub params: Vec<String>,
50}
51
52impl Line {
53    /// Creates a new [`Line`]. You should never call this directly, but
54    /// instead use the [ircparser::parse](super::parse) function.
55    ///
56    /// # Arguments
57    /// - `tags` - This line's tags.
58    /// = `source` - This line's source, or [`None`] if not to be
59    /// provided.
60    /// - `command` - This line's command.
61    /// - `params` - Any parameters passed to the command.
62    ///
63    /// # Returns
64    /// - [`Line`] - The new [`Line`] instance.
65    ///
66    /// # Example
67    /// ```
68    /// use std::collections::HashMap;
69    ///
70    /// let mut tags: HashMap<String, String> = HashMap::new();
71    /// tags.insert("id".to_string(), "123".to_string());
72    ///
73    /// let source = Some(":nick!user@host.tmi.twitch.tv".to_string());
74    /// let command = "PRIVMSG";
75    /// let params = vec!["#rickastley".to_string()];
76    ///
77    /// let line = ircparser::Line::new(tags, source, command, params);
78    ///
79    /// assert_eq!(&line.tags["id"], "123");
80    /// assert_eq!(line.source.unwrap(), ":nick!user@host.tmi.twitch.tv");
81    /// assert_eq!(line.command, "PRIVMSG");
82    /// assert_eq!(line.params[0], "#rickastley");
83    /// ```
84    ///
85    pub fn new(
86        tags: HashMap<String, String>,
87        source: Option<String>,
88        command: &str,
89        params: Vec<String>,
90    ) -> Self {
91        Self {
92            tags,
93            source,
94            command: command.to_string(),
95            params,
96        }
97    }
98}