Lines
87.23 %
Functions
50 %
use serde::{Deserialize, Serialize};
use crate::{
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
response_tokenizer::{ResponseAttributes, expect_property_type},
};
pub struct Decoders;
empty_command_request!(Decoders, "decoders");
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Decoder {
pub plugin: String,
pub suffixes: Vec<String>,
pub mime_types: Vec<String>,
}
impl Decoder {
pub fn new(plugin: String, suffixes: Vec<String>, mime_types: Vec<String>) -> Self {
Decoder {
plugin,
suffixes,
mime_types,
pub struct DecodersResponse(Vec<Decoder>);
impl DecodersResponse {
pub fn new(items: Vec<Decoder>) -> Self {
DecodersResponse(items)
impl CommandResponse for DecodersResponse {
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
let mut result = Vec::new();
let mut current_decoder: Option<Decoder> = None;
for (key, value) in parts.into_vec()?.into_iter() {
match key {
"plugin" => {
if let Some(decoder) = current_decoder.take() {
result.push(decoder);
let plugin_name = expect_property_type!(Some(value), key, Text).to_string();
current_decoder = Some(Decoder {
plugin: plugin_name,
suffixes: Vec::new(),
mime_types: Vec::new(),
});
"suffix" => {
current_decoder
.as_mut()
.ok_or(ResponseParserError::SyntaxError(0, key.to_string()))?
.suffixes
.push(expect_property_type!(Some(value), key, Text).to_string());
"mime_type" => {
.mime_types
k => {
return Err(ResponseParserError::UnexpectedProperty(k.to_string()));
Ok(DecodersResponse(result))
impl Command for Decoders {
type Request = DecodersRequest;
type Response = DecodersResponse;
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::*;
#[test]
fn test_parse_response() {
let input = indoc! {"
plugin: audiofile
suffix: wav
suffix: au
suffix: aiff
suffix: aif
mime_type: audio/wav
mime_type: audio/aiff
mime_type: audio/x-wav
mime_type: audio/x-aiff
plugin: pcm
mime_type: audio/L16
mime_type: audio/L24
mime_type: audio/x-mpd-float
mime_type: audio/x-mpd-cdda-pcm
mime_type: audio/x-mpd-cdda-pcm-reverse
mime_type: audio/x-mpd-alsa-pcm
OK
"};
let result = Decoders::parse_raw_response(input.as_bytes());
assert_eq!(
result,
Ok(DecodersResponse(vec![
plugin: "audiofile".to_string(),
suffixes: vec![
"wav".to_string(),
"au".to_string(),
"aiff".to_string(),
"aif".to_string()
],
mime_types: vec![
"audio/wav".to_string(),
"audio/aiff".to_string(),
"audio/x-wav".to_string(),
"audio/x-aiff".to_string()
},
plugin: "pcm".to_string(),
suffixes: vec![],
"audio/L16".to_string(),
"audio/L24".to_string(),
"audio/x-mpd-float".to_string(),
"audio/x-mpd-cdda-pcm".to_string(),
"audio/x-mpd-cdda-pcm-reverse".to_string(),
"audio/x-mpd-alsa-pcm".to_string(),
])),
);