Files
empidee/src/commands/client_to_client/channels.rs
T

79 lines
2.1 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::{
commands::{Command, Request, RequestParserResult, ResponseParserError},
common::types::ChannelName,
request_tokenizer::RequestTokenizer,
response_tokenizer::{ResponseAttributes, expect_property_type},
};
pub struct Channels;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChannelsResponse {
pub channels: Vec<ChannelName>,
}
impl Command for Channels {
type Request = ();
type Response = ChannelsResponse;
const COMMAND: &'static str = "channels";
fn serialize_request(&self, _: Self::Request) -> String {
Self::COMMAND.to_string()
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
debug_assert!(parts.next().is_none());
Ok((Request::Channels, ""))
}
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: Vec<_> = parts.into_vec()?;
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);
let channel_name = channel_name
.parse()
.map_err(|_| ResponseParserError::SyntaxError(0, channel_name))?;
channel_names.push(channel_name);
}
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".parse().unwrap(),
"bar".parse().unwrap(),
"baz".parse().unwrap(),
]
}
);
}
}