Files
empidee/src/commands/client_to_client/channels.rs
h7x4 380a4aed2c
Some checks failed
Build and test / build (push) Successful in 59s
Build and test / check (push) Failing after 1m2s
Build and test / test (push) Successful in 1m40s
Build and test / docs (push) Successful in 1m27s
commands: deduplicate logic in macros, add more macros
2025-02-23 19:26:46 +01:00

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()]
}
);
}
}