Removed error mod and all Location generics and trait

This commit is contained in:
MrDulfin 2024-07-07 19:08:51 -04:00
parent c41044f53f
commit 8b0e77cfca
3 changed files with 35 additions and 51 deletions

2
Cargo.lock generated
View file

@ -4,7 +4,7 @@ version = 3
[[package]] [[package]]
name = "kushi" name = "kushi"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"thiserror", "thiserror",
] ]

View file

@ -1,13 +0,0 @@
use thiserror::Error;
#[derive(Error, Debug)]
pub enum QueueError {
#[error("Index out of bounds! Index {index} is over len {len}")]
OutOfBounds { index: usize, len: usize },
#[error("The Queue is empty!")]
EmptyQueue,
#[error("There are no past played songs!")]
EmptyPlayed,
#[error("There is no item after this in the Queue")]
NoNext,
}

View file

@ -1,12 +1,5 @@
use std::fmt::Debug; use std::fmt::Debug;
use error::QueueError;
pub mod error;
pub trait Location {}
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy)]
pub enum QueueState { pub enum QueueState {
Played, Played,
@ -20,11 +13,9 @@ pub enum QueueState {
pub struct QueueItem< pub struct QueueItem<
T: Debug + Clone + PartialEq, // T: The Singular Item Type T: Debug + Clone + PartialEq, // T: The Singular Item Type
U: Debug + PartialEq + Clone + IntoIterator, // U: an Iterator U: Debug + PartialEq + Clone + IntoIterator, // U: an Iterator
L: Debug + PartialEq + Clone + Copy + Location // L: The Location Type. Optional but maybe useful
> { > {
pub item: QueueItemType<T, U>, pub item: QueueItemType<T, U>,
pub state: QueueState, pub state: QueueState,
pub source: Option<L>,
pub by_human: bool, pub by_human: bool,
} }
@ -55,14 +46,12 @@ impl<
impl< impl<
T: Debug + Clone + PartialEq, T: Debug + Clone + PartialEq,
U: Debug + PartialEq + Clone + IntoIterator, U: Debug + PartialEq + Clone + IntoIterator,
L: Debug + PartialEq + Clone + Copy + Location
> >
QueueItem<T, U, L> { QueueItem<T, U> {
pub fn from_item_type(item: QueueItemType<T, U>, source: Option<L>) -> Self { pub fn from_item_type(item: QueueItemType<T, U>) -> Self {
QueueItem { QueueItem {
item, item,
state: QueueState::NoState, state: QueueState::NoState,
source,
by_human: false, by_human: false,
} }
} }
@ -72,10 +61,9 @@ QueueItem<T, U, L> {
pub struct Queue< pub struct Queue<
T: Debug + Clone + PartialEq, // T: The Singular Item Type T: Debug + Clone + PartialEq, // T: The Singular Item Type
U: Debug + PartialEq + Clone + IntoIterator, // U: The Multi-Item Type. Needs to be tracked as multiple items U: Debug + PartialEq + Clone + IntoIterator, // U: The Multi-Item Type. Needs to be tracked as multiple items
L: Debug + PartialEq + Clone + Copy + Location // L: The Location Type. Optional but maybe useful
> { > {
pub items: Vec<QueueItem<T, U, L>>, pub items: Vec<QueueItem<T, U>>,
pub played: Vec<QueueItem<T, U, L>>, pub played: Vec<QueueItem<T, U>>,
pub loop_: bool, pub loop_: bool,
pub shuffle: Option<Vec<usize>>, pub shuffle: Option<Vec<usize>>,
} }
@ -84,8 +72,7 @@ pub struct Queue<
impl< impl<
T: Debug + Clone + PartialEq, T: Debug + Clone + PartialEq,
U: Debug + PartialEq + Clone + IntoIterator, U: Debug + PartialEq + Clone + IntoIterator,
L: Debug + PartialEq + Clone + Copy + Location > Queue<T, U> {
> Queue<T, U, L> {
fn has_addhere(&self) -> bool { fn has_addhere(&self) -> bool {
for item in &self.items { for item in &self.items {
if item.state == QueueState::AddHere { if item.state == QueueState::AddHere {
@ -115,14 +102,14 @@ impl<
} }
} }
pub fn set_items(&mut self, tracks: Vec<QueueItem<T, U, L>>) { pub fn set_items(&mut self, tracks: Vec<QueueItem<T, U>>) {
let mut tracks = tracks; let mut tracks = tracks;
self.items.clear(); self.items.clear();
self.items.append(&mut tracks); self.items.append(&mut tracks);
} }
/// Inserts an item after the AddHere item /// Inserts an item after the AddHere item
pub fn add_item(&mut self, item: T, source: Option<L>, by_human: bool) { pub fn add_item(&mut self, item: T, by_human: bool) {
let item = QueueItemType::from_single(item); let item = QueueItemType::from_single(item);
let mut i: usize = 0; let mut i: usize = 0;
@ -139,21 +126,20 @@ impl<
} }
item_ item_
}) })
.collect::<Vec<QueueItem<T, U, L>>>(); .collect::<Vec<QueueItem<T, U>>>();
self.items.insert( self.items.insert(
i + if self.items.is_empty() { 0 } else { 1 }, i + if self.items.is_empty() { 0 } else { 1 },
QueueItem { QueueItem {
item, item,
state: QueueState::AddHere, state: QueueState::AddHere,
source,
by_human, by_human,
}, },
); );
} }
/// Inserts an item after the currently playing item /// Inserts an item after the currently playing item
pub fn add_item_next(&mut self, item: T, source: Option<L>) { pub fn add_item_next(&mut self, item: T) {
let item = QueueItemType::from_single(item); let item = QueueItemType::from_single(item);
use QueueState::*; use QueueState::*;
let empty = self.items.is_empty(); let empty = self.items.is_empty();
@ -170,13 +156,12 @@ impl<
} else { } else {
NoState NoState
}, },
source,
by_human: true, by_human: true,
}, },
) )
} }
pub fn add_multi(&mut self, items: Vec<QueueItemType<T, U>>, source: Option<L>, by_human: bool) { pub fn add_multi(&mut self, items: Vec<QueueItemType<T, U>>, by_human: bool) {
let mut i: usize = 0; let mut i: usize = 0;
self.items = self self.items = self
@ -192,7 +177,7 @@ impl<
} }
item_ item_
}) })
.collect::<Vec<QueueItem<T, U, L>>>(); .collect::<Vec<QueueItem<T, U>>>();
let empty = self.items.is_empty(); let empty = self.items.is_empty();
@ -203,7 +188,6 @@ impl<
QueueItem { QueueItem {
item, item,
state: QueueState::NoState, state: QueueState::NoState,
source,
by_human, by_human,
}, },
); );
@ -212,7 +196,7 @@ impl<
} }
/// Add multiple Items after the currently playing Item /// Add multiple Items after the currently playing Item
pub fn add_multi_next(&mut self, items: Vec<QueueItemType<T, U>>, source: Option<L>) { pub fn add_multi_next(&mut self, items: Vec<QueueItemType<T, U>>) {
use QueueState::*; use QueueState::*;
let empty = self.items.is_empty(); let empty = self.items.is_empty();
@ -228,7 +212,6 @@ impl<
QueueItem { QueueItem {
item, item,
state: NoState, state: NoState,
source,
by_human: true, by_human: true,
}, },
) )
@ -239,7 +222,7 @@ impl<
} }
} }
pub fn remove_item(&mut self, remove_index: usize) -> Result<QueueItem<T, U, L>, QueueError> { pub fn remove_item(&mut self, remove_index: usize) -> Result<QueueItem<T, U>, QueueError> {
// dbg!(/*&remove_index, self.current_index(), &index,*/ &self.items[remove_index]); // dbg!(/*&remove_index, self.current_index(), &index,*/ &self.items[remove_index]);
if remove_index < self.items.len() { if remove_index < self.items.len() {
@ -257,7 +240,6 @@ impl<
&mut self, &mut self,
index: usize, index: usize,
new_item: QueueItemType<T, U>, new_item: QueueItemType<T, U>,
source: Option<L>,
addhere: bool, addhere: bool,
) -> Result<(), QueueError> { ) -> Result<(), QueueError> {
if self.items.get_mut(index).is_none() if self.items.get_mut(index).is_none()
@ -270,7 +252,7 @@ impl<
}); });
} }
if addhere { if addhere {
let mut new_item = QueueItem::from_item_type(new_item, source); let mut new_item = QueueItem::from_item_type(new_item);
for item in &mut self.items { for item in &mut self.items {
if item.state == QueueState::AddHere { if item.state == QueueState::AddHere {
item.state = QueueState::NoState item.state = QueueState::NoState
@ -279,7 +261,7 @@ impl<
new_item.state = QueueState::AddHere; new_item.state = QueueState::AddHere;
self.items.insert(index, new_item); self.items.insert(index, new_item);
} else { } else {
let new_item = QueueItem::from_item_type(new_item, source); let new_item = QueueItem::from_item_type(new_item);
self.items.insert(index, new_item); self.items.insert(index, new_item);
} }
Ok(()) Ok(())
@ -374,7 +356,7 @@ impl<
} }
#[allow(clippy::should_implement_trait)] #[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Result<&QueueItem<T, U, L>, QueueError> { pub fn next(&mut self) -> Result<&QueueItem<T, U>, QueueError> {
if self.items.is_empty() { if self.items.is_empty() {
if self.loop_ { if self.loop_ {
unimplemented!() // TODO: add function to loop the queue unimplemented!() // TODO: add function to loop the queue
@ -403,7 +385,7 @@ impl<
} }
} }
pub fn prev(&mut self) -> Result<&QueueItem<T, U, L>, QueueError> { pub fn prev(&mut self) -> Result<&QueueItem<T, U>, QueueError> {
if let Some(item) = self.played.pop() { if let Some(item) = self.played.pop() {
if item.state == QueueState::First && self.loop_ { if item.state == QueueState::First && self.loop_ {
todo!() todo!()
@ -422,7 +404,7 @@ impl<
} }
} }
pub fn current(&self) -> Result<&QueueItem<T, U, L>, QueueError> { pub fn current(&self) -> Result<&QueueItem<T, U>, QueueError> {
if !self.items.is_empty() { if !self.items.is_empty() {
if let QueueItemType::Multi(_) = self.items[0].item { if let QueueItemType::Multi(_) = self.items[0].item {
unimplemented!(); // TODO: Handle Multi items here? unimplemented!(); // TODO: Handle Multi items here?
@ -439,3 +421,18 @@ impl<
} }
} }
} }
use thiserror::Error;
#[derive(Error, Debug)]
pub enum QueueError {
#[error("Index out of bounds! Index {index} is over len {len}")]
OutOfBounds { index: usize, len: usize },
#[error("The Queue is empty!")]
EmptyQueue,
#[error("There are no past played songs!")]
EmptyPlayed,
#[error("There is no item after this in the Queue")]
NoNext,
}