diff options
| author | Jokler <jokler@protonmail.com> | 2020-01-07 06:48:43 +0100 |
|---|---|---|
| committer | Jokler <jokler@protonmail.com> | 2020-01-07 06:48:43 +0100 |
| commit | daa895e3be120ba640ca29d7653a03b6a547f4a0 (patch) | |
| tree | 2312ffc926afe064334b5152441d0c71bb5f286e /src/playlist.rs | |
| parent | 6ff7cd94cf612e7eb2d54d2a3b754a97899b6a94 (diff) | |
| download | pokebot-daa895e3be120ba640ca29d7653a03b6a547f4a0.tar.gz pokebot-daa895e3be120ba640ca29d7653a03b6a547f4a0.zip | |
Add playlist feature and default channel flag
Diffstat (limited to 'src/playlist.rs')
| -rw-r--r-- | src/playlist.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/playlist.rs b/src/playlist.rs new file mode 100644 index 0000000..e0df816 --- /dev/null +++ b/src/playlist.rs @@ -0,0 +1,68 @@ +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(3), + read: 0, + write: 0, + is_full: false, + } + } + + pub fn push(&mut self, req: AudioRequest) -> bool { + if self.is_full { + return false; + } + + 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.read += 1; + self.is_full = false; + self.data[self.read].take() + } + } + + pub fn clear(&mut self) { + self.data.clear(); + self.read = 0; + self.write = 0; + self.is_full = false; + } +} + +#[derive(Clone, Debug)] +pub struct AudioRequest { + pub title: String, + pub address: String, +} |
