summaryrefslogtreecommitdiffstats
path: root/src/playlist.rs
blob: 689a743da9b2e8156f374709f6f3f582e2ea41f2 (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
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
use log::info;

pub struct Playlist {
    data: Vec<Option<AudioRequest>>,
    read: usize,
    write: usize,
    is_full: bool,
}

impl Playlist {
    pub fn new() -> Self {
        Self {
            data: Vec::with_capacity(50),
            read: 0,
            write: 0,
            is_full: false,
        }
    }

    pub fn push(&mut self, req: AudioRequest) -> bool {
        if self.is_full {
            return false;
        }

        info!("Adding {} to playlist", &req.title);

        if self.data.len() < self.data.capacity() {
            self.data.push(Some(req));
        } else {
            self.data[self.write] = Some(req);
        }

        self.write = (self.write + 1) % self.data.capacity();

        if self.write == self.read {
            self.is_full = true;
        }

        true
    }

    pub fn is_empty(&self) -> bool {
        !self.is_full && self.write == self.read
    }

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

    pub fn pop(&mut self) -> Option<AudioRequest> {
        if self.is_empty() {
            None
        } else {
            self.is_full = false;
            let res = self.data[self.read].take();
            self.read = (self.read + 1) % self.data.capacity();

            info!("Popping {:?} from playlist", res.as_ref().map(|r| &r.title));

            res
        }
    }

    pub fn clear(&mut self) {
        self.data.clear();
        self.read = 0;
        self.write = 0;
        self.is_full = false;

        info!("Cleared playlist")
    }
}

#[derive(Clone, Debug)]
pub struct AudioRequest {
    pub title: String,
    pub address: String,
}