commands: make better use of expect_property_type!

This commit is contained in:
2025-11-21 14:38:09 +09:00
parent 7dc3d7f9cf
commit 7a966051d5
6 changed files with 14 additions and 75 deletions

View File

@@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
ResponseParserError, expect_property_type,
};
pub struct TagTypes;
@@ -25,14 +25,7 @@ impl Command for TagTypes {
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);
}

View File

@@ -1,6 +1,6 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
ResponseParserError, expect_property_type,
};
pub struct Commands;
@@ -25,15 +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",
));
}
};
result.push(value.to_string());
result.push(expect_property_type!(Some(value), "key", Text).to_string());
}
Ok(result)
}

View File

@@ -1,6 +1,5 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
expect_property_type, Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes, ResponseParserError
};
pub struct NotCommands;
@@ -25,14 +24,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);
result.push(value.to_string());
}
Ok(result)

View File

@@ -1,6 +1,5 @@
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
expect_property_type, Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes, ResponseParserError
};
pub struct UrlHandlers;
@@ -25,14 +24,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)

View File

@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
expect_property_type, Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError
};
pub struct StickerFind;
@@ -87,21 +86,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", Text);
// TODO: This assumes the first = is the only one.
// See: https://github.com/MusicPlayerDaemon/MPD/issues/2166

View File

@@ -1,8 +1,7 @@
use std::collections::HashMap;
use crate::commands::{
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
ResponseParserError,
expect_property_type, Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes, ResponseParserError
};
pub struct StickerNamesTypes;
@@ -34,23 +33,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", Text).to_string();
let sticker_type = expect_property_type!(Some(type_value), "type", Text).to_string();
result
.entry(name)