selene-core 0.3.0

selene-core is the backend for Selene, a local-first music player
Documentation
use sled::Db;

use crate::{
    database::{
        CompareAndSwapTransaction, Createable, DatabaseEntry, DatabaseError, collection_tree,
    },
    library::collection::{Collection, CollectionId},
};

impl DatabaseEntry for Collection {
    type Id = CollectionId;

    const VERSION_NUMBER: u32 = 1;

    fn tree(db: &Db) -> sled::Tree {
        collection_tree(db)
    }

    fn id(&self) -> Self::Id {
        self.id
    }
}

pub struct CollectionCreateArgs {
    name: String,
}

impl Createable for Collection {
    type CreateArgs = CollectionCreateArgs;

    fn tx_create(
        cas_tx: &mut CompareAndSwapTransaction,
        args: Self::CreateArgs,
    ) -> Result<Self::Id, DatabaseError> {
        let collection = Collection::new(args.name).ok_or(DatabaseError::InvalidInput(
            "Name field must not be empty".to_owned(),
        ))?;

        if cas_tx.tx_get(collection.id())?.is_some() {
            return Err(DatabaseError::AlreadyInDatabase);
        }

        Ok(collection.id())
    }
}