Skip to content

Commit

Permalink
Checklist #2 to #5 done
Browse files Browse the repository at this point in the history
  • Loading branch information
Stridsvagn69420 committed Oct 26, 2022
1 parent 4bdf5ec commit 87a677c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 6 deletions.
68 changes: 65 additions & 3 deletions src/album.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Serialize, Deserialize};
use super::Artist;
use std::cmp::PartialEq;
use super::{Artist, add_vec, remove_vec};

/// Album
///
Expand All @@ -24,12 +25,73 @@ pub struct Album {
/// Artists
///
/// The Artists of the album.
pub authors: Vec<Artist>,
pub artists: Vec<Artist>,

/// Files
///
/// All files present in the album as a relative path.
pub files: Vec<String>
}

// TODO: Add trait impls and useful functions here
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<Vec<Artist>>, files: Option<Vec<String>>) -> 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
}
}
91 changes: 90 additions & 1 deletion src/artist.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand All @@ -23,4 +28,88 @@ pub struct Artist {
pub features: Vec<String>
}

// TODO: Add trait impls here and other useful functions
impl Artist {
/// New Artist
///
/// Creates a new Artist
pub fn new(name: String, website: Option<String>, music: Vec<String>, features: Vec<String>) -> 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<Owner> for Artist {
fn into(self) -> Owner {
Owner {
name: self.name,
email: None,
website: self.website
}
}
}

impl From<Owner> 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
}
}
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
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<T: PartialEq>(arr: &mut Vec<T>, 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<T: PartialEq>(arr: &mut Vec<T>, elm: T) {
if !arr.contains(&elm) {
arr.push(elm);
}
}
2 changes: 1 addition & 1 deletion src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Owner {
/// Website
///
/// The website or profile of an owner. This is optional.
pub website: String
pub website: Option<String>
}

// TODO: Add traits and other useful functions here

0 comments on commit 87a677c

Please sign in to comment.