@@ -13,7 +13,7 @@ use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};
1313
1414/// Database drivers.
1515#[ derive( PartialEq , Eq , Debug , Clone , Serialize , Deserialize ) ]
16- pub enum DatabaseDriver {
16+ pub enum Driver {
1717 Sqlite3 ,
1818 Mysql ,
1919}
@@ -50,7 +50,7 @@ pub enum Sorting {
5050
5151/// Database errors.
5252#[ derive( Debug ) ]
53- pub enum DatabaseError {
53+ pub enum Error {
5454 Error ,
5555 UnrecognizedDatabaseDriver , // when the db path does not start with sqlite or mysql
5656 UsernameTaken ,
@@ -64,7 +64,11 @@ pub enum DatabaseError {
6464}
6565
6666/// Connect to a database.
67- pub async fn connect_database ( db_path : & str ) -> Result < Box < dyn Database > , DatabaseError > {
67+ ///
68+ /// # Errors
69+ ///
70+ /// This function will return an `Error::UnrecognizedDatabaseDriver` if unable to match database type.
71+ pub async fn connect ( db_path : & str ) -> Result < Box < dyn Database > , Error > {
6872 match & db_path. chars ( ) . collect :: < Vec < char > > ( ) as & [ char ] {
6973 [ 's' , 'q' , 'l' , 'i' , 't' , 'e' , ..] => {
7074 let db = SqliteDatabase :: new ( db_path) . await ;
@@ -74,66 +78,66 @@ pub async fn connect_database(db_path: &str) -> Result<Box<dyn Database>, Databa
7478 let db = MysqlDatabase :: new ( db_path) . await ;
7579 Ok ( Box :: new ( db) )
7680 }
77- _ => Err ( DatabaseError :: UnrecognizedDatabaseDriver ) ,
81+ _ => Err ( Error :: UnrecognizedDatabaseDriver ) ,
7882 }
7983}
8084
8185/// Trait for database implementations.
8286#[ async_trait]
8387pub trait Database : Sync + Send {
8488 /// Return current database driver.
85- fn get_database_driver ( & self ) -> DatabaseDriver ;
89+ fn get_database_driver ( & self ) -> Driver ;
8690
8791 /// Add new user and return the newly inserted `user_id`.
88- async fn insert_user_and_get_id ( & self , username : & str , email : & str , password : & str ) -> Result < i64 , DatabaseError > ;
92+ async fn insert_user_and_get_id ( & self , username : & str , email : & str , password : & str ) -> Result < i64 , Error > ;
8993
9094 /// Get `User` from `user_id`.
91- async fn get_user_from_id ( & self , user_id : i64 ) -> Result < User , DatabaseError > ;
95+ async fn get_user_from_id ( & self , user_id : i64 ) -> Result < User , Error > ;
9296
9397 /// Get `UserAuthentication` from `user_id`.
94- async fn get_user_authentication_from_id ( & self , user_id : i64 ) -> Result < UserAuthentication , DatabaseError > ;
98+ async fn get_user_authentication_from_id ( & self , user_id : i64 ) -> Result < UserAuthentication , Error > ;
9599
96100 /// Get `UserProfile` from `username`.
97- async fn get_user_profile_from_username ( & self , username : & str ) -> Result < UserProfile , DatabaseError > ;
101+ async fn get_user_profile_from_username ( & self , username : & str ) -> Result < UserProfile , Error > ;
98102
99103 /// Get `UserCompact` from `user_id`.
100- async fn get_user_compact_from_id ( & self , user_id : i64 ) -> Result < UserCompact , DatabaseError > ;
104+ async fn get_user_compact_from_id ( & self , user_id : i64 ) -> Result < UserCompact , Error > ;
101105
102106 /// Get a user's `TrackerKey`.
103107 async fn get_user_tracker_key ( & self , user_id : i64 ) -> Option < TrackerKey > ;
104108
105109 /// Get total user count.
106- async fn count_users ( & self ) -> Result < i64 , DatabaseError > ;
110+ async fn count_users ( & self ) -> Result < i64 , Error > ;
107111
108112 /// Ban user with `user_id`, `reason` and `date_expiry`.
109- async fn ban_user ( & self , user_id : i64 , reason : & str , date_expiry : NaiveDateTime ) -> Result < ( ) , DatabaseError > ;
113+ async fn ban_user ( & self , user_id : i64 , reason : & str , date_expiry : NaiveDateTime ) -> Result < ( ) , Error > ;
110114
111115 /// Grant a user the administrator role.
112- async fn grant_admin_role ( & self , user_id : i64 ) -> Result < ( ) , DatabaseError > ;
116+ async fn grant_admin_role ( & self , user_id : i64 ) -> Result < ( ) , Error > ;
113117
114118 /// Verify a user's email with `user_id`.
115- async fn verify_email ( & self , user_id : i64 ) -> Result < ( ) , DatabaseError > ;
119+ async fn verify_email ( & self , user_id : i64 ) -> Result < ( ) , Error > ;
116120
117121 /// Link a `TrackerKey` to a certain user with `user_id`.
118- async fn add_tracker_key ( & self , user_id : i64 , tracker_key : & TrackerKey ) -> Result < ( ) , DatabaseError > ;
122+ async fn add_tracker_key ( & self , user_id : i64 , tracker_key : & TrackerKey ) -> Result < ( ) , Error > ;
119123
120124 /// Delete user and all related user data with `user_id`.
121- async fn delete_user ( & self , user_id : i64 ) -> Result < ( ) , DatabaseError > ;
125+ async fn delete_user ( & self , user_id : i64 ) -> Result < ( ) , Error > ;
122126
123127 /// Add a new category and return `category_id`.
124- async fn insert_category_and_get_id ( & self , category_name : & str ) -> Result < i64 , DatabaseError > ;
128+ async fn insert_category_and_get_id ( & self , category_name : & str ) -> Result < i64 , Error > ;
125129
126130 /// Get `Category` from `category_id`.
127- async fn get_category_from_id ( & self , category_id : i64 ) -> Result < Category , DatabaseError > ;
131+ async fn get_category_from_id ( & self , category_id : i64 ) -> Result < Category , Error > ;
128132
129133 /// Get `Category` from `category_name`.
130- async fn get_category_from_name ( & self , category_name : & str ) -> Result < Category , DatabaseError > ;
134+ async fn get_category_from_name ( & self , category_name : & str ) -> Result < Category , Error > ;
131135
132136 /// Get all categories as `Vec<Category>`.
133- async fn get_categories ( & self ) -> Result < Vec < Category > , DatabaseError > ;
137+ async fn get_categories ( & self ) -> Result < Vec < Category > , Error > ;
134138
135139 /// Delete category with `category_name`.
136- async fn delete_category ( & self , category_name : & str ) -> Result < ( ) , DatabaseError > ;
140+ async fn delete_category ( & self , category_name : & str ) -> Result < ( ) , Error > ;
137141
138142 /// Get results of a torrent search in a paginated and sorted form as `TorrentsResponse` from `search`, `categories`, `sort`, `offset` and `page_size`.
139143 async fn get_torrents_search_sorted_paginated (
@@ -143,7 +147,7 @@ pub trait Database: Sync + Send {
143147 sort : & Sorting ,
144148 offset : u64 ,
145149 page_size : u8 ,
146- ) -> Result < TorrentsResponse , DatabaseError > ;
150+ ) -> Result < TorrentsResponse , Error > ;
147151
148152 /// Add new torrent and return the newly inserted `torrent_id` with `torrent`, `uploader_id`, `category_id`, `title` and `description`.
149153 async fn insert_torrent_and_get_id (
@@ -153,10 +157,10 @@ pub trait Database: Sync + Send {
153157 category_id : i64 ,
154158 title : & str ,
155159 description : & str ,
156- ) -> Result < i64 , DatabaseError > ;
160+ ) -> Result < i64 , Error > ;
157161
158162 /// Get `Torrent` from `InfoHash`.
159- async fn get_torrent_from_infohash ( & self , infohash : & InfoHash ) -> Result < Torrent , DatabaseError > {
163+ async fn get_torrent_from_infohash ( & self , infohash : & InfoHash ) -> Result < Torrent , Error > {
160164 let torrent_info = self . get_torrent_info_from_infohash ( infohash) . await ?;
161165
162166 let torrent_files = self . get_torrent_files_from_id ( torrent_info. torrent_id ) . await ?;
@@ -171,7 +175,7 @@ pub trait Database: Sync + Send {
171175 }
172176
173177 /// Get `Torrent` from `torrent_id`.
174- async fn get_torrent_from_id ( & self , torrent_id : i64 ) -> Result < Torrent , DatabaseError > {
178+ async fn get_torrent_from_id ( & self , torrent_id : i64 ) -> Result < Torrent , Error > {
175179 let torrent_info = self . get_torrent_info_from_id ( torrent_id) . await ?;
176180
177181 let torrent_files = self . get_torrent_files_from_id ( torrent_id) . await ?;
@@ -186,44 +190,38 @@ pub trait Database: Sync + Send {
186190 }
187191
188192 /// Get torrent's info as `DbTorrentInfo` from `torrent_id`.
189- async fn get_torrent_info_from_id ( & self , torrent_id : i64 ) -> Result < DbTorrentInfo , DatabaseError > ;
193+ async fn get_torrent_info_from_id ( & self , torrent_id : i64 ) -> Result < DbTorrentInfo , Error > ;
190194
191195 /// Get torrent's info as `DbTorrentInfo` from torrent `InfoHash`.
192- async fn get_torrent_info_from_infohash ( & self , info_hash : & InfoHash ) -> Result < DbTorrentInfo , DatabaseError > ;
196+ async fn get_torrent_info_from_infohash ( & self , info_hash : & InfoHash ) -> Result < DbTorrentInfo , Error > ;
193197
194198 /// Get all torrent's files as `Vec<TorrentFile>` from `torrent_id`.
195- async fn get_torrent_files_from_id ( & self , torrent_id : i64 ) -> Result < Vec < TorrentFile > , DatabaseError > ;
199+ async fn get_torrent_files_from_id ( & self , torrent_id : i64 ) -> Result < Vec < TorrentFile > , Error > ;
196200
197201 /// Get all torrent's announce urls as `Vec<Vec<String>>` from `torrent_id`.
198- async fn get_torrent_announce_urls_from_id ( & self , torrent_id : i64 ) -> Result < Vec < Vec < String > > , DatabaseError > ;
202+ async fn get_torrent_announce_urls_from_id ( & self , torrent_id : i64 ) -> Result < Vec < Vec < String > > , Error > ;
199203
200204 /// Get `TorrentListing` from `torrent_id`.
201- async fn get_torrent_listing_from_id ( & self , torrent_id : i64 ) -> Result < TorrentListing , DatabaseError > ;
205+ async fn get_torrent_listing_from_id ( & self , torrent_id : i64 ) -> Result < TorrentListing , Error > ;
202206
203207 /// Get `TorrentListing` from `InfoHash`.
204- async fn get_torrent_listing_from_infohash ( & self , infohash : & InfoHash ) -> Result < TorrentListing , DatabaseError > ;
208+ async fn get_torrent_listing_from_infohash ( & self , infohash : & InfoHash ) -> Result < TorrentListing , Error > ;
205209
206210 /// Get all torrents as `Vec<TorrentCompact>`.
207- async fn get_all_torrents_compact ( & self ) -> Result < Vec < TorrentCompact > , DatabaseError > ;
211+ async fn get_all_torrents_compact ( & self ) -> Result < Vec < TorrentCompact > , Error > ;
208212
209213 /// Update a torrent's title with `torrent_id` and `title`.
210- async fn update_torrent_title ( & self , torrent_id : i64 , title : & str ) -> Result < ( ) , DatabaseError > ;
214+ async fn update_torrent_title ( & self , torrent_id : i64 , title : & str ) -> Result < ( ) , Error > ;
211215
212216 /// Update a torrent's description with `torrent_id` and `description`.
213- async fn update_torrent_description ( & self , torrent_id : i64 , description : & str ) -> Result < ( ) , DatabaseError > ;
217+ async fn update_torrent_description ( & self , torrent_id : i64 , description : & str ) -> Result < ( ) , Error > ;
214218
215219 /// Update the seeders and leechers info for a torrent with `torrent_id`, `tracker_url`, `seeders` and `leechers`.
216- async fn update_tracker_info (
217- & self ,
218- torrent_id : i64 ,
219- tracker_url : & str ,
220- seeders : i64 ,
221- leechers : i64 ,
222- ) -> Result < ( ) , DatabaseError > ;
220+ async fn update_tracker_info ( & self , torrent_id : i64 , tracker_url : & str , seeders : i64 , leechers : i64 ) -> Result < ( ) , Error > ;
223221
224222 /// Delete a torrent with `torrent_id`.
225- async fn delete_torrent ( & self , torrent_id : i64 ) -> Result < ( ) , DatabaseError > ;
223+ async fn delete_torrent ( & self , torrent_id : i64 ) -> Result < ( ) , Error > ;
226224
227225 /// DELETES ALL DATABASE ROWS, ONLY CALL THIS IF YOU KNOW WHAT YOU'RE DOING!
228- async fn delete_all_database_rows ( & self ) -> Result < ( ) , DatabaseError > ;
226+ async fn delete_all_database_rows ( & self ) -> Result < ( ) , Error > ;
229227}
0 commit comments