From 95604aa20d003d8e31e5aade10d40e5dae366904 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 21 Nov 2025 15:07:50 +0900 Subject: [PATCH] commands: return runtime errors on invalid property names --- src/commands/connection_settings/tag_types.rs | 4 ++- .../partition_commands/listpartitions.rs | 4 ++- src/commands/queue/addid.rs | 4 ++- src/commands/stickers/sticker_find.rs | 6 ++-- src/commands/stickers/sticker_list.rs | 4 ++- src/commands/stickers/stickernames.rs | 4 ++- src/commands/stickers/stickernamestypes.rs | 9 ++++-- src/commands/stickers/stickertypes.rs | 4 ++- src/commands/stored_playlists/listplaylist.rs | 28 +++++++++---------- 9 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/commands/connection_settings/tag_types.rs b/src/commands/connection_settings/tag_types.rs index 421bc1cb..3c435118 100644 --- a/src/commands/connection_settings/tag_types.rs +++ b/src/commands/connection_settings/tag_types.rs @@ -23,7 +23,9 @@ impl Command for TagTypes { let mut tagtypes = Vec::with_capacity(parts.len()); for (key, value) in parts.into_iter() { - debug_assert_eq!(key, "tagtype"); + if key != "tagtype" { + return Err(ResponseParserError::UnexpectedProperty(key)); + } let tagtype = expect_property_type!(Some(value), "tagtype", Text).to_string(); diff --git a/src/commands/partition_commands/listpartitions.rs b/src/commands/partition_commands/listpartitions.rs index 8f6ecff4..bec05925 100644 --- a/src/commands/partition_commands/listpartitions.rs +++ b/src/commands/partition_commands/listpartitions.rs @@ -23,7 +23,9 @@ impl Command for ListPartitions { let mut partitions = Vec::with_capacity(parts.len()); for (key, value) in parts.into_iter() { - debug_assert_eq!(key, "partition"); + if key != "partition" { + return Err(ResponseParserError::UnexpectedProperty(key)); + } let partition = expect_property_type!(Some(value), "partition", Text).to_string(); partitions.push(partition); } diff --git a/src/commands/queue/addid.rs b/src/commands/queue/addid.rs index fe708d6f..87f347f7 100644 --- a/src/commands/queue/addid.rs +++ b/src/commands/queue/addid.rs @@ -44,7 +44,9 @@ impl Command for AddId { 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)); + } Ok(AddIdResponse { id }) } } diff --git a/src/commands/stickers/sticker_find.rs b/src/commands/stickers/sticker_find.rs index b8e4f383..d6fb5764 100644 --- a/src/commands/stickers/sticker_find.rs +++ b/src/commands/stickers/sticker_find.rs @@ -82,8 +82,10 @@ impl Command for StickerFind { .get(1) .ok_or(ResponseParserError::UnexpectedEOF)?; - debug_assert!(sticker.0 == "sticker"); - // TODO: debug assert that this is a valid sticker type + if sticker.0 != "sticker" { + return Err(ResponseParserError::UnexpectedProperty(sticker.0)); + } + // TODO: check that this is a valid sticker type // debug_assert!(uri.0 == ""); let uri = expect_property_type!(Some(uri.1), uri.0, Text).to_string(); diff --git a/src/commands/stickers/sticker_list.rs b/src/commands/stickers/sticker_list.rs index 4af1aa61..f966c9a1 100644 --- a/src/commands/stickers/sticker_list.rs +++ b/src/commands/stickers/sticker_list.rs @@ -33,7 +33,9 @@ impl Command for StickerList { parts: ResponseAttributes<'_>, ) -> Result> { let parts: Vec<_> = parts.into(); - debug_assert!(parts.iter().all(|(k, _)| *k == "sticker")); + for part in parts.iter().filter(|part| part.0 != "sticker") { + return Err(ResponseParserError::UnexpectedProperty(part.0)); + } let result = parts .iter() diff --git a/src/commands/stickers/stickernames.rs b/src/commands/stickers/stickernames.rs index b17a92c5..aae9e3c6 100644 --- a/src/commands/stickers/stickernames.rs +++ b/src/commands/stickers/stickernames.rs @@ -20,7 +20,9 @@ impl Command for StickerNames { fn parse_response( parts: ResponseAttributes<'_>, ) -> Result> { - debug_assert!(parts.0.iter().all(|(k, _)| *k == "name")); + for (k, _) in parts.0.iter().filter(|(k, _)| *k != "name") { + return Err(ResponseParserError::UnexpectedProperty(k)); + } let list = parts .0 diff --git a/src/commands/stickers/stickernamestypes.rs b/src/commands/stickers/stickernamestypes.rs index 08c2b8ef..9d08f3af 100644 --- a/src/commands/stickers/stickernamestypes.rs +++ b/src/commands/stickers/stickernamestypes.rs @@ -30,8 +30,13 @@ impl Command for StickerNamesTypes { // TODO: don't depend on order, just make sure we have both let (name_key, name_value) = name_type_pair[0]; let (type_key, type_value) = name_type_pair[1]; - debug_assert!(name_key == "name"); - debug_assert!(type_key == "type"); + + if name_key != "name" { + return Err(ResponseParserError::UnexpectedProperty(name_key)); + } + if type_key != "type" { + return Err(ResponseParserError::UnexpectedProperty(type_key)); + } 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(); diff --git a/src/commands/stickers/stickertypes.rs b/src/commands/stickers/stickertypes.rs index 84e3727a..b79f3afe 100644 --- a/src/commands/stickers/stickertypes.rs +++ b/src/commands/stickers/stickertypes.rs @@ -20,7 +20,9 @@ impl Command for StickerTypes { fn parse_response( parts: ResponseAttributes<'_>, ) -> Result> { - debug_assert!(parts.0.iter().all(|(k, _)| *k == "stickertype")); + for (k, _) in parts.0.iter().filter(|(k, _)| *k != "stickertype") { + return Err(ResponseParserError::UnexpectedProperty(k)); + } let list = parts .0 diff --git a/src/commands/stored_playlists/listplaylist.rs b/src/commands/stored_playlists/listplaylist.rs index 067d4c20..1138980d 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,17 @@ impl Command for ListPlaylist { parts: ResponseAttributes<'_>, ) -> Result> { let parts: Vec<_> = parts.into(); - 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"), - ), - } - }) - .collect::, _>>() + let mut files = Vec::with_capacity(parts.len()); + for (key, value) in parts.into_iter() { + if key != "file" { + return Err(ResponseParserError::UnexpectedProperty(key)); + } + + let file = expect_property_type!(Some(value), "file", Text).to_string(); + + files.push(file); + } + + Ok(files) } }