| 1 | #[derive(Debug)] |
| 2 | pub struct ContentList<T> { |
| 3 | pub list: Vec<T>, |
| 4 | pub index: usize, |
| 5 | } |
| 6 | |
| 7 | impl<T> ContentList<T> { |
| 8 | pub fn new() -> Self { |
| 9 | ContentList { |
| 10 | list: Vec::new(), |
| 11 | index: 0, |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | // Go to next item in list |
| 16 | pub fn next(&mut self) { |
| 17 | let len = self.list.len(); |
| 18 | if len != 0 && self.index < len - 1 { |
| 19 | self.index += 1; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /// Go to previous item in list |
| 24 | pub fn prev(&mut self) { |
| 25 | if self.index != 0 { |
| 26 | self.index -= 1; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub fn reset_index(&mut self) { |
| 31 | self.index = 0; |
| 32 | } |
| 33 | |
| 34 | /// Returns the self.list[self.index] item |
| 35 | pub fn get_item_at_current_index(&mut self) -> &T { |
| 36 | self.list.get(self.index).unwrap() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl<T> Default for ContentList<T> { |
| 41 | fn default() -> Self { |
| 42 | Self::new() |
| 43 | } |
| 44 | } |