From 1cf14297530a2e7225f96b9647bdae471c4fbdea Mon Sep 17 00:00:00 2001 From: Sijmen Date: Tue, 16 May 2023 02:00:43 +0200 Subject: [PATCH] Add CommandResult::new and CommandResult::new_binary methods --- src/mpd.rs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/mpd.rs b/src/mpd.rs index ceef18e..74c6520 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -49,6 +49,20 @@ pub struct CommandResult { } impl CommandResult { + pub fn new(properties: Vec<(String, String)>) -> Self { + Self { + properties, + binary: None, + } + } + + pub fn new_binary(properties: Vec<(String, String)>, binary: Vec) -> Self { + Self { + properties, + binary: Some(binary), + } + } + pub fn into_hashmap(self) -> HashMap { self.properties.into_iter().collect() } @@ -142,17 +156,10 @@ impl Mpd { if key == "binary" { let binary = self.read_binary_data(value.parse()?).await?; - - break Ok(CommandResult { - properties, - binary: Some(binary), - }); + break Ok(CommandResult::new_binary(properties, binary)); } } else if buffer.starts_with("OK") { - break Ok(CommandResult { - properties, - binary: None, - }); + break Ok(CommandResult::new(properties)); } else if buffer.starts_with("ACK") { break Err(anyhow!(buffer)); } else { @@ -172,16 +179,10 @@ impl Mpd { if !binary.is_empty() { buffer.append(&mut binary); } else { - return Ok(CommandResult { - properties: result.properties, - binary: Some(buffer), - }); + return Ok(CommandResult::new_binary(result.properties, buffer)); } } else { - return Ok(CommandResult { - properties: result.properties, - binary: None, - }); + return Ok(CommandResult::new(result.properties)); } } }