selene-core 0.3.1

selene-core is the backend for Selene, a local-first music player
Documentation
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 {
    /// Creates a new collection with the input name
    ///
    /// # Errors
    ///
    /// This function will return `None` if the input name is empty or is trimmed to an empty string
    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()),
        })
    }
}