1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
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<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;
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(())
}
}
|