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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::ops::Index;

/// Tag Map as described through IRCv3.
pub struct Tags<'a> {
    raw: &'a str,
}

impl<'a> Tags<'a> {
    /// Create a new Tag map from the given string. Expects it to be in valid IRCv3 format.
    pub fn new(raw: &'a str) -> Tags<'a> {
        Tags {
            raw,
        }
    }

    /// Character length of the tags if formatted as IRC string.
    pub fn len(&self) -> usize {
        self.raw.len()
    }

    pub fn is_empty(&self) -> bool {
        self.raw.is_empty()
    }

    /// Iterator over the tag entries.
    pub fn iter(&self) -> impl Iterator<Item=(&'a str, &'a str)> {
        self.raw.split(';')
            .map(|kv| {
                let mut split = kv.split('=');
                (split.next().unwrap(), split.next().unwrap())
            })
    }

    // Search for the key and return start and end of the value
    fn find(&self, key: &'a str) -> Option<(usize, usize)> {
        let key_equals = format!("{}=", key);
        self.raw.find(&key_equals)
            .map(|start| {
                start + key.len() + 1
            })
            .and_then(|start| {
                self.raw[start..].find(';')
                    .or_else(|| self.raw[start..].find(' '))
                    .or_else(|| Some(self.raw.len() - start))
                    .map(|end| (start, start + end))
            })
    }

    pub fn get(&self, key: &'a str) -> Option<&'a str> {
        self.find(key)
            .map(|(start, end)| &self.raw[start..end])
    }
}

impl<'a> Index<&'a str> for Tags<'a> {
    type Output = str;

    fn index(&self, key: &'a str) -> &Self::Output {
        // Find the key
        let (start, end) = self.find(key)
            .expect("no element with key found");
        &self.raw[start..end]
    }
}

#[cfg(test)]
mod tests {
    use crate::tags::Tags;

    #[test]
    fn test_get_and_index() {
        let tags = Tags::new("hello=world;whats=goes");
        let get = tags.get("hello");
        let index = &tags["hello"];
        assert_eq!(get, Some("world"));
        assert_eq!(index, "world");
        let get = tags.get("whats");
        let index = &tags["whats"];
        assert_eq!(get, Some("goes"));
        assert_eq!(index, "goes");
        let get = tags.get("world");
        // Would panic
        // let index = &tags["world"];
        assert_eq!(get, None);
    }
}