-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplaylist.rs
More file actions
313 lines (287 loc) · 9.56 KB
/
playlist.rs
File metadata and controls
313 lines (287 loc) · 9.56 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// music struct
struct Music {
title: String,
artist: String,
album: String,
duration: u32, // in seconds
liked: bool,
}
// playlist struct
struct Playlist {
name: String,
songs: Vec<Music>,
current: usize, // index of the current song
}
impl Playlist {
// create a new playlist with a name
fn new(name: String) -> Self {
Self {
name,
songs: Vec::new(),
current: 0,
}
}
// add a song to the playlist
fn add_song(&mut self, song: Music) {
// check if the song already exists in the playlist
if self.songs.iter().any(|s| s.title == song.title) {
println!("This song already exists in the playlist.");
} else {
self.songs.push(song);
}
}
// remove a song from the playlist by title
fn remove_song(&mut self, title: &str) {
// find the index of the song to remove
if let Some(index) = self.songs.iter().position(|s| s.title == title) {
self.songs.remove(index);
println!("{} removed from the playlist.", title);
} else {
println!("This song does not exist in the playlist.");
}
}
// toggle the like status of the current song
fn toggle_like(&mut self) {
// get the current song as a mutable reference
if let Some(song) = self.songs.get_mut(self.current) {
song.liked = !song.liked;
println!(
"You {} {} song.",
if song.liked { "liked" } else { "unliked" },
song.title
);
} else {
println!("There is no song playing.");
}
}
// print the current song
fn now_playing(&self) -> String {
// get the current song as an immutable reference
if let Some(song) = self.songs.get(self.current) {
format!(
"Now playing: {} by {} from {} ({:02}:{:02})",
song.title,
song.artist,
song.album,
song.duration / 60,
song.duration % 60
)
} else {
format!("There is no song playing.")
}
}
// play the next song in the playlist
fn next(&mut self) {
// increment the current index and wrap around if needed
self.current += 1;
}
// play the previous song in the playlist
fn prev(&mut self) {
// decrement the current index and wrap around if needed
self.current -= 1;
}
// stop the playlist
fn stop(&mut self) {
// reset the current index to zero
self.current = 0;
println!("Playlist stopped.");
}
}
fn main() {
// create some sample songs
let song1 = Music {
title: "Bohemian Rhapsody".to_string(),
artist: "Queen".to_string(),
album: "A Night at the Opera".to_string(),
duration: 355,
liked: false,
};
let song2 = Music {
title: "Stairway to Heaven".to_string(),
artist: "Led Zeppelin".to_string(),
album: "Led Zeppelin IV".to_string(),
duration: 482,
liked: false,
};
let song3 = Music {
title: "Hotel California".to_string(),
artist: "Eagles".to_string(),
album: "Hotel California".to_string(),
duration: 390,
liked: false,
};
// create a playlist
let mut playlist = Playlist::new("Up is Down".to_string());
println!("Playlist: {} has been created", playlist.name);
// add songs to the playlist
playlist.add_song(song1);
playlist.add_song(song2);
playlist.add_song(song3);
// play the playlist
let playing = playlist.now_playing();
println!("{}", playing);
playlist.next();
let playing = playlist.now_playing();
println!("{}", playing);
playlist.toggle_like();
playlist.prev();
let playing = playlist.now_playing();
println!("{}", playing);
playlist.stop();
playlist.remove_song("Bohemian Rhapsody");
}
#[cfg(test)]
mod unit_tests {
use super::*;
// create a sample song for testing
fn sample_song() -> Music {
Music {
title: "Bohemian Rhapsody".to_string(),
artist: "Queen".to_string(),
album: "A Night at the Opera".to_string(),
duration: 355,
liked: false,
}
}
// create a sample playlist for testing
fn sample_playlist() -> Playlist {
// create some sample songs
let song1 = Music {
title: "Bohemian Rhapsody".to_string(),
artist: "Queen".to_string(),
album: "A Night at the Opera".to_string(),
duration: 355,
liked: false,
};
let song2 = Music {
title: "Stairway to Heaven".to_string(),
artist: "Led Zeppelin".to_string(),
album: "Led Zeppelin IV".to_string(),
duration: 482,
liked: false,
};
let song3 = Music {
title: "Hotel California".to_string(),
artist: "Eagles".to_string(),
album: "Hotel California".to_string(),
duration: 390,
liked: false,
};
// create a playlist with a name and some songs
let mut playlist = Playlist::new("My Playlist".to_string());
playlist.add_song(song1);
playlist.add_song(song2);
playlist.add_song(song3);
playlist
}
// test the new method of the Playlist struct
#[test]
fn test_new_playlist() {
// create a new playlist with a name
let playlist = Playlist::new("My Playlist".to_string());
// check the name of the playlist
assert_eq!(playlist.name, "My Playlist");
// check the songs vector is empty
assert_eq!(playlist.songs.len(), 0);
// check the current index is zero
assert_eq!(playlist.current, 0);
}
// test the add_song method of the Playlist struct
#[test]
fn test_add_song() {
// create a mutable playlist
let mut playlist = Playlist::new("My Playlist".to_string());
// create a song
let song = sample_song();
// add the song to the playlist
playlist.add_song(song);
// check the songs vector has one element
assert_eq!(playlist.songs.len(), 1);
// check the song in the playlist is the same as the added song
assert_eq!(playlist.songs[0].title, "Bohemian Rhapsody");
assert_eq!(playlist.songs[0].artist, "Queen");
assert_eq!(playlist.songs[0].album, "A Night at the Opera");
assert_eq!(playlist.songs[0].duration, 355);
assert_eq!(playlist.songs[0].liked, false);
}
// test the remove_song method of the Playlist struct
#[test]
fn test_remove_song() {
// create a mutable playlist
let mut playlist = sample_playlist();
// check the initial songs vector length
assert_eq!(playlist.songs.len(), 3);
// remove a song by title
playlist.remove_song("Bohemian Rhapsody");
// check the updated songs vector length
assert_eq!(playlist.songs.len(), 2);
// check the song is removed from the playlist
assert!(!playlist
.songs
.iter()
.any(|s| s.title == "Bohemian Rhapsody"));
}
// test the toggle_like method of the Playlist struct
#[test]
fn test_toggle_like_playlist() {
// create a mutable playlist
let mut playlist = sample_playlist();
// check the initial liked status of the current song
assert_eq!(playlist.songs[playlist.current].liked, false);
// toggle the like status of the current song
playlist.toggle_like();
// check the updated liked status of the current song
assert_eq!(playlist.songs[playlist.current].liked, true);
}
// test the now_playing method of the Playlist struct
#[test]
fn test_now_playing() {
// create a playlist
let playlist = sample_playlist();
// get the current song
let song = &playlist.songs[playlist.current];
let output = playlist.now_playing();
// check the output matches the expected format
assert!(output.contains(&song.title));
}
// test the next method of the Playlist struct
#[test]
fn test_next() {
// create a mutable playlist
let mut playlist = sample_playlist();
// check the initial current index
assert_eq!(playlist.current, 0);
// play the next song in the playlist
playlist.next();
// check the updated current index
assert_eq!(playlist.current, 1);
}
// test the prev method of the Playlist struct
#[test]
fn test_prev() {
// create a mutable playlist
let mut playlist = sample_playlist();
// set the current index to the last song
playlist.current = playlist.songs.len() - 1;
// check the initial current index
assert_eq!(playlist.current, 2);
// play the previous song in the playlist
playlist.prev();
// check the updated current index
assert_eq!(playlist.current, 1);
}
// test the stop method of the Playlist struct
#[test]
fn test_stop() {
// create a mutable playlist
let mut playlist = sample_playlist();
// set the current index to a non-zero value
playlist.current = 1;
// check the initial current index
assert_eq!(playlist.current, 1);
// stop the playlist
playlist.stop();
// check the updated current index
assert_eq!(playlist.current, 0);
}
}