From 0937a3ba4cbac769552d2e9ca5afa3003f46b4bd Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 21 Nov 2025 14:38:09 +0900 Subject: [PATCH] commands: make better use of `expect_property_type!` --- src/commands/connection_settings/tag_types.rs | 11 ++-------- src/commands/reflection/commands.rs | 12 ++-------- src/commands/reflection/not_commands.rs | 12 ++-------- src/commands/reflection/url_handlers.rs | 12 ++-------- src/commands/stickers/sticker_find.rs | 20 +++-------------- src/commands/stickers/stickernamestypes.rs | 22 +++---------------- 6 files changed, 14 insertions(+), 75 deletions(-) diff --git a/src/commands/connection_settings/tag_types.rs b/src/commands/connection_settings/tag_types.rs index 650b1c2..421bc1c 100644 --- a/src/commands/connection_settings/tag_types.rs +++ b/src/commands/connection_settings/tag_types.rs @@ -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); } diff --git a/src/commands/reflection/commands.rs b/src/commands/reflection/commands.rs index 84bca37..5b48a3d 100644 --- a/src/commands/reflection/commands.rs +++ b/src/commands/reflection/commands.rs @@ -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) } diff --git a/src/commands/reflection/not_commands.rs b/src/commands/reflection/not_commands.rs index f1a5af7..83aeb5c 100644 --- a/src/commands/reflection/not_commands.rs +++ b/src/commands/reflection/not_commands.rs @@ -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) diff --git a/src/commands/reflection/url_handlers.rs b/src/commands/reflection/url_handlers.rs index df060a7..3d0b80f 100644 --- a/src/commands/reflection/url_handlers.rs +++ b/src/commands/reflection/url_handlers.rs @@ -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) diff --git a/src/commands/stickers/sticker_find.rs b/src/commands/stickers/sticker_find.rs index 87c64db..28bbc59 100644 --- a/src/commands/stickers/sticker_find.rs +++ b/src/commands/stickers/sticker_find.rs @@ -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 diff --git a/src/commands/stickers/stickernamestypes.rs b/src/commands/stickers/stickernamestypes.rs index e9818b6..08c2b8e 100644 --- a/src/commands/stickers/stickernamestypes.rs +++ b/src/commands/stickers/stickernamestypes.rs @@ -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)