use std::{convert::Infallible, str::FromStr};
use blake3::Hash;
use serde::{Deserialize, Serialize};
use crate::{
database::EntryId,
library::{album::AlbumId, artist::ArtistId, cover_art::CoverArt, track::TrackId},
};
pub mod frontend_impls;
pub mod trait_impls;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Collection {
id: CollectionId,
pub name: String,
pub cover_art: Option<CoverArt>,
pub collectables: Vec<Collectable>,
}
impl Collection {
fn new(name: String) -> Option<Self> {
let id = CollectionId::from_str(&name).unwrap();
Some(Self {
id,
name,
cover_art: None,
collectables: Vec::new(),
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Collectable {
Track(TrackId),
Artist(ArtistId),
Album(AlbumId),
Collection(CollectionId),
}
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CollectionId {
id: Hash,
}
impl EntryId for CollectionId {
type Entry = Collection;
}
impl std::ops::Deref for CollectionId {
type Target = Hash;
fn deref(&self) -> &Self::Target {
&self.id
}
}
impl FromStr for CollectionId {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
id: blake3::hash(s.as_bytes()),
})
}
}