about summary refs log tree commit diff stats
path: root/pkgs/by-name/mp/mpdpopm/src/storage
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/mp/mpdpopm/src/storage')
-rw-r--r--pkgs/by-name/mp/mpdpopm/src/storage/mod.rs145
1 files changed, 145 insertions, 0 deletions
diff --git a/pkgs/by-name/mp/mpdpopm/src/storage/mod.rs b/pkgs/by-name/mp/mpdpopm/src/storage/mod.rs
new file mode 100644
index 00000000..c13475ad
--- /dev/null
+++ b/pkgs/by-name/mp/mpdpopm/src/storage/mod.rs
@@ -0,0 +1,145 @@
+use anyhow::Result;
+
+pub mod play_count {
+    use anyhow::Context;
+
+    use crate::clients::Client;
+
+    use super::Result;
+
+    pub const STICKER: &str = "unwoundstack.com:playcount";
+
+    /// Retrieve the play count for a track
+    pub async fn get(client: &mut Client, file: &str) -> Result<Option<usize>> {
+        match client
+            .get_sticker::<usize>(file, STICKER)
+            .await
+            .context("Failed to get sticker from client")?
+        {
+            Some(n) => Ok(Some(n)),
+            None => Ok(None),
+        }
+    }
+
+    /// Set the play count for a track-- this will run the associated command, if any
+    pub async fn set(client: &mut Client, file: &str, play_count: usize) -> Result<()> {
+        client
+            .set_sticker(file, STICKER, &format!("{}", play_count))
+            .await
+            .context("Failed to set_sticker on client")?;
+
+        Ok(())
+    }
+
+    #[cfg(test)]
+    mod pc_lp_tests {
+        use super::*;
+        use crate::{clients::test_mock::Mock, storage::play_count};
+
+        /// "Smoke" tests for play counts & last played times
+        #[tokio::test]
+        async fn pc_smoke() {
+            let mock = Box::new(Mock::new(&[
+                (
+                    "sticker get song a unwoundstack.com:playcount",
+                    "sticker: unwoundstack.com:playcount=11\nOK\n",
+                ),
+                (
+                    "sticker get song a unwoundstack.com:playcount",
+                    "ACK [50@0] {sticker} no such sticker\n",
+                ),
+                ("sticker get song a unwoundstack.com:playcount", "splat!"),
+            ]));
+            let mut cli = Client::new(mock).unwrap();
+
+            assert_eq!(play_count::get(&mut cli, "a").await.unwrap().unwrap(), 11);
+            let val = play_count::get(&mut cli, "a").await.unwrap();
+            assert!(val.is_none());
+            play_count::get(&mut cli, "a").await.unwrap_err();
+        }
+    }
+}
+
+pub mod skipped {
+    use anyhow::Context;
+
+    use crate::clients::Client;
+
+    use super::Result;
+
+    pub(crate) const STICKER: &str = "unwoundstack.com:skipped_count";
+
+    /// Retrieve the skip count for a track
+    pub async fn get(client: &mut Client, file: &str) -> Result<Option<usize>> {
+        match client
+            .get_sticker::<usize>(file, STICKER)
+            .await
+            .context("Failed to get_sticker on client")?
+        {
+            Some(n) => Ok(Some(n)),
+            None => Ok(None),
+        }
+    }
+
+    /// Set the skip count for a track
+    pub async fn set(client: &mut Client, file: &str, skip_count: usize) -> Result<()> {
+        client
+            .set_sticker(file, STICKER, &format!("{}", skip_count))
+            .await
+            .context("Failed to set_sticker on client")
+    }
+}
+
+pub mod last_played {
+    use anyhow::Context;
+
+    use crate::clients::Client;
+
+    use super::Result;
+
+    pub const STICKER: &str = "unwoundstack.com:lastplayed";
+
+    /// Retrieve the last played timestamp for a track (seconds since Unix epoch)
+    pub async fn get(client: &mut Client, file: &str) -> Result<Option<u64>> {
+        client
+            .get_sticker::<u64>(file, STICKER)
+            .await
+            .context("Falied to get_sticker on client")
+    }
+
+    /// Set the last played for a track
+    pub async fn set(client: &mut Client, file: &str, last_played: u64) -> Result<()> {
+        client
+            .set_sticker(file, STICKER, &format!("{}", last_played))
+            .await
+            .context("Failed to set_sticker on client")?;
+        Ok(())
+    }
+}
+
+pub mod rating_count {
+    use anyhow::Context;
+
+    use crate::clients::Client;
+
+    use super::Result;
+
+    pub const STICKER: &str = "unwoundstack.com:ratings_count";
+
+    /// Retrieve the rating count for a track
+    pub async fn get(client: &mut Client, file: &str) -> Result<Option<i8>> {
+        client
+            .get_sticker::<i8>(file, STICKER)
+            .await
+            .context("Failed to get_sticker on client")
+    }
+
+    /// Set the rating count for a track
+    pub async fn set(client: &mut Client, file: &str, rating_count: i8) -> Result<()> {
+        client
+            .set_sticker(file, STICKER, &format!("{}", rating_count))
+            .await
+            .context("Failed to set_sticker on client")?;
+        Ok(())
+    }
+}