selene_core/library/collection/
trait_impls.rs1use sled::Db;
2
3use crate::{
4 database::{
5 CompareAndSwapTransaction, Createable, DatabaseEntry, DatabaseError, collection_tree,
6 },
7 library::collection::{Collection, CollectionId},
8};
9
10impl DatabaseEntry for Collection {
11 type Id = CollectionId;
12
13 const VERSION_NUMBER: u32 = 1;
14
15 fn tree(db: &Db) -> sled::Tree {
16 collection_tree(db)
17 }
18
19 fn id(&self) -> Self::Id {
20 self.id
21 }
22}
23
24pub struct CollectionCreateArgs {
25 name: String,
26}
27
28impl Createable for Collection {
29 type CreateArgs = CollectionCreateArgs;
30
31 fn tx_create(
32 cas_tx: &mut CompareAndSwapTransaction,
33 args: Self::CreateArgs,
34 ) -> Result<Self::Id, DatabaseError> {
35 let collection = Collection::new(args.name).ok_or(DatabaseError::InvalidInput(
36 "Name field must not be empty".to_owned(),
37 ))?;
38
39 if cas_tx.tx_get(collection.id())?.is_some() {
40 return Err(DatabaseError::AlreadyInDatabase);
41 }
42
43 Ok(collection.id())
44 }
45}