commands: make macros usable without use statements

This commit is contained in:
Oystein Kristoffer Tveit 2024-11-30 02:28:10 +01:00
parent 84f061a79f
commit 74074bf9c7
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
2 changed files with 45 additions and 37 deletions

View File

@ -139,18 +139,20 @@ impl<'a> From<ResponseAttributes<'a>> for HashMap<&'a str, GenericResponseValue<
macro_rules! get_property { macro_rules! get_property {
($parts:expr, $name:literal, $variant:ident) => { ($parts:expr, $name:literal, $variant:ident) => {
match $parts.get($name) { match $parts.get($name) {
Some(GenericResponseValue::$variant(value)) => *value, Some(crate::commands::GenericResponseValue::$variant(value)) => *value,
Some(value) => { Some(value) => {
let actual_type = match value { let actual_type = match value {
GenericResponseValue::Text(_) => "Text", crate::commands::GenericResponseValue::Text(_) => "Text",
GenericResponseValue::Binary(_) => "Binary", crate::commands::GenericResponseValue::Binary(_) => "Binary",
}; };
return Err(ResponseParserError::UnexpectedPropertyType( return Err(
$name, crate::commands::ResponseParserError::UnexpectedPropertyType(
actual_type, $name,
)); actual_type,
),
);
} }
None => return Err(ResponseParserError::MissingProperty($name)), None => return Err(crate::commands::ResponseParserError::MissingProperty($name)),
} }
}; };
} }
@ -158,16 +160,18 @@ macro_rules! get_property {
macro_rules! get_optional_property { macro_rules! get_optional_property {
($parts:expr, $name:literal, $variant:ident) => { ($parts:expr, $name:literal, $variant:ident) => {
match $parts.get($name) { match $parts.get($name) {
Some(GenericResponseValue::$variant(value)) => Some(*value), Some(crate::commands::GenericResponseValue::$variant(value)) => Some(*value),
Some(value) => { Some(value) => {
let actual_type = match value { let actual_type = match value {
GenericResponseValue::Text(_) => "Text", crate::commands::GenericResponseValue::Text(_) => "Text",
GenericResponseValue::Binary(_) => "Binary", crate::commands::GenericResponseValue::Binary(_) => "Binary",
}; };
return Err(ResponseParserError::UnexpectedPropertyType( return Err(
$name, crate::commands::ResponseParserError::UnexpectedPropertyType(
actual_type, $name,
)); actual_type,
),
);
} }
None => None, None => None,
} }
@ -177,20 +181,22 @@ macro_rules! get_optional_property {
macro_rules! get_and_parse_property { macro_rules! get_and_parse_property {
($parts:ident, $name:literal, $variant:ident) => { ($parts:ident, $name:literal, $variant:ident) => {
match $parts.get($name) { match $parts.get($name) {
Some(GenericResponseValue::$variant(value)) => (*value) Some(crate::commands::GenericResponseValue::$variant(value)) => (*value)
.parse() .parse()
.map_err(|_| ResponseParserError::InvalidProperty($name, value))?, .map_err(|_| crate::commands::ResponseParserError::InvalidProperty($name, value))?,
Some(value) => { Some(value) => {
let actual_type = match value { let actual_type = match value {
GenericResponseValue::Text(_) => "Text", crate::commands::GenericResponseValue::Text(_) => "Text",
GenericResponseValue::Binary(_) => "Binary", crate::commands::GenericResponseValue::Binary(_) => "Binary",
}; };
return Err(ResponseParserError::UnexpectedPropertyType( return Err(
$name, crate::commands::ResponseParserError::UnexpectedPropertyType(
actual_type, $name,
)); actual_type,
),
);
} }
None => return Err(ResponseParserError::MissingProperty($name)), None => return Err(crate::commands::ResponseParserError::MissingProperty($name)),
} }
}; };
} }
@ -198,20 +204,22 @@ macro_rules! get_and_parse_property {
macro_rules! get_and_parse_optional_property { macro_rules! get_and_parse_optional_property {
($parts:ident, $name:literal, $variant:ident) => { ($parts:ident, $name:literal, $variant:ident) => {
match $parts.get($name) { match $parts.get($name) {
Some(GenericResponseValue::$variant(value)) => Some( Some(crate::commands::GenericResponseValue::$variant(value)) => {
(*value) Some((*value).parse().map_err(|_| {
.parse() crate::commands::ResponseParserError::InvalidProperty($name, value)
.map_err(|_| ResponseParserError::InvalidProperty($name, value))?, })?)
), }
Some(value) => { Some(value) => {
let actual_type = match value { let actual_type = match value {
GenericResponseValue::Text(_) => "Text", crate::commands::GenericResponseValue::Text(_) => "Text",
GenericResponseValue::Binary(_) => "Binary", crate::commands::GenericResponseValue::Binary(_) => "Binary",
}; };
return Err(ResponseParserError::UnexpectedPropertyType( return Err(
$name, crate::commands::ResponseParserError::UnexpectedPropertyType(
actual_type, $name,
)); actual_type,
),
);
} }
None => None, None => None,
} }

View File

@ -2,8 +2,8 @@ use std::{collections::HashMap, str::FromStr};
use crate::{ use crate::{
commands::{ commands::{
get_property, Command, GenericResponseValue, Request, RequestParserResult, get_property, Command, Request, RequestParserResult, ResponseAttributes,
ResponseAttributes, ResponseParserError, ResponseParserError,
}, },
request::ReplayGainModeMode, request::ReplayGainModeMode,
}; };