1#![allow(clippy::module_name_repetitions)]
2pub mod library;
3use std::{fmt::Display, time::Duration};
4
5use mecomp_storage::db::schemas::song::Song;
6use serde::{Deserialize, Serialize};
7
8use crate::format_duration;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
11pub enum SeekType {
12 Absolute,
13 RelativeForwards,
14 RelativeBackwards,
15}
16
17impl Display for SeekType {
18 #[inline]
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 match self {
21 Self::Absolute => write!(f, "Absolute"),
22 Self::RelativeForwards => write!(f, "Forwards"),
23 Self::RelativeBackwards => write!(f, "Backwards"),
24 }
25 }
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
29pub enum RepeatMode {
30 #[default]
32 None,
33 One,
35 All,
37}
38
39impl Display for RepeatMode {
40 #[inline]
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 Self::None => write!(f, "None"),
44 Self::One => write!(f, "One"),
45 Self::All => write!(f, "All"),
46 }
47 }
48}
49
50impl RepeatMode {
51 #[must_use]
52 #[inline]
53 pub const fn is_none(&self) -> bool {
54 matches!(self, Self::None)
55 }
56
57 #[must_use]
58 #[inline]
59 pub const fn is_one(&self) -> bool {
60 matches!(self, Self::One)
61 }
62
63 #[must_use]
64 #[inline]
65 pub const fn is_all(&self) -> bool {
66 matches!(self, Self::All)
67 }
68}
69
70#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Serialize, Default)]
71pub struct Percent(f32);
72
73impl Percent {
74 #[must_use]
75 #[inline]
76 pub const fn new(value: f32) -> Self {
77 Self(if value.is_finite() {
78 value.clamp(0.0, 100.0)
79 } else {
80 0.0
81 })
82 }
83
84 #[must_use]
85 #[inline]
86 pub const fn into_inner(self) -> f32 {
87 self.0
88 }
89}
90
91impl Display for Percent {
92 #[inline]
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 write!(f, "{:.2}%", self.into_inner())
95 }
96}
97
98#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Default)]
100pub struct StateRuntime {
101 pub seek_position: Duration,
102 pub seek_percent: Percent,
103 pub duration: Duration,
104}
105
106impl Display for StateRuntime {
107 #[inline]
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 write!(
110 f,
111 "StateRuntime {{ seek_position: {}, seek_percent: {}, duration: {} }}",
112 format_duration(&self.seek_position),
113 self.seek_percent,
114 format_duration(&self.duration)
115 )
116 }
117}
118
119#[derive(
120 Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default,
121)]
122pub enum Status {
123 #[default]
124 Stopped,
125 Paused,
126 Playing,
127}
128
129impl Display for Status {
130 #[inline]
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 match self {
133 Self::Paused => write!(f, "Paused"),
134 Self::Playing => write!(f, "Playing"),
135 Self::Stopped => write!(f, "Stopped"),
136 }
137 }
138}
139
140#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
141pub struct StateAudio {
142 pub queue: Box<[Song]>,
143 pub queue_position: Option<usize>,
144 pub current_song: Option<Song>,
145 pub repeat_mode: RepeatMode,
146 pub runtime: Option<StateRuntime>,
147 pub status: Status,
148 pub muted: bool,
149 pub volume: f32,
150}
151
152impl Display for StateAudio {
153 #[inline]
154 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155 write!(
156 f,
157 "StateAudio {{ queue: {:?}, queue_position: {}, current_song: {}, repeat_mode: {}, runtime: {}, status: {}, muted: {}, volume: {:.0}% }}",
158 self.queue
159 .iter()
160 .map(|song| song.title.to_string())
161 .collect::<Vec<_>>(),
162 self.queue_position.map_or_else(|| "None".to_string(), |pos| pos.to_string()),
163 self.current_song.as_ref().map_or_else(|| "None".to_string(),|song| format!("\"{}\"",song.title)),
164 self.repeat_mode,
165 self.runtime.as_ref().map_or_else(|| "None".to_string(),std::string::ToString::to_string),
166 self.status,
167 self.muted,
168 self.volume * 100.0,
169 )
170 }
171}
172
173impl Default for StateAudio {
174 #[inline]
176 fn default() -> Self {
177 Self {
178 queue: Box::default(),
179 queue_position: None,
180 current_song: None,
181 repeat_mode: RepeatMode::default(),
182 runtime: None,
183 status: Status::default(),
184 muted: false,
185 volume: 1.0,
186 }
187 }
188}
189
190impl StateAudio {
191 #[must_use]
192 #[inline]
193 pub const fn paused(&self) -> bool {
194 !matches!(self.status, Status::Playing)
195 }
196}
197
198#[cfg(test)]
199mod tests {
200 use std::time::Duration;
201
202 use super::*;
203 use one_or_many::OneOrMany;
204 use pretty_assertions::{assert_eq, assert_str_eq};
205 use rstest::rstest;
206
207 #[test]
208 fn test_state_audio_default() {
209 let state = StateAudio::default();
210 assert_eq!(state.queue.as_ref(), &[]);
211 assert_eq!(state.queue_position, None);
212 assert_eq!(state.current_song, None);
213 assert_eq!(state.repeat_mode, RepeatMode::None);
214 assert_eq!(state.runtime, None);
215 assert_eq!(state.status, Status::Stopped);
216 assert_eq!(state.muted, false);
217 assert!(
218 f32::EPSILON > (state.volume - 1.0).abs(),
219 "{} != 1.0",
220 state.volume
221 );
222 }
223
224 #[rstest]
225 #[case::none(RepeatMode::None, [true, false, false])]
226 #[case::one(RepeatMode::One, [false, true, false])]
227 #[case::all(RepeatMode::All, [false, false, true])]
228 fn test_repeat_mode(#[case] mode: RepeatMode, #[case] expected: [bool; 3]) {
229 assert_eq!(mode.is_none(), expected[0]);
230 assert_eq!(mode.is_one(), expected[1]);
231 assert_eq!(mode.is_all(), expected[2]);
232 }
233
234 #[rstest]
235 #[case::seek_type(SeekType::Absolute, "Absolute")]
236 #[case::seek_type(SeekType::RelativeForwards, "Forwards")]
237 #[case::seek_type(SeekType::RelativeBackwards, "Backwards")]
238 #[case::repeat_mode(RepeatMode::None, "None")]
239 #[case::repeat_mode(RepeatMode::One, "One")]
240 #[case::repeat_mode(RepeatMode::All, "All")]
241 #[case::percent(Percent::new(50.0), "50.00%")]
242 #[case::state_runtimme(
243 StateRuntime {
244 seek_position: Duration::from_secs(3),
245 seek_percent: Percent::new(50.0),
246 duration: Duration::from_secs(6),
247 },
248 "StateRuntime { seek_position: 00:00:03.00, seek_percent: 50.00%, duration: 00:00:06.00 }"
249 )]
250 #[case::state_audio_empty(
251 StateAudio {
252 queue: Box::new([]),
253 queue_position: None,
254 current_song: None,
255 repeat_mode: RepeatMode::None,
256 runtime: None,
257 status: Status::Paused,
258 muted: false,
259 volume: 1.0,
260 },
261 "StateAudio { queue: [], queue_position: None, current_song: None, repeat_mode: None, runtime: None, status: Paused, muted: false, volume: 100% }"
262 )]
263 #[case::state_audio_empty(
264 StateAudio {
265 queue: Box::new([]),
266 queue_position: None,
267 current_song: None,
268 repeat_mode: RepeatMode::None,
269 runtime: None,
270 status: Status::Paused,
271 muted: false,
272 volume: 1.0,
273 },
274 "StateAudio { queue: [], queue_position: None, current_song: None, repeat_mode: None, runtime: None, status: Paused, muted: false, volume: 100% }"
275 )]
276 #[case::state_audio(
277 StateAudio {
278 queue: Box::new([
279 Song {
280 id: Song::generate_id(),
281 title: "Song 1".into(),
282 artist: OneOrMany::None,
283 album_artist: OneOrMany::None,
284 album: "album".into(),
285 genre: OneOrMany::None,
286 runtime: Duration::from_secs(100),
287 track: None,
288 disc: None,
289 release_year: None,
290 extension: "mp3".into(),
291 path: "foo/bar.mp3".into(),
292 }
293 ]),
294 queue_position: Some(1),
295 current_song: Some(
296 Song {
297 id: Song::generate_id(),
298 title: "Song 1".into(),
299 artist: OneOrMany::None,
300 album_artist: OneOrMany::None,
301 album: "album".into(),
302 genre: OneOrMany::None,
303 runtime: Duration::from_secs(100),
304 track: None,
305 disc: None,
306 release_year: None,
307 extension: "mp3".into(),
308 path: "foo/bar.mp3".into(),
309 }
310 ),
311 repeat_mode: RepeatMode::None,
312 runtime: Some(StateRuntime {
313 seek_position: Duration::from_secs(20),
314 seek_percent: Percent::new(20.0),
315 duration: Duration::from_secs(100),
316 }),
317 status: Status::Playing,
318 muted: false,
319 volume: 1.0,
320 },
321 "StateAudio { queue: [\"Song 1\"], queue_position: 1, current_song: \"Song 1\", repeat_mode: None, runtime: StateRuntime { seek_position: 00:00:20.00, seek_percent: 20.00%, duration: 00:01:40.00 }, status: Playing, muted: false, volume: 100% }"
322 )]
323 fn test_display_impls<T: Display>(#[case] input: T, #[case] expected: &str) {
324 assert_str_eq!(input.to_string(), expected);
325 }
326}