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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
use std::path::PathBuf;
use backtrace::Backtrace;
#[derive(Debug)]
pub enum Error {
PlayerStopped,
BadPath {
pth: PathBuf,
},
SystemTime {
source: std::time::SystemTimeError,
back: Backtrace,
},
Client {
source: crate::clients::Error,
back: Backtrace,
},
Rating {
source: crate::ratings::Error,
back: Backtrace,
},
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::PlayerStopped => write!(f, "The MPD player is stopped"),
Error::BadPath { pth } => write!(f, "Bad path: {:?}", pth),
Error::SystemTime { source, back: _ } => {
write!(f, "Couldn't get system time: {}", source)
}
Error::Client { source, back: _ } => write!(f, "Client error: {}", source),
Error::Rating { source, back: _ } => write!(f, "Rating error: {}", source),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self {
Error::SystemTime { source, back: _ } => Some(source),
Error::Client { source, back: _ } => Some(source),
_ => None,
}
}
}
type Result<T> = std::result::Result<T, Error>;
pub mod play_count {
use backtrace::Backtrace;
use crate::clients::Client;
use super::{Error, 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
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})? {
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
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})?;
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 pc", "sticker: pc=11\nOK\n"),
(
"sticker get song a pc",
"ACK [50@0] {sticker} no such sticker\n",
),
("sticker get song a pc", "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 backtrace::Backtrace;
use crate::clients::Client;
use super::{Error, 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
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})? {
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
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})
}
}
pub mod last_played {
use backtrace::Backtrace;
use crate::clients::Client;
use super::{Error, 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
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})
}
/// 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
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})?;
Ok(())
}
}
pub mod rating_count {
use backtrace::Backtrace;
use crate::clients::Client;
use super::{Error, 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<u8>> {
client
.get_sticker::<u8>(file, STICKER)
.await
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})
}
/// Set the rating count for a track
pub async fn set(client: &mut Client, file: &str, rating_count: u8) -> Result<()> {
client
.set_sticker(file, STICKER, &format!("{}", rating_count))
.await
.map_err(|err| Error::Client {
source: err,
back: Backtrace::new(),
})?;
Ok(())
}
}
|