commands: return runtime errors on invalid property names

This commit is contained in:
2025-11-21 15:07:50 +09:00
parent 130fe49597
commit ede28623ef
9 changed files with 43 additions and 24 deletions

View File

@@ -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();

View File

@@ -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);
}

View File

@@ -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 })
}
}

View File

@@ -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();

View File

@@ -33,7 +33,9 @@ impl Command for StickerList {
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
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()

View File

@@ -20,7 +20,9 @@ impl Command for StickerNames {
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
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

View File

@@ -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();

View File

@@ -20,7 +20,9 @@ impl Command for StickerTypes {
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
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

View File

@@ -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<Self::Response, ResponseParserError<'_>> {
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::<Result<Vec<_>, _>>()
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)
}
}