logo
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
//! Iterator implementation for `Capabilities`

use std::{borrow::Cow, collections::btree_map};

use serde_json::Value as JsonValue;

use super::Capabilities;

/// Reference to a capability.
#[derive(Debug)]
pub struct CapabilityRef<'a> {
    name: &'a str,
    value: Option<&'a JsonValue>,
    caps: &'a Capabilities,
}

impl<'a> CapabilityRef<'a> {
    /// Get name of the capability.
    pub fn name(&self) -> &'a str {
        self.name
    }

    /// Get value of the capability.
    pub fn value(&self) -> Cow<'a, JsonValue> {
        match self.value {
            // unknown capability from btreemap iterator
            Some(val) => Cow::Borrowed(val),
            // O(1) lookup of known capability
            None => self.caps.get(self.name).unwrap(),
        }
    }
}

/// An iterator over capabilities.
#[derive(Debug)]
pub struct CapabilitiesIter<'a> {
    /// Reference to Capabilities
    caps: &'a Capabilities,
    /// Current position of the iterator
    pos: usize,
    /// Iterator for custom capabilities
    custom_caps_iterator: btree_map::Iter<'a, String, JsonValue>,
}

impl<'a> CapabilitiesIter<'a> {
    /// Creates a new CapabilitiesIter
    pub(super) fn new(caps: &'a Capabilities) -> Self {
        Self { caps, pos: 0, custom_caps_iterator: caps.custom_capabilities.iter() }
    }
}

impl<'a> Iterator for CapabilitiesIter<'a> {
    type Item = CapabilityRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.pos {
            0 => {
                self.pos += 1;
                Some(CapabilityRef { name: "m.change_password", value: None, caps: self.caps })
            }
            1 => {
                self.pos += 1;
                Some(CapabilityRef { name: "m.room_versions", value: None, caps: self.caps })
            }
            2 => {
                self.pos += 1;
                Some(CapabilityRef { name: "m.set_displayname", value: None, caps: self.caps })
            }
            3 => {
                self.pos += 1;
                Some(CapabilityRef { name: "m.set_avatar_url", value: None, caps: self.caps })
            }
            4 => {
                self.pos += 1;
                Some(CapabilityRef { name: "m.3pid_changes", value: None, caps: self.caps })
            }
            _ => self.custom_caps_iterator.next().map(|(name, value)| CapabilityRef {
                name,
                value: Some(value),
                caps: self.caps,
            }),
        }
    }
}