Move split_properties to CommandResult
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Sijmen 2023-05-14 15:40:51 +02:00
parent 41a06eb9d1
commit 8e7a087e41
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
2 changed files with 33 additions and 36 deletions

View File

@ -50,8 +50,8 @@ struct PlayerTemplate<'a> {
async fn get_player(_req: tide::Request<()>) -> tide::Result { async fn get_player(_req: tide::Request<()>) -> tide::Result {
let mut mpd = mpd::Mpd::connect().await?; let mut mpd = mpd::Mpd::connect().await?;
let song = mpd.command("currentsong").await?.as_hashmap(); let song = mpd.command("currentsong").await?.into_hashmap();
let status = mpd.command("status").await?.as_hashmap(); let status = mpd.command("status").await?.into_hashmap();
let elapsed = status["elapsed"].parse().unwrap_or(0.0); let elapsed = status["elapsed"].parse().unwrap_or(0.0);
let duration = status["duration"].parse().unwrap_or(1.0); let duration = status["duration"].parse().unwrap_or(1.0);

View File

@ -49,8 +49,32 @@ pub struct CommandResult {
} }
impl CommandResult { impl CommandResult {
pub fn as_hashmap<'a>(&'a self) -> HashMap<String, String> { pub fn into_hashmap(self) -> HashMap<String, String> {
self.properties.iter().cloned().collect() self.properties.into_iter().collect()
}
pub fn into_hashmaps(self, split_at: &[&str]) -> Vec<HashMap<String, String>> {
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<HashMap<String, String>> {
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<Vec<Entry>> { pub async fn ls(&mut self, path: &str) -> anyhow::Result<Vec<Entry>> {
fn get_filename(path: &str) -> String { fn get_filename(path: &str) -> String {
@ -259,11 +257,10 @@ impl Mpd {
let result = self let result = self
.command(&format!("lsinfo \"{}\"", Self::escape_str(&path))) .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 = result
let files = props
.iter() .iter()
.flat_map(|prop| { .flat_map(|prop| {
if let Some(file) = prop.get("file") { if let Some(file) = prop.get("file") {
@ -292,11 +289,11 @@ impl Mpd {
} }
pub async fn playlist(&mut self) -> anyhow::Result<Vec<QueueItem>> { pub async fn playlist(&mut self) -> anyhow::Result<Vec<QueueItem>> {
let status = self.command("status").await?.as_hashmap(); let status = self.command("status").await?.into_hashmap();
let current_songid = status.get("songid"); let current_songid = status.get("songid");
let playlistinfo = self.command("playlistinfo").await?; let playlistinfo = self.command("playlistinfo").await?;
let queue = Self::split_properties(playlistinfo.properties, &["file"]); let queue = playlistinfo.into_hashmaps(&["file"]);
let queue = queue let queue = queue
.iter() .iter()