65 lines
1.6 KiB
Rust
65 lines
1.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::commands::{
|
|
expect_property_type, Command, Request, RequestParserResult, ResponseAttributes,
|
|
ResponseParserError,
|
|
};
|
|
|
|
pub struct Channels;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct ChannelsResponse {
|
|
pub channels: Vec<String>,
|
|
}
|
|
|
|
impl Command for Channels {
|
|
type Response = ChannelsResponse;
|
|
const COMMAND: &'static str = "channels";
|
|
|
|
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
|
debug_assert!(parts.next().is_none());
|
|
|
|
Ok((Request::Channels, ""))
|
|
}
|
|
|
|
fn parse_response(
|
|
parts: ResponseAttributes<'_>,
|
|
) -> Result<Self::Response, ResponseParserError> {
|
|
let parts: Vec<_> = parts.into();
|
|
let mut channel_names = Vec::with_capacity(parts.len());
|
|
for (key, value) in parts {
|
|
debug_assert!(key == "channels");
|
|
let channel_name = expect_property_type!(Some(value), "channels", Text);
|
|
channel_names.push(channel_name.to_string());
|
|
}
|
|
|
|
Ok(ChannelsResponse {
|
|
channels: channel_names,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
use indoc::indoc;
|
|
|
|
#[test]
|
|
fn test_parse_response() {
|
|
let response = indoc! {"
|
|
channels: foo
|
|
channels: bar
|
|
channels: baz
|
|
OK
|
|
"};
|
|
let response = Channels::parse_raw_response(response).unwrap();
|
|
assert_eq!(
|
|
response,
|
|
ChannelsResponse {
|
|
channels: vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]
|
|
}
|
|
);
|
|
}
|
|
}
|