use anyhow::{Error, 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> { match client .get_sticker::(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; const STICKER: &str = "unwoundstack.com:skipped_count"; /// Retrieve the skip count for a track pub async fn get(client: &mut Client, file: &str) -> Result> { match client .get_sticker::(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> { client .get_sticker::(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> { client .get_sticker::(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(()) } }