1
use serde::{Deserialize, Serialize};
2

            
3
use crate::commands::{
4
    Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
5
    expect_property_type,
6
};
7

            
8
pub struct Channels;
9

            
10
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11
pub struct ChannelsResponse {
12
    pub channels: Vec<String>,
13
}
14

            
15
impl Command for Channels {
16
    type Response = ChannelsResponse;
17
    const COMMAND: &'static str = "channels";
18

            
19
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
20
        debug_assert!(parts.next().is_none());
21

            
22
        Ok((Request::Channels, ""))
23
    }
24

            
25
1
    fn parse_response(
26
1
        parts: ResponseAttributes<'_>,
27
1
    ) -> Result<Self::Response, ResponseParserError> {
28
1
        let parts: Vec<_> = parts.into();
29
1
        let mut channel_names = Vec::with_capacity(parts.len());
30
4
        for (key, value) in parts {
31
3
            debug_assert!(key == "channels");
32
3
            let channel_name = expect_property_type!(Some(value), "channels", Text);
33
3
            channel_names.push(channel_name.to_string());
34
        }
35

            
36
1
        Ok(ChannelsResponse {
37
1
            channels: channel_names,
38
1
        })
39
1
    }
40
}
41

            
42
#[cfg(test)]
43
mod tests {
44
    use super::*;
45

            
46
    use indoc::indoc;
47

            
48
    #[test]
49
1
    fn test_parse_response() {
50
1
        let response = indoc! {"
51
1
            channels: foo
52
1
            channels: bar
53
1
            channels: baz
54
1
            OK
55
1
        "};
56
1
        let response = Channels::parse_raw_response(response).unwrap();
57
1
        assert_eq!(
58
1
            response,
59
1
            ChannelsResponse {
60
1
                channels: vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]
61
1
            }
62
1
        );
63
1
    }
64
}