Compare commits

...

1 Commits

Author SHA1 Message Date
e469a8eb3c
WIP 2025-05-07 10:54:28 +02:00
11 changed files with 50 additions and 105 deletions

@ -34,8 +34,12 @@ impl Command for ReadMessages {
let (ckey, cvalue) = channel_message_pair[0];
let (mkey, mvalue) = channel_message_pair[1];
debug_assert!(ckey == "channel");
debug_assert!(mkey == "message");
if ckey != "channel" {
return Err(ResponseParserError::UnexpectedProperty(ckey));
}
if mkey != "message" {
return Err(ResponseParserError::UnexpectedProperty(mkey));
}
let channel = expect_property_type!(Some(cvalue), "channel", Text).to_string();
let message = expect_property_type!(Some(mvalue), "message", Text).to_string();

@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
expect_property_type,
};
pub struct TagTypes;
@ -24,16 +24,7 @@ impl Command for TagTypes {
let mut tagtypes = Vec::with_capacity(parts.len());
for (key, value) in parts.into_iter() {
debug_assert_eq!(key, "tagtype");
let tagtype = match value {
GenericResponseValue::Text(name) => name.to_string(),
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"tagtype", "Binary",
));
}
};
let tagtype = expect_property_type!(Some(value), "tagtype", Text).to_string();
tagtypes.push(tagtype);
}

@ -2,7 +2,7 @@ use std::collections::HashMap;
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
ResponseAttributes, ResponseParserError, expect_property_type,
};
pub struct ReadComments;
@ -30,10 +30,12 @@ impl Command for ReadComments {
let parts: HashMap<_, _> = parts.into();
let comments = parts
.iter()
.map(|(k, v)| match v {
GenericResponseValue::Text(s) => Ok((k.to_string(), s.to_string())),
GenericResponseValue::Binary(_) => Err(ResponseParserError::SyntaxError(1, k)),
.into_iter()
.map(|(k, v)| {
Ok((
k.to_string(),
expect_property_type!(Some(v), k, Text).to_string(),
))
})
.collect::<Result<HashMap<_, _>, ResponseParserError>>()?;

@ -40,8 +40,15 @@ impl Command for AddId {
) -> Result<Self::Response, ResponseParserError> {
let parts: Vec<_> = parts.into();
let mut iter = parts.into_iter();
let (key, id) = get_next_and_parse_property!(iter, Text);
debug_assert!(key == "Id");
if key != "Id" {
return Err(ResponseParserError::UnexpectedProperty(key));
}
debug_assert!(iter.next().is_none());
Ok(AddIdResponse { id })
}
}

@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
expect_property_type,
};
pub struct Commands;
@ -25,14 +25,7 @@ impl Command for Commands {
if key != "command" {
return Err(ResponseParserError::UnexpectedProperty(key));
}
let value = match value {
GenericResponseValue::Text(value) => value,
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"handler", "Binary",
));
}
};
let value = expect_property_type!(Some(value), "command", Text);
result.push(value.to_string());
}
Ok(result)

@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
expect_property_type,
};
pub struct NotCommands;
@ -25,14 +25,7 @@ impl Command for NotCommands {
if key != "command" {
return Err(ResponseParserError::UnexpectedProperty(key));
}
let value = match value {
GenericResponseValue::Text(value) => value,
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"handler", "Binary",
));
}
};
let value = expect_property_type!(Some(value), "command", Text).to_string();
result.push(value.to_string());
}
Ok(result)

@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
expect_property_type,
};
pub struct UrlHandlers;
@ -25,14 +25,7 @@ impl Command for UrlHandlers {
if key != "handler" {
return Err(ResponseParserError::UnexpectedProperty(key));
}
let value = match value {
GenericResponseValue::Text(value) => value,
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"handler", "Binary",
));
}
};
let value = expect_property_type!(Some(value), "handler", Text);
url_handlers.push(value.to_string());
}
Ok(url_handlers)

@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
ResponseParserError, expect_property_type,
};
pub struct StickerFind;
@ -84,21 +84,8 @@ impl Command for StickerFind {
// TODO: debug assert that this is a valid sticker type
// debug_assert!(uri.0 == "");
let uri = match uri.1 {
GenericResponseValue::Text(s) => s.to_string(),
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(uri.0, "Binary"));
}
};
let sticker = match sticker.1 {
GenericResponseValue::Text(s) => s,
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"sticker", "Binary",
));
}
};
let uri = expect_property_type!(Some(uri.1), uri.0, Text).to_string();
let sticker = expect_property_type!(Some(sticker.1), sticker.0, Text);
// TODO: This assumes the first = is the only one.
// See: https://github.com/MusicPlayerDaemon/MPD/issues/2166

@ -1,8 +1,8 @@
use std::collections::HashMap;
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
ResponseParserError, expect_property_type,
};
pub struct StickerList;
@ -37,12 +37,7 @@ impl Command for StickerList {
let result = parts
.iter()
.map(|(_, v)| match v {
GenericResponseValue::Text(value) => Ok(value),
GenericResponseValue::Binary(_) => Err(
ResponseParserError::UnexpectedPropertyType("sticker", "Binary"),
),
})
.map(|(k, v)| Ok(expect_property_type!(Some(v), k, Text)))
.collect::<Result<Vec<_>, ResponseParserError>>()?;
result

@ -1,8 +1,8 @@
use std::collections::HashMap;
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
expect_property_type,
};
pub struct StickerNamesTypes;
@ -34,23 +34,8 @@ impl Command for StickerNamesTypes {
debug_assert!(name_key == "name");
debug_assert!(type_key == "type");
let name = match name_value {
GenericResponseValue::Text(s) => s.to_string(),
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"name", "Binary",
));
}
};
let sticker_type = match type_value {
GenericResponseValue::Text(s) => s.to_string(),
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType(
"type", "Binary",
));
}
};
let name = expect_property_type!(Some(name_value), name_key, Text).to_string();
let sticker_type = expect_property_type!(Some(type_value), type_key, Text).to_string();
result
.entry(name)

@ -1,7 +1,7 @@
use crate::{
commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
ResponseParserError, expect_property_type,
},
common::PlaylistName,
};
@ -37,17 +37,12 @@ impl Command for ListPlaylist {
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError> {
let parts: Vec<_> = parts.into();
debug_assert!(parts.iter().all(|(k, _)| *k == "file"));
parts
.into_iter()
.map(|(name, value)| {
debug_assert_eq!(name, "file");
match value {
GenericResponseValue::Text(value) => Ok(value.to_string()),
GenericResponseValue::Binary(_) => Err(
ResponseParserError::UnexpectedPropertyType("file", "Binary"),
),
}
})
.map(|(name, value)| Ok(expect_property_type!(Some(value), name, Text).to_string()))
.collect::<Result<Vec<_>, _>>()
}
}