Add CommandResult::new and CommandResult::new_binary methods

This commit is contained in:
Sijmen 2023-05-16 02:00:43 +02:00
parent 05dddfde6d
commit 1cf1429753
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -49,6 +49,20 @@ pub struct CommandResult {
} }
impl 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<u8>) -> Self {
Self {
properties,
binary: Some(binary),
}
}
pub fn into_hashmap(self) -> HashMap<String, String> { pub fn into_hashmap(self) -> HashMap<String, String> {
self.properties.into_iter().collect() self.properties.into_iter().collect()
} }
@ -142,17 +156,10 @@ impl Mpd {
if key == "binary" { if key == "binary" {
let binary = self.read_binary_data(value.parse()?).await?; let binary = self.read_binary_data(value.parse()?).await?;
break Ok(CommandResult::new_binary(properties, binary));
break Ok(CommandResult {
properties,
binary: Some(binary),
});
} }
} else if buffer.starts_with("OK") { } else if buffer.starts_with("OK") {
break Ok(CommandResult { break Ok(CommandResult::new(properties));
properties,
binary: None,
});
} else if buffer.starts_with("ACK") { } else if buffer.starts_with("ACK") {
break Err(anyhow!(buffer)); break Err(anyhow!(buffer));
} else { } else {
@ -172,16 +179,10 @@ impl Mpd {
if !binary.is_empty() { if !binary.is_empty() {
buffer.append(&mut binary); buffer.append(&mut binary);
} else { } else {
return Ok(CommandResult { return Ok(CommandResult::new_binary(result.properties, buffer));
properties: result.properties,
binary: Some(buffer),
});
} }
} else { } else {
return Ok(CommandResult { return Ok(CommandResult::new(result.properties));
properties: result.properties,
binary: None,
});
} }
} }
} }