From e469a8eb3c44d5abde25671d807bee048f95ab05 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 25 Feb 2025 12:16:08 +0100 Subject: [PATCH] WIP --- src/commands/client_to_client/readmessages.rs | 8 +++++-- src/commands/connection_settings/tag_types.rs | 15 +++--------- src/commands/music_database/readcomments.rs | 12 ++++++---- src/commands/queue/addid.rs | 9 +++++++- src/commands/reflection/commands.rs | 13 +++-------- src/commands/reflection/not_commands.rs | 13 +++-------- src/commands/reflection/url_handlers.rs | 13 +++-------- src/commands/stickers/sticker_find.rs | 21 ++++------------- src/commands/stickers/sticker_list.rs | 11 +++------ src/commands/stickers/stickernamestypes.rs | 23 ++++--------------- src/commands/stored_playlists/listplaylist.rs | 17 +++++--------- 11 files changed, 50 insertions(+), 105 deletions(-) diff --git a/src/commands/client_to_client/readmessages.rs b/src/commands/client_to_client/readmessages.rs index cf2aea5..5fcb952 100644 --- a/src/commands/client_to_client/readmessages.rs +++ b/src/commands/client_to_client/readmessages.rs @@ -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(); diff --git a/src/commands/connection_settings/tag_types.rs b/src/commands/connection_settings/tag_types.rs index 74b3d91..ea63957 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, + 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); } diff --git a/src/commands/music_database/readcomments.rs b/src/commands/music_database/readcomments.rs index 73236f6..13c34cb 100644 --- a/src/commands/music_database/readcomments.rs +++ b/src/commands/music_database/readcomments.rs @@ -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::, ResponseParserError>>()?; diff --git a/src/commands/queue/addid.rs b/src/commands/queue/addid.rs index e3bfaf9..882002c 100644 --- a/src/commands/queue/addid.rs +++ b/src/commands/queue/addid.rs @@ -40,8 +40,15 @@ impl Command for AddId { ) -> Result { 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 }) } } diff --git a/src/commands/reflection/commands.rs b/src/commands/reflection/commands.rs index 3a36cb9..6b0cd4f 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, + 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) diff --git a/src/commands/reflection/not_commands.rs b/src/commands/reflection/not_commands.rs index a194e61..606e7c8 100644 --- a/src/commands/reflection/not_commands.rs +++ b/src/commands/reflection/not_commands.rs @@ -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) diff --git a/src/commands/reflection/url_handlers.rs b/src/commands/reflection/url_handlers.rs index 1841f3c..88b8b41 100644 --- a/src/commands/reflection/url_handlers.rs +++ b/src/commands/reflection/url_handlers.rs @@ -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) diff --git a/src/commands/stickers/sticker_find.rs b/src/commands/stickers/sticker_find.rs index 17ad414..2a78382 100644 --- a/src/commands/stickers/sticker_find.rs +++ b/src/commands/stickers/sticker_find.rs @@ -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 diff --git a/src/commands/stickers/sticker_list.rs b/src/commands/stickers/sticker_list.rs index 7d51045..8f81dcc 100644 --- a/src/commands/stickers/sticker_list.rs +++ b/src/commands/stickers/sticker_list.rs @@ -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::, ResponseParserError>>()?; result diff --git a/src/commands/stickers/stickernamestypes.rs b/src/commands/stickers/stickernamestypes.rs index 03caa59..3388307 100644 --- a/src/commands/stickers/stickernamestypes.rs +++ b/src/commands/stickers/stickernamestypes.rs @@ -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) diff --git a/src/commands/stored_playlists/listplaylist.rs b/src/commands/stored_playlists/listplaylist.rs index 4d7c2fb..8c84d86 100644 --- a/src/commands/stored_playlists/listplaylist.rs +++ b/src/commands/stored_playlists/listplaylist.rs @@ -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 { 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::, _>>() } }