From 8e7a087e41255a7d2223d6bd1e02c231a166384d Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sun, 14 May 2023 15:40:51 +0200 Subject: [PATCH] Move split_properties to CommandResult --- src/main.rs | 4 ++-- src/mpd.rs | 65 +++++++++++++++++++++++++---------------------------- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7025200..7270417 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,8 @@ struct PlayerTemplate<'a> { async fn get_player(_req: tide::Request<()>) -> tide::Result { let mut mpd = mpd::Mpd::connect().await?; - let song = mpd.command("currentsong").await?.as_hashmap(); - let status = mpd.command("status").await?.as_hashmap(); + let song = mpd.command("currentsong").await?.into_hashmap(); + let status = mpd.command("status").await?.into_hashmap(); let elapsed = status["elapsed"].parse().unwrap_or(0.0); let duration = status["duration"].parse().unwrap_or(1.0); diff --git a/src/mpd.rs b/src/mpd.rs index ec8e981..287ab08 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -49,8 +49,32 @@ pub struct CommandResult { } impl CommandResult { - pub fn as_hashmap<'a>(&'a self) -> HashMap { - self.properties.iter().cloned().collect() + pub fn into_hashmap(self) -> HashMap { + self.properties.into_iter().collect() + } + + pub fn into_hashmaps(self, split_at: &[&str]) -> Vec> { + let mut output = Vec::new(); + let mut current = None; + + for (key, value) in self.properties { + if split_at.contains(&key.as_str()) { + if let Some(current) = current { + output.push(current); + } + current = Some(HashMap::new()); + } + + if let Some(current) = current.as_mut() { + current.insert(key, value); + } + } + + if let Some(current) = current { + output.push(current); + } + + output } } @@ -222,32 +246,6 @@ impl Mpd { } } - pub fn split_properties( - properties: Vec<(String, String)>, - at: &[&str], - ) -> Vec> { - let mut output = Vec::new(); - let mut current = None; - - for (key, value) in properties { - if at.contains(&key.as_str()) { - if let Some(current) = current { - output.push(current); - } - current = Some(HashMap::new()); - } - - if let Some(current) = current.as_mut() { - current.insert(key, value); - } - } - - if let Some(current) = current { - output.push(current); - } - - output - } pub async fn ls(&mut self, path: &str) -> anyhow::Result> { fn get_filename(path: &str) -> String { @@ -259,11 +257,10 @@ impl Mpd { let result = self .command(&format!("lsinfo \"{}\"", Self::escape_str(&path))) - .await?; + .await? + .into_hashmaps(&["file", "directory", "playlist"]); - let props = Self::split_properties(result.properties, &["file", "directory", "playlist"]); - - let files = props + let files = result .iter() .flat_map(|prop| { if let Some(file) = prop.get("file") { @@ -292,11 +289,11 @@ impl Mpd { } pub async fn playlist(&mut self) -> anyhow::Result> { - let status = self.command("status").await?.as_hashmap(); + let status = self.command("status").await?.into_hashmap(); let current_songid = status.get("songid"); let playlistinfo = self.command("playlistinfo").await?; - let queue = Self::split_properties(playlistinfo.properties, &["file"]); + let queue = playlistinfo.into_hashmaps(&["file"]); let queue = queue .iter()