diff --git a/src/commands.rs b/src/commands.rs index 7d2ce2c..c3a8aa4 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -38,11 +38,12 @@ pub trait Command { // TODO: Replace the HashMap datastructure with something that allows keeping // duplicate keys and order of insertion - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, - ) -> Result>; + fn parse_response( + parts: ResponseAttributes<'_>, + ) -> Result>; fn parse_raw_response(raw: &str) -> Result> { + // TODO: Fix the implementation to use ResponseAttributes natively let mut parts = HashMap::new(); let mut lines = raw.lines(); loop { @@ -67,7 +68,7 @@ pub trait Command { parts.insert(key, GenericResponseValue::Text(value)); } - Self::parse_response(parts) + Self::parse_response(parts.into()) } } @@ -101,9 +102,40 @@ pub type GenericResponse<'a> = HashMap<&'a str, GenericResponseValue<'a>>; pub enum GenericResponseValue<'a> { Text(&'a str), Binary(&'a [u8]), - Many(Vec>), + // Many(Vec>), } +pub struct ResponseAttributes<'a>(Vec<(&'a str, GenericResponseValue<'a>)>); + +// impl ResponseAttributes<'_> { +// pub fn +// pub fn get<'a>(&self, key: &str) -> Option<&GenericResponseValue<'a>> { +// self.0.iter().find_map(|(k, v)| if k == &key { Some(v) } else { None }) +// } +// } + +impl ResponseAttributes<'_> { + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +impl<'a> From>> for ResponseAttributes<'a> { + fn from(map: HashMap<&'a str, GenericResponseValue<'a>>) -> Self { + Self(map.into_iter().collect()) + } +} + +impl<'a> From> for HashMap<&'a str, GenericResponseValue<'a>> { + fn from(val: ResponseAttributes<'a>) -> Self { + val.0.into_iter().collect() + } +} + +/*******************/ +/* Parsing Helpers */ +/*******************/ + macro_rules! get_property { ($parts:expr, $name:literal, $variant:ident) => { match $parts.get($name) { @@ -112,7 +144,6 @@ macro_rules! get_property { let actual_type = match value { GenericResponseValue::Text(_) => "Text", GenericResponseValue::Binary(_) => "Binary", - GenericResponseValue::Many(_) => "Many", }; return Err(ResponseParserError::UnexpectedPropertyType( $name, @@ -132,7 +163,6 @@ macro_rules! get_optional_property { let actual_type = match value { GenericResponseValue::Text(_) => "Text", GenericResponseValue::Binary(_) => "Binary", - GenericResponseValue::Many(_) => "Many", }; return Err(ResponseParserError::UnexpectedPropertyType( $name, @@ -154,7 +184,6 @@ macro_rules! get_and_parse_property { let actual_type = match value { GenericResponseValue::Text(_) => "Text", GenericResponseValue::Binary(_) => "Binary", - GenericResponseValue::Many(_) => "Many", }; return Err(ResponseParserError::UnexpectedPropertyType( $name, @@ -178,7 +207,6 @@ macro_rules! get_and_parse_optional_property { let actual_type = match value { GenericResponseValue::Text(_) => "Text", GenericResponseValue::Binary(_) => "Binary", - GenericResponseValue::Many(_) => "Many", }; return Err(ResponseParserError::UnexpectedPropertyType( $name, diff --git a/src/commands/audio_output_devices/disableoutput.rs b/src/commands/audio_output_devices/disableoutput.rs index ceb7362..df3a565 100644 --- a/src/commands/audio_output_devices/disableoutput.rs +++ b/src/commands/audio_output_devices/disableoutput.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -19,9 +17,9 @@ impl Command for DisableOutput { Ok((Request::DisableOutput(output_id.to_string()), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, - ) -> Result { + fn parse_response( + parts: ResponseAttributes<'_>, + ) -> Result> { debug_assert!(parts.is_empty()); Ok(()) } diff --git a/src/commands/audio_output_devices/enableoutput.rs b/src/commands/audio_output_devices/enableoutput.rs index b2c9034..ab5b555 100644 --- a/src/commands/audio_output_devices/enableoutput.rs +++ b/src/commands/audio_output_devices/enableoutput.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -19,8 +17,8 @@ impl Command for EnableOutput { Ok((Request::EnableOutput(output_id.to_string()), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/audio_output_devices/outputs.rs b/src/commands/audio_output_devices/outputs.rs index 3a388ab..763bb75 100644 --- a/src/commands/audio_output_devices/outputs.rs +++ b/src/commands/audio_output_devices/outputs.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Outputs; @@ -26,8 +26,8 @@ impl Command for Outputs { Ok((Request::Outputs, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { todo!() // debug_assert!(parts.is_empty()); diff --git a/src/commands/audio_output_devices/outputset.rs b/src/commands/audio_output_devices/outputset.rs index 0833936..0e55dcb 100644 --- a/src/commands/audio_output_devices/outputset.rs +++ b/src/commands/audio_output_devices/outputset.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -28,8 +26,8 @@ impl Command for OutputSet { )) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/audio_output_devices/toggleoutput.rs b/src/commands/audio_output_devices/toggleoutput.rs index 43586af..8601f55 100644 --- a/src/commands/audio_output_devices/toggleoutput.rs +++ b/src/commands/audio_output_devices/toggleoutput.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -19,8 +17,8 @@ impl Command for ToggleOutput { Ok((Request::ToggleOutput(output_id.to_string()), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/client_to_client/channels.rs b/src/commands/client_to_client/channels.rs index 220f1e2..5db9c4d 100644 --- a/src/commands/client_to_client/channels.rs +++ b/src/commands/client_to_client/channels.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Channels; @@ -21,8 +19,8 @@ impl Command for Channels { Ok((Request::Channels, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { todo!() // let channels = parts diff --git a/src/commands/client_to_client/readmessages.rs b/src/commands/client_to_client/readmessages.rs index 183c8d0..7e635a4 100644 --- a/src/commands/client_to_client/readmessages.rs +++ b/src/commands/client_to_client/readmessages.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct ReadMessages; @@ -21,8 +19,8 @@ impl Command for ReadMessages { Ok((Request::ReadMessages, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { todo!() } diff --git a/src/commands/client_to_client/sendmessage.rs b/src/commands/client_to_client/sendmessage.rs index 2f1c5ee..292b1bc 100644 --- a/src/commands/client_to_client/sendmessage.rs +++ b/src/commands/client_to_client/sendmessage.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -22,8 +20,8 @@ impl Command for SendMessage { Ok((Request::SendMessage(channel.to_string(), message), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/client_to_client/subscribe.rs b/src/commands/client_to_client/subscribe.rs index 96d94da..d76f175 100644 --- a/src/commands/client_to_client/subscribe.rs +++ b/src/commands/client_to_client/subscribe.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -19,8 +17,8 @@ impl Command for Subscribe { Ok((Request::Subscribe(channel_name.to_string()), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/client_to_client/unsubscribe.rs b/src/commands/client_to_client/unsubscribe.rs index d688e26..0a3ff59 100644 --- a/src/commands/client_to_client/unsubscribe.rs +++ b/src/commands/client_to_client/unsubscribe.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -19,8 +17,8 @@ impl Command for Unsubscribe { Ok((Request::Unsubscribe(channel_name.to_string()), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/next.rs b/src/commands/controlling_playback/next.rs index 667133d..aff182f 100644 --- a/src/commands/controlling_playback/next.rs +++ b/src/commands/controlling_playback/next.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Next; @@ -15,8 +13,8 @@ impl Command for Next { Ok((Request::Next, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/pause.rs b/src/commands/controlling_playback/pause.rs index eb2d81c..8170117 100644 --- a/src/commands/controlling_playback/pause.rs +++ b/src/commands/controlling_playback/pause.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -20,8 +18,8 @@ impl Command for Pause { } } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/play.rs b/src/commands/controlling_playback/play.rs index 9460e7b..6dfd166 100644 --- a/src/commands/controlling_playback/play.rs +++ b/src/commands/controlling_playback/play.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::SongPosition, @@ -27,8 +25,8 @@ impl Command for Play { Ok((Request::Play(songpos), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/playid.rs b/src/commands/controlling_playback/playid.rs index 94e9c62..112f2e9 100644 --- a/src/commands/controlling_playback/playid.rs +++ b/src/commands/controlling_playback/playid.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::SongId, @@ -27,8 +25,8 @@ impl Command for PlayId { Ok((Request::PlayId(songid), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/previous.rs b/src/commands/controlling_playback/previous.rs index ec70a97..fa61a14 100644 --- a/src/commands/controlling_playback/previous.rs +++ b/src/commands/controlling_playback/previous.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Previous; @@ -15,8 +13,8 @@ impl Command for Previous { Ok((Request::Previous, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/seek.rs b/src/commands/controlling_playback/seek.rs index 8a92ab3..742a396 100644 --- a/src/commands/controlling_playback/seek.rs +++ b/src/commands/controlling_playback/seek.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::{SongPosition, TimeWithFractions}, @@ -32,8 +30,8 @@ impl Command for Seek { Ok((Request::Seek(songpos, time), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/seekcur.rs b/src/commands/controlling_playback/seekcur.rs index 48c9489..99597e2 100644 --- a/src/commands/controlling_playback/seekcur.rs +++ b/src/commands/controlling_playback/seekcur.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::TimeWithFractions, @@ -45,8 +43,8 @@ impl Command for SeekCur { Ok((Request::SeekCur(mode, time), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/seekid.rs b/src/commands/controlling_playback/seekid.rs index 51b7f94..ef45dbf 100644 --- a/src/commands/controlling_playback/seekid.rs +++ b/src/commands/controlling_playback/seekid.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::{SongId, TimeWithFractions}, @@ -32,8 +30,8 @@ impl Command for SeekId { Ok((Request::SeekId(songid, time), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/controlling_playback/stop.rs b/src/commands/controlling_playback/stop.rs index c4c246a..dd9937c 100644 --- a/src/commands/controlling_playback/stop.rs +++ b/src/commands/controlling_playback/stop.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Stop; @@ -15,8 +13,8 @@ impl Command for Stop { Ok((Request::Stop, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/consume.rs b/src/commands/playback_options/consume.rs index ced63b1..727933a 100644 --- a/src/commands/playback_options/consume.rs +++ b/src/commands/playback_options/consume.rs @@ -1,7 +1,7 @@ -use std::{collections::HashMap, str::FromStr}; +use std::str::FromStr; use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -23,8 +23,8 @@ impl Command for Consume { Ok((Request::Consume(state), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/crossfade.rs b/src/commands/playback_options/crossfade.rs index 6779d2c..b94ee99 100644 --- a/src/commands/playback_options/crossfade.rs +++ b/src/commands/playback_options/crossfade.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::Seconds, @@ -27,8 +25,8 @@ impl Command for Crossfade { Ok((Request::Crossfade(seconds), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/getvol.rs b/src/commands/playback_options/getvol.rs index 36e319e..1278bb3 100644 --- a/src/commands/playback_options/getvol.rs +++ b/src/commands/playback_options/getvol.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::{ - commands::{Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError}, + commands::{Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError}, request::Volume, }; @@ -16,8 +14,8 @@ impl Command for GetVol { Ok((Request::GetVol, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { todo!() // let volume = get_property!(parts, Volume, "volume"); diff --git a/src/commands/playback_options/mixrampdb.rs b/src/commands/playback_options/mixrampdb.rs index 165b674..b80e94d 100644 --- a/src/commands/playback_options/mixrampdb.rs +++ b/src/commands/playback_options/mixrampdb.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -24,8 +22,8 @@ impl Command for MixRampDb { Ok((Request::MixRampDb(db), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/mixrampdelay.rs b/src/commands/playback_options/mixrampdelay.rs index 4e381d3..c85224f 100644 --- a/src/commands/playback_options/mixrampdelay.rs +++ b/src/commands/playback_options/mixrampdelay.rs @@ -1,8 +1,6 @@ -use std::collections::HashMap; - use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, common::Seconds, @@ -27,8 +25,8 @@ impl Command for MixRampDelay { Ok((Request::MixRampDelay(seconds), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/random.rs b/src/commands/playback_options/random.rs index a2521a9..1445e53 100644 --- a/src/commands/playback_options/random.rs +++ b/src/commands/playback_options/random.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -24,8 +22,8 @@ impl Command for Random { Ok((Request::Random(state), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/repeat.rs b/src/commands/playback_options/repeat.rs index 65646a7..7432380 100644 --- a/src/commands/playback_options/repeat.rs +++ b/src/commands/playback_options/repeat.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -24,8 +22,8 @@ impl Command for Repeat { Ok((Request::Repeat(state), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/replay_gain_mode.rs b/src/commands/playback_options/replay_gain_mode.rs index db9e81f..5af7db7 100644 --- a/src/commands/playback_options/replay_gain_mode.rs +++ b/src/commands/playback_options/replay_gain_mode.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, str::FromStr}; +use std::str::FromStr; use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, request::ReplayGainModeMode, @@ -26,8 +26,8 @@ impl Command for ReplayGainMode { Ok((Request::ReplayGainMode(mode), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/replay_gain_status.rs b/src/commands/playback_options/replay_gain_status.rs index 7d9d806..464766c 100644 --- a/src/commands/playback_options/replay_gain_status.rs +++ b/src/commands/playback_options/replay_gain_status.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, str::FromStr}; use crate::{ commands::{ get_property, Command, GenericResponseValue, Request, RequestParserResult, - ResponseParserError, + ResponseAttributes, ResponseParserError, }, request::ReplayGainModeMode, }; @@ -23,9 +23,10 @@ impl Command for ReplayGainStatus { Ok((Request::ReplayGainStatus, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { + let parts: HashMap<_, _> = parts.into(); let replay_gain_mode = get_property!(parts, "replay_gain_mode", Text); Ok(ReplayGainStatusResponse { diff --git a/src/commands/playback_options/setvol.rs b/src/commands/playback_options/setvol.rs index 1c1fdb0..860969b 100644 --- a/src/commands/playback_options/setvol.rs +++ b/src/commands/playback_options/setvol.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, str::FromStr}; +use std::str::FromStr; use crate::{ commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }, request::Volume, @@ -27,8 +27,8 @@ impl Command for SetVol { Ok((Request::SetVol(volume), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/single.rs b/src/commands/playback_options/single.rs index 6ea42d5..e9210d2 100644 --- a/src/commands/playback_options/single.rs +++ b/src/commands/playback_options/single.rs @@ -1,7 +1,7 @@ -use std::{collections::HashMap, str::FromStr}; +use std::str::FromStr; use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -23,8 +23,8 @@ impl Command for Single { Ok((Request::Single(state), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/playback_options/volume.rs b/src/commands/playback_options/volume.rs index e559ce1..a00a30e 100644 --- a/src/commands/playback_options/volume.rs +++ b/src/commands/playback_options/volume.rs @@ -1,7 +1,7 @@ -use std::{collections::HashMap, str::FromStr}; +use std::str::FromStr; use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserError, RequestParserResult, + Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError, }; @@ -23,8 +23,8 @@ impl Command for Volume { Ok((Request::Volume(change), "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/querying_mpd_status/clearerror.rs b/src/commands/querying_mpd_status/clearerror.rs index 5f1b369..76a84b3 100644 --- a/src/commands/querying_mpd_status/clearerror.rs +++ b/src/commands/querying_mpd_status/clearerror.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct ClearError; @@ -16,8 +14,8 @@ impl Command for ClearError { Ok((Request::ClearError, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/querying_mpd_status/currentsong.rs b/src/commands/querying_mpd_status/currentsong.rs index 460f6b7..871ebb3 100644 --- a/src/commands/querying_mpd_status/currentsong.rs +++ b/src/commands/querying_mpd_status/currentsong.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct CurrentSong; @@ -18,8 +16,8 @@ impl Command for CurrentSong { Ok((Request::CurrentSong, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { todo!() } diff --git a/src/commands/querying_mpd_status/idle.rs b/src/commands/querying_mpd_status/idle.rs index 340fa40..ce8fef6 100644 --- a/src/commands/querying_mpd_status/idle.rs +++ b/src/commands/querying_mpd_status/idle.rs @@ -3,7 +3,7 @@ use std::str::{FromStr, SplitWhitespace}; use crate::common::SubSystem; use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Idle; @@ -24,8 +24,8 @@ impl Command for Idle { }) } - fn parse_response<'a>( - parts: std::collections::HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { debug_assert!(parts.is_empty()); Ok(()) diff --git a/src/commands/querying_mpd_status/stats.rs b/src/commands/querying_mpd_status/stats.rs index 37bc7c6..a101a7c 100644 --- a/src/commands/querying_mpd_status/stats.rs +++ b/src/commands/querying_mpd_status/stats.rs @@ -1,9 +1,7 @@ -use std::collections::HashMap; - use serde::{Deserialize, Serialize}; use crate::commands::{ - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError, }; pub struct Stats; @@ -29,8 +27,8 @@ impl Command for Stats { Ok((Request::Stats, "")) } - fn parse_response<'a>( - parts: HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { todo!() } diff --git a/src/commands/querying_mpd_status/status.rs b/src/commands/querying_mpd_status/status.rs index 7498d3c..ecfa342 100644 --- a/src/commands/querying_mpd_status/status.rs +++ b/src/commands/querying_mpd_status/status.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::str::FromStr; use serde::{Deserialize, Serialize}; @@ -6,7 +7,8 @@ use crate::common::{Audio, BoolOrOneshot, SongId, SongPosition}; use crate::commands::{ get_and_parse_optional_property, get_and_parse_property, get_optional_property, get_property, - Command, GenericResponseValue, Request, RequestParserResult, ResponseParserError, + Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes, + ResponseParserError, }; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -59,9 +61,10 @@ pub struct StatusResponse { } #[inline] -fn parse_status_response<'a>( - parts: std::collections::HashMap<&'a str, GenericResponseValue<'a>>, +fn parse_status_response( + parts: ResponseAttributes<'_>, ) -> Result { + let parts: HashMap<&str, GenericResponseValue> = parts.into(); let partition = get_property!(parts, "partition", Text).to_string(); let volume = match get_property!(parts, "volume", Text) { @@ -163,8 +166,8 @@ impl Command for Status { Ok((Request::Status, "")) } - fn parse_response<'a>( - parts: std::collections::HashMap<&'a str, GenericResponseValue<'a>>, + fn parse_response( + parts: ResponseAttributes<'_>, ) -> Result { parse_status_response(parts) }