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
//! Target and related types.

use super::channel;

/// Standard target represents a channel names list and a channel group names
/// list that together are suitable for use in the API calls.
/// The value of this type is guaranteed to fulfill the standard invariants
/// required by the API calls - that at least one channel or channel group has
/// to be specified.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Standard {
    /// The channel names you are subscribing to.
    channels: Vec<channel::Name>,

    /// The channel group names you are subscribing to.
    channel_groups: Vec<channel::Name>,
}

impl Standard {
    /// Create a standard target from a list of channels and a list of channel
    /// groups.
    ///
    /// # Errors
    ///
    /// Retuns an error when both channels and channel groups lists are empty.
    ///
    pub fn new(
        channels: Vec<channel::Name>,
        channel_groups: Vec<channel::Name>,
    ) -> Result<Self, (Vec<channel::Name>, Vec<channel::Name>)> {
        if channels.is_empty() && channel_groups.is_empty() {
            return Err((channels, channel_groups));
        }

        Ok(Self {
            channels,
            channel_groups,
        })
    }

    /// Extract target value into channels and channel groups.
    #[must_use]
    pub fn into_inner(self) -> (Vec<channel::Name>, Vec<channel::Name>) {
        (self.channels, self.channel_groups)
    }
}

#[cfg(test)]
mod tests {
    use super::Standard;

    #[test]
    fn standard_valid() {
        assert!(Standard::new(vec!["channel".parse().unwrap()], vec![]).is_ok());
        assert!(Standard::new(vec![], vec!["channel_group".parse().unwrap()]).is_ok());
        assert!(Standard::new(
            vec!["channel".parse().unwrap()],
            vec!["channel_group".parse().unwrap()]
        )
        .is_ok());
        assert!(Standard::new(
            vec!["a".parse().unwrap(), "b".parse().unwrap()],
            vec!["a".parse().unwrap(), "b".parse().unwrap()]
        )
        .is_ok());
    }

    #[test]
    fn standard_invalid() {
        assert!(Standard::new(vec![], vec![]).is_err());
    }
}