diff --git a/src/album.rs b/src/album.rs index 9376985..e2b64d5 100644 --- a/src/album.rs +++ b/src/album.rs @@ -1,5 +1,6 @@ use serde::{Serialize, Deserialize}; -use super::Artist; +use std::cmp::PartialEq; +use super::{Artist, add_vec, remove_vec}; /// Album /// @@ -24,7 +25,7 @@ pub struct Album { /// Artists /// /// The Artists of the album. - pub authors: Vec, + pub artists: Vec, /// Files /// @@ -32,4 +33,65 @@ pub struct Album { pub files: Vec } -// TODO: Add trait impls and useful functions here \ No newline at end of file +impl Album { + /// New Album + /// + /// Creates a new album. If `artists` or `files` is [None], an empty array will be created for them. + pub fn new(name: String, cover: String, path: String, artists: Option>, files: Option>) -> Album { + Album { + name, + cover, + path, + artists: match artists { + Some(x) => x, + None => Vec::new() + }, + files: match files { + Some(x) => x, + None => Vec::new() + } + } + } + + /// Add [Artist] + /// + /// Adds an [Artist] to the album if they don't exist already. + pub fn add_artist(&mut self, art: Artist) -> &mut Album { + add_vec(&mut self.artists, art); + self + } + + /// Add file + /// + /// Adds a file to the album if it doesn't exist already. + pub fn add_file(&mut self, file: String) -> &mut Album { + add_vec(&mut self.files, file); + self + } + + /// Remove [Artist] + /// + /// Removes an [Artist] from the album if they already exist. + pub fn remove_artist(&mut self, art: Artist) -> &mut Album { + remove_vec(&mut self.artists, art); + self + } + + /// Remove file + /// + /// Removes a file from the album if it already exists. + pub fn remove_file(&mut self, file: String) -> &mut Album { + remove_vec(&mut self.files, file); + self + } +} + +impl PartialEq for Album { + fn eq(&self, other: &Self) -> bool { + self.name == other.name && + self.cover == other.cover && + self.path == other.path && + self.artists == other.artists && + self.files == other.files + } +} \ No newline at end of file diff --git a/src/artist.rs b/src/artist.rs index 02326dd..bfcc5e6 100644 --- a/src/artist.rs +++ b/src/artist.rs @@ -1,4 +1,9 @@ use serde::{Serialize, Deserialize}; +use std::fmt::{Display, Result}; +use std::cmp::PartialEq; +use std::convert::Into; +use std::convert::From; +use super::{Owner, add_vec, remove_vec}; /// Artist /// @@ -23,4 +28,88 @@ pub struct Artist { pub features: Vec } -// TODO: Add trait impls here and other useful functions \ No newline at end of file +impl Artist { + /// New Artist + /// + /// Creates a new Artist + pub fn new(name: String, website: Option, music: Vec, features: Vec) -> Artist { + Artist { + name, + website, + music, + features + } + } + + /// Add music + /// + /// Adds a new music entry if it doesn't exist already + pub fn add_music(&mut self, music: String) -> &mut Artist { + add_vec(&mut self.music, music); + self + } + + /// Add feature + /// + /// Adds a new featured entry if it doesn't exist already + pub fn add_feature(&mut self, featured: String) -> &mut Artist { + add_vec(&mut self.features, featured); + self + } + + /// Remove music + /// + /// Removes a music entry if it exists + pub fn remove_music(&mut self, music: String) -> &mut Artist { + remove_vec(&mut self.music, music); + self + } + + /// Remove feature + /// + /// Removes a featured entry if it exists + pub fn remove_feature(&mut self, featured: String) -> &mut Artist { + remove_vec(&mut self.features, featured); + self + } +} + +impl Into for Artist { + fn into(self) -> Owner { + Owner { + name: self.name, + email: None, + website: self.website + } + } +} + +impl From for Artist { + fn from(x: Owner) -> Artist { + Artist { + name: x.name, + website: x.website, + music: Vec::new(), + features: Vec::new() + } + } +} + +impl Display for Artist { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result { + if let Some(web) = self.website.clone() { + write!(f, "{} ({})", self.name, web) + } else { + write!(f, "{}", self.name) + } + } +} + +impl PartialEq for Artist { + fn eq(&self, other: &Self) -> bool { + self.name == other.name && + self.website == other.website && + self.music == other.music && + self.features == other.features + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 5b07610..e3320b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ // TODO: Add description here and instructions for using the library +use std::cmp::PartialEq; + /// Owner struct mod owner; pub use self::owner::Owner; @@ -50,4 +52,22 @@ pub mod meta { /// Server Routes /// /// This submodule (only visible locally) contains routes and other tools needed only for the Cyrkensia server. -pub(crate) mod server; \ No newline at end of file +pub(crate) mod server; + +/// Remove from [Vec] +/// +/// Removes an element from a [Vec], if it exists already, without returning a new [Vec]. +pub(crate) fn remove_vec(arr: &mut Vec, elm: T) { + if let Some(i) = arr.iter().position(|x| *x == elm) { + arr.remove(i); + } +} + +/// Add to [Vec] +/// +/// Adds an element to a [Vec], if the element doesn't exist yet, without returning a new [Vec]. +pub(crate) fn add_vec(arr: &mut Vec, elm: T) { + if !arr.contains(&elm) { + arr.push(elm); + } +} \ No newline at end of file diff --git a/src/owner.rs b/src/owner.rs index 0f90a7e..650df65 100644 --- a/src/owner.rs +++ b/src/owner.rs @@ -18,7 +18,7 @@ pub struct Owner { /// Website /// /// The website or profile of an owner. This is optional. - pub website: String + pub website: Option } // TODO: Add traits and other useful functions here \ No newline at end of file