aboutsummaryrefslogtreecommitdiffstats
path: root/src/playlist.rs
blob: 445f8a5cfc251170f4aa2799be8b5fb06df0f53e (plain) (blame)
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
use std::collections::VecDeque;

use log::info;

use crate::youtube_dl::AudioMetadata;

pub struct Playlist {
    data: VecDeque<AudioMetadata>,
}

impl Playlist {
    pub fn new() -> Self {
        Self {
            data: VecDeque::new(),
        }
    }

    pub fn push(&mut self, data: AudioMetadata) {
        info!("Adding {:?} to playlist", &data.title);

        self.data.push_front(data)
    }

    pub fn pop(&mut self) -> Option<AudioMetadata> {
        let res = self.data.pop_back();
        info!("Popping {:?} from playlist", res.as_ref().map(|r| &r.title));

        res
    }

    pub fn to_vec(&self) -> Vec<AudioMetadata> {
        let (a, b) = self.data.as_slices();

        let mut res = a.to_vec();
        res.extend_from_slice(b);
        res.reverse();

        res
    }

    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    pub fn clear(&mut self) {
        self.data.clear();

        info!("Cleared playlist")
    }
}